一.编程思路
1.简单的界面实现;
2.字母的生成,以及位置的确定;
3.字母的自动下落;
4.飞机的生成以及位置的确定;
5.子弹的生成;
6.子弹与字母位置相同时,字母消失;
7.当字母消失时出现爆炸效果,以及爆炸音效;
二.编程重点
1.控制字母的生成,以及自动下落;
2.飞机位置的确定;
3.子弹的生成以及位置的确定;
4.爆炸效果以及爆炸音效;
三.代码实现
1.界面的布局的划分,主要为游戏区域和控制区域;
Timer timer1 = new Timer();
Timer timer2 = new Timer();
Panel panel1 = new Panel();
PictureBox picture = new PictureBox();
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
this.Text = "不会打字的小强";
panel1.Size = new Size(700, 430);
panel1.BorderStyle = BorderStyle.Fixed3D;
panel1.Location = new Point(10,10);
this.Controls.Add(panel1);
timer1.Tick += Timer1_Tick1;
timer1.Interval = 1000;
timer2.Tick += Timer2_Tick1;
timer2.Interval = 100;
}
2.控制生成字母;
Random p = new Random();
private void Timer1_Tick1(object sender, EventArgs e)
{
Label zimu = new Label();
zimu.Text = ((char)p.Next(97, 123)).ToString();
zimu.Font = new Font("宋体", p.Next(25, 35));
zimu.ForeColor = Color.FromArgb(p.Next(255), p.Next(255), p.Next(255));
zimu.Left = p.Next(panel1.Width - zimu.Width);
zimu.AutoSize = true;
zimu.Tag = "zimu";
panel1.Controls.Add(zimu);
}
3.控制生成"飞机"(初学可以写在窗体加载事件中);
picture.Size = new Size(100, 100);
picture.BackgroundImage = Image.FromFile("../../image/BP03.png");
picture.BackgroundImageLayout = ImageLayout.Stretch;
picture.Left = panel1.Width / 2 - picture.Width / 2;
picture.Top = panel1.Height - picture.Height;
picture.Tag = "feiji";
panel1.Controls.Add(picture);
4.实现字母的下落;
private void Timer2_Tick1(object sender, EventArgs e)
{
foreach (Control item in panel1.Controls)
{
if (item.Tag.ToString() == "zimu" || item.Tag.ToString() == "anxia")
{
item.Top += 8;
}
}
}
5.实现键盘点击对应的字母按键飞机进行移动;
这里需要键盘点击事件,我们用代码一般可以在加载时输入:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
foreach (Control item in panel1.Controls)
{
if (item.Text == e.KeyChar.ToString() && item.Tag.ToString() == "zimu")
{
picture.Left = item.Left + item.Width / 2 - picture.Width / 2;
}
}
}
6.实现子弹的生成,并确定位置;
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
foreach (Control item in panel1.Controls)
{
if (item.Text == e.KeyChar.ToString() && item.Tag.ToString() == "zimu")
{
item.Tag = "anxia";
picture.Left = item.Left + item.Width / 2 - picture.Width / 2;
PictureBox zd = new PictureBox();
zd.Size = new Size(20,20);
zd.Left = picture.Left + picture.Width / 2 - zd.Width / 2;
zd.BackgroundImage = Image.FromFile("../../image/01.jpg");
zd.BackgroundImageLayout = ImageLayout.Stretch;
zd.Top = picture.Top-50;
zd.Tag = "zidan";
panel1.Controls.Add(zd);
return;
}
}
}
7.子弹与字母相碰撞释放字母资源;
private void Timer2_Tick1(object sender, EventArgs e)
{
foreach (Control item in panel1.Controls)
{
if (item.Tag.ToString() == "zimu" || item.Tag.ToString() == "anxia")
{
item.Top += 8;
if (item.Top >= panel1.Height)
{
item.Dispose();
}
}
if (item.Tag.ToString() == "zidan")
{
item.Top -= 10;
foreach (Control zimu in panel1.Controls)
{
if (zimu.Tag.ToString() == "anxia")
{
if (item.Top <= zimu.Top + zimu.Height && item.Left + item.Width / 2 == zimu.Left + zimu.Width / 2)
{
item.Dispose();
zimu.Dispose();
}
}
}
}
}
8.爆炸音效的设置;
if (item.Top <= zimu.Top + zimu.Height && item.Left + item.Width / 2 == zimu.Left + zimu.Width / 2)
{
string bz = "../../music/1.wav";
SoundPlayer baozha = new SoundPlayer(bz);
baozha.Play();
item.Dispose();
zimu.Dispose();
}
9.爆炸效果的实现;
{
PictureBox bomd = new PictureBox();
bomd.Tag = "bomd";
bomd.Size = new Size(20, 20);
bomd.Image = imageList1.Images[0];
bomd.Tag = 0;
bomd.Left = zimu.Left + zimu.Width / 2 - bomd.Width / 2;
bomd.Top = zimu.Top + zimu.Height / 2 - bomd.Height / 2;
panel1.Controls.Add(bomd);
Timer bom = new Timer();
bom.Start();
bom.Interval = 50;
bom.Tick += Bom_Tick;
bom.Tag = bomd;
}
private void Bom_Tick(object sender, EventArgs e)
{
Timer bom = (Timer)sender;
PictureBox photo=(PictureBox)bom.Tag;
photo.Image = imageList1.Images[(int)photo.Tag];
photo.Tag = (int)photo.Tag + 1;
if ((int)photo.Tag>31)
{
bom.Dispose();
photo.Dispose();
}
}
四.效果图