分析:
1.飞机的出现,
2.飞机的移动通过鼠标,
3.子弹的出现
4.敌机的随机出现
5.子弹碰撞敌机 飞机碰撞敌机 碰撞的原理
namespace 飞战
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 游戏背景
Panel bg = new Panel();
//飞机
PictureBox fly = new PictureBox();
// 随机敌机位置
Random r = new Random();
//敌机的创建
Timer diji = new Timer();
// 敌机的下落
Timer di = new Timer();
int coun = 0;
int count = 0;
// 飞机下面的火焰
PictureBox f1 = new PictureBox();
PictureBox f2 = new PictureBox();
int index = 0;
int index1 = 0;
Label lb1 = new Label();
Timer zdt = new Timer();
Timer crezd = new Timer();
Label lb3 = new Label();
SoundPlayer bs = new SoundPlayer();
private void Form1_Load(object sender, EventArgs e)
{
//form的样式
this.Size = new Size(1300,800);
this.Location = new Point(20,20);
//创建背景
bg.Size = new Size(1100, 700);
bg.Location = new Point(10, 20);
bg.BackColor = Color.Gray;
bg.MouseMove += Bg_MouseMove;
this.Controls.Add(bg);
//创建飞机
fly.Size = new Size(100,100);
fly.Tag = "fj";
fly.Image = Image.FromFile(@"../../img/fei1.png");
fly.Location = new Point(bg.Width/2-fly.Width/2,bg.Height-fly.Height);
bg.Controls.Add(fly);
lb1.Size = new Size(150, 500);
lb1.Location = new Point(this.Width - lb1.Width, 30);
lb1.BackColor = Color.Aqua;
lb1.Text = "得分:" + count + "分";
lb1.Font = new Font("", 15);
this.Controls.Add(lb1);
lb3.Size = new Size(100, 30);
lb3.Location = new Point(5, 60);
lb3.ForeColor = Color.DeepPink;
lb3.Text = "开始游戏";
lb3.Click += Lb3_Click;
lb1.Controls.Add(lb3);
//子弹的计时器
zdt.Tick += Zdt_Tick;
zdt.Interval = 80;
// 子弹创建的计时器
crezd.Tick += Crezd_Tick;
crezd.Interval =400;
diji.Tick += Diji_Tick;
diji.Interval = 1000;
di.Tick += Di_Tick;
di.Interval = 60;
f2.Size = new Size(15, 30);
f2.Tag = "f2";
f2.Location = new Point(fly.Left + fly.Width / 2 - f2.Width - 20, fly.Top + fly.Height);
f2.Image = imageList3.Images[0];
f2.SizeMode = PictureBoxSizeMode.StretchImage;
bg.Controls.Add(f2);
f1.Size = new Size(15, 30);
f1.Tag = "f1";
f1.Location = new Point(fly.Left + fly.Width / 2 - f1.Width / 2, fly.Top + fly.Height);
f1.Image = imageList2.Images[0];
f1.SizeMode = PictureBoxSizeMode.StretchImage;
bg.Controls.Add(f1);
// 火焰的计时器
Timer t1 = new Timer();
Timer t2 = new Timer();
t1.Tick += T1_Tick;
t1.Interval = 10;
t1.Start();
t2.Tick += T2_Tick;
t2.Interval = 10;
t2.Start();
}
bool po = true;
private void Lb3_Click(object sender, EventArgs e)
{
if (po==true)
{
diji.Start();
di.Start();
crezd.Start();
zdt.Start();
lb3.Text = "暂停游戏";
po = false;
}
else
{
diji.Stop();
di.Stop();
crezd.Stop();
zdt.Stop();
lb3.Text = "开始游戏";
po = true;
}
}
private void T2_Tick(object sender, EventArgs e)
{
f2.Image = imageList3.Images[index1];
index1++;
if (index1 >1)
{
index1 = 0;
}
}
// 火焰的动画
private void T1_Tick(object sender, EventArgs e)
{
f1.Image = imageList2.Images[index];
index++;
if (index >1)
{
index = 0;
}
}
//敌机的移动
private void Di_Tick(object sender, EventArgs e)
{
foreach (Control dj in bg.Controls)
{
if (dj.Tag.ToString()=="diji")
{
dj.Top += 5;
}
}
}
//敌机的创建
private void Diji_Tick(object sender, EventArgs e)
{
PictureBox diji = new PictureBox();
diji.Image = Image.FromFile(@"../../img/Enemy1.png");
diji.Tag = "diji";
diji.Size = new Size(60,60);
diji.SizeMode = PictureBoxSizeMode.StretchImage;
diji.Location = new Point( r.Next(bg.Width-diji.Width),0);
coun++;
// diji.Image = Image.FromFile(@"../../img/Enemy"+r.Next(1,3)+".png");
if (coun % 2 == 0)
{
diji.Image = Image.FromFile(@"../../img/Enemy2.png");
}else if(coun%3==0)
{
diji.Image = Image.FromFile(@"../../img/Enemy3_1.png");
}
bg.Controls.Add(diji);
}
List<PictureBox> pictureBoxes = new List<PictureBox>();
// 子弹的创建
private void Crezd_Tick(object sender, EventArgs e)
{
PictureBox zd = new PictureBox();
zd.Image = Image.FromFile(@"../../img/zd1.png");
zd.Size = new Size(20, 20);
zd.Tag = "zd";
zd.Location = new Point(fly.Left + fly.Width / 2 - zd.Width + 2, fly.Top - fly.Height+zd.Height+60);
// zd.SizeMode = PictureBoxSizeMode.StretchImage;
bg.Controls.Add(zd);
pictureBoxes.Add(zd);
}
// 子弹的移动
private void Zdt_Tick(object sender, EventArgs e)
{
foreach(Control item in bg.Controls)
{
if (item.Tag.ToString() == "zd")
{
item.Top -= 10;
foreach (Control dj in bg.Controls)
{
if (dj.Tag.ToString() == "diji")
{
if (item.Left >= dj.Left && item.Left <= dj.Left + dj.Width && item.Top >= dj.Top && item.Top <= dj.Top + dj.Height)
{
item.Dispose();
dj.Dispose();
count++;
lb1.Text = "得分:" + count + "分";
//爆炸图片
PictureBox baozha = new PictureBox();
baozha.Tag = 0;
baozha.Size = new Size(50, 50);
baozha.Location = new Point(dj.Left+ dj.Width/2-baozha.Width/2, dj.Height/2+ dj.Top-baozha.Height/2);
baozha.Image = imageList1.Images[0];
bg.Controls.Add(baozha);
Timer ztim = new Timer();
ztim.Tag = baozha;
ztim.Interval = 10;
ztim.Start();
ztim.Tick += Ztim_Tick;
}
}
}
}
}
}
private void Ztim_Tick(object sender, EventArgs e)
{
Timer tim = sender as Timer;
PictureBox pic = (PictureBox)tim.Tag;
pic.Image = imageList1.Images[(int)pic.Tag];
pic.Tag = (int)pic.Tag + 1;
if ((int)pic.Tag > 31)
{
pic.Dispose();
tim.Dispose();
}
}
// 鼠标控制飞机移动
private void qing()
{
foreach (PictureBox zd in pictureBoxes)
{
zd.Dispose();
}
}
private void Bg_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Hide();
fly.Left = MousePosition.X - this.Left - bg.Left - fly.Width ;
fly.Top = MousePosition.Y - this.Top - bg.Top - fly.Height;
f1.Location = new Point(fly.Left + fly.Width / 2 - f1.Width / 2, fly.Top + fly.Height);
f2.Location = new Point(fly.Left + fly.Width / 2 - f1.Width / 2-20, fly.Top + fly.Height);
foreach (Control item in bg.Controls)
{
if (item.Tag.ToString()=="diji")
{
if ((item.Left >= fly.Left &&item.Left <= fly.Left + fly.Width &&item.Top >= fly.Top &&item.Top <= fly.Left + fly.Height))
{
item.Dispose();
fly.Dispose();
qing();
zdt.Stop();
crezd.Stop();
f1.Dispose();
f2.Dispose();
}
}
}
}
}
}