分析;
1.地图的背景色
2.字母的随机生成
3.字母的下落
4.键盘事件
5.按键对应的值飞机的移动以及子弹的发射;
6.碰撞之后爆炸的动画
7.红色血条的消失
8.打中几个清屏等等
int count; //分数
Label lb1 = new Label();
Label lb2 = new Label();
Label lb3 = new Label();
// 子弹创建的计时器
Timer zm = new Timer();
// 子弹落下计时器
Timer fly = new Timer();
Panel bg = new Panel();
Random rad = new Random();
bool po = true;
Label lb4 = new Label();
Label lb5 = new Label();
Label lb6 = new Label();
Label lb7 = new Label();
Label lb8 = new Label();
PictureBox player = new PictureBox();
//泛型集合 用来装 label
List<Label> labels = new List<Label>();
private void Form1_Load(object sender, EventArgs e)
{
this.Size = new Size(1500, 800);
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2);
// 创建的地图
bg.BackColor = Color.Pink;
bg.Tag = "bgc";
bg.Size = new Size(1300, 700);
bg.Location = new Point(20, 30);
this.Controls.Add(bg);
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);
// 血条记录
lb4.BackColor = Color.Red;
lb4.Size = new Size(100, 20);
lb4.Location = new Point(10,100);
lb5.BackColor = Color.Wheat;
lb5.Size = new Size(100, 20);
lb5.Location = new Point(10, 100);
// 大招记录
lb6.BackColor = Color.Green;
lb6.Size = new Size(0, 20);
lb6.Location = new Point(10, 160);
lb8.BackColor = Color.White;
lb8.Size = new Size(100, 20);
lb8.Location = new Point(10, 160);
lb7.ForeColor = Color.Black;
lb7.Text = "清屏:";
lb7.Location = new Point(10,130);
lb1.Controls.Add(lb4);
lb1.Controls.Add(lb5);
lb1.Controls.Add(lb6);
lb1.Controls.Add(lb8);
lb1.Controls.Add(lb7);
zm.Tick += Zm_Tick;
zm.Interval = 500;
fly.Tick += Fly_Tick;
fly.Interval = 30;
// 创建的飞机
player.Image = Image.FromFile(@"../../img/RP03.png");
player.Size = new Size(60, 60);
player.Tag = "fei";
player.Location = new Point(bg.Width/2-player.Width/2,bg.Height-player.Height);
player.SizeMode = PictureBoxSizeMode.StretchImage;
bg.Controls.Add(player);
this.KeyPress += Form1_KeyPress;
}
private void Lb3_Click(object sender, EventArgs e)
{
// 暂停和开始游戏的第一种方法
Label lb = (Label)sender;
if (lb.Text=="开始游戏")
{
zm.Start();
fly.Start();
lb.Text = "暂停游戏";
}else if (lb.Text=="暂停游戏")
{
zm.Stop();
fly.Stop();
lb.Text = "开始游戏";
}
/* if (po==true)
{
zm.Start();
fly.Start();
lb3.Text = "暂停游戏";
po = false;
}
else
{
zm.Stop();
fly.Stop();
lb3.Text = "开始游戏";
po = true;
}*/
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
//事件2,e参数,e事件触发者对象
//e.KeyChar
foreach (Control item in bg.Controls)
{
//判断按键值和某个字母值对应
if (item.Text == e.KeyChar.ToString() && item.Tag.ToString() == "zim")
{
// 控制飞机的坐标移动的到字母的下面
item.Tag = "biaoji";
player.Left = item.Left + item.Width/2 - player.Width / 2;
PictureBox zd = new PictureBox();
// 添加图片
zd.Image = Image.FromFile(@"../../img/Ammo1.png");
zd.SizeMode = PictureBoxSizeMode.StretchImage;
zd.Size = new Size(6, 30);
zd.Tag = "zd";
zd.Left = player.Left + player.Width /2- zd.Width / 2;
zd.Top = player.Top - zd.Height;
bg.Controls.Add(zd);
return;
//break;
}
}
}
int xue = 0;
private void Fly_Tick(object sender, EventArgs e)
{
foreach (Control item in bg.Controls)
{
//判断的另一种方法
//if(item.GetType().Name=="Label")
if (item.Tag.ToString()=="zim"||item.Tag.ToString()=="biaoji")
{
item.Top += 10;
if (item.Top+item.Height >= bg.Height)
{
item.Dispose();
lb4.Width -= 5;
// 血条的控制
if (lb4.Width <=0)
{
zm.Stop();
fly.Stop();
if (MessageBox.Show("游戏结束,你的得分:" + count, "Game Over", MessageBoxButtons.RetryCancel) == DialogResult.Retry)
{
// 数据初始化
count = 0;
lb1.Text = "得分:" + count + "分";
lb3.Text = "开始游戏";
lb4.Width = 100;
lb6.Width = 0;
kong();
}
else
{
this.Close();
}
}
}
}
// 判断是否为子弹
if (item.Tag.ToString() == "zd")
{ // 字母落下
item.Top -= 3;
// 小于屏幕
if(item.Top<=0)
{
item.Dispose();
}
foreach(Control zz in bg.Controls)
{
if (zz.Tag.ToString()=="biaoji")
{
if (item.Top <= zz.Top + zz.Height && item.Left + item.Width / 2 == zz.Left + zz.Width / 2)
{
item.Dispose();
zz.Dispose();
xue+=10;
lb6.Width = xue;
if (lb6.Width == 100)
{
xue = 0;
kong();
lb6.Width = xue;
}
//计分板
count++;
lb1.Text = "得分:" + count.ToString() + "分";
// 爆炸效果的图片
PictureBox box = new PictureBox();
box.Size = new Size(50, 50);
box.Tag = 0;
box.Location = new Point(zz.Left+zz.Width/2-box.Width/2,zz.Top+zz.Height/2-box.Height/2);
bg.Controls.Add(box);
box.Image = imageList1.Images[0];
// 爆炸图片的计时器
Timer bom = new Timer();
bom.Interval = 20;
bom.Tag = box;
bom.Tick += Bom_Tick;
bom.Start();
}
}
}
}
}
}
private void Bom_Tick(object sender, EventArgs e)
{
/* // 利用这个sender获取事件的发起者
Timer tm = (Timer)sender;
// 获取设置的tag值
PictureBox pic = (PictureBox)tm.Tag;*/
// 获取timer
Timer tm = (Timer)sender;
// 利用timer的tag=爆炸图片 从而获取爆炸图片
PictureBox pic = (PictureBox)tm.Tag;
pic.Tag = ((int)pic.Tag) + 1;
pic.Image = imageList1.Images[(int)pic.Tag];
if ((int)pic.Tag>=31)
{
pic.Dispose();
tm.Dispose();
}
}
private void Zm_Tick(object sender, EventArgs e)
{
Label lb = new Label();
lb.Tag = "zim";
lb.Text =((char)rad.Next(97, 123)).ToString();
lb.ForeColor = Color.FromArgb(rad.Next(256), rad.Next(256), rad.Next(256));
lb.Font = new Font("",rad.Next(20,30));
lb.Top = 0;
lb.Left = rad.Next(0, bg.Width - lb.Width);
lb.AutoSize = true;
// 背景
lb.BackColor = Color.Transparent;
bg.Controls.Add(lb);
labels.Add(lb);
}
private void kong()
{
foreach (Label ite in labels)
{
ite.Dispose();
}
}
}
}