大体思路:c#
建立游戏区 :设置所需属性dom
建立字母生成:随机大小和字母动画
字母下落this
建立飞机:添加事件,飞机跟随字母移动spa
建立子弹:子弹上升,碰到字母消失code
其他小细节本身须要处理orm
注意:为了避免使项目那么突兀,加了一个小的移动动画:blog
字母跟随鸟的移动建立字母游戏
代码以下:事件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 金山打字 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Panel lb = new Panel(); Random r = new Random(); //字母生成 Timer zm = new Timer(); //字母下落 Timer xq = new Timer(); //动画移动 Timer move = new Timer(); //建立鸟盒子 PictureBox bird = new PictureBox(); //建立飞机 PictureBox plan = new PictureBox(); //建立动画计时器 Timer fly = new Timer(); //建立的得分的label Label df = new Label(); int x = 0; //实例化血条 Label xt = new Label(); Label xt1 = new Label(); int dl = 0; private void Form1_Load(object sender, EventArgs e) { this.WindowState = FormWindowState.Maximized; this.Text = "金山打字"; this.BackgroundImage = Image.FromFile(@"../../img/2.jpg"); this.BackgroundImageLayout = ImageLayout.Stretch; //游戏区域 lb.Width = 1300; lb.Height = 780; lb.BackColor = Color.Brown; this.Controls.Add(lb); //开始游戏 Label ks = new Label(); ks.Size = new Size(120, 20); ks.BackColor = Color.White; ks.Location = new Point(1380, 100); ks.Tag = "ksyx"; ks.Text = "开始游戏"; ks.Font = new Font("", 15); ks.AutoSize = true; this.Controls.Add(ks); ks.Click += Ks_Click; //建立暂停按钮 Label zt = new Label(); zt.Size = new Size(120, 20); zt.BackColor = Color.White; zt.Location = new Point(1380, 150); zt.Tag = "ztyx"; zt.Text = "暂停游戏"; zt.Font = new Font("", 15); zt.AutoSize = true; this.Controls.Add(zt); zt.Click += Zt_Click; //得分栏 df.Size = new Size(130, 20); df.Location = new Point(1380, 210); df.Tag = "dfl"; df.Text = "得分:0分"; df.Font = new Font("", 15); df.AutoSize = true; this.Controls.Add(df); //血条 xt.Size = new Size(130, 20); xt.Location = new Point(1380, 280); xt.BackColor = Color.White; xt1.Size = new Size(130, 20); xt1.Location = new Point(1380, 280); xt1.BackColor = Color.Red; xt.Tag = "xt"; xt.Tag = "xt1"; this.Controls.Add(xt); this.Controls.Add(xt1); //字母计时器 zm.Interval = 700; zm.Tick += Zm_Tick; //动画图片 bird.Tag = "b"; bird.Size = new Size(140, 120); bird.Location = new Point(0, 0); bird.Image = imageList1.Images[0]; lb.Controls.Add(bird); //动画事件 fly.Interval = 80; fly.Tick += Fly_Tick; //动画移动 move.Interval = 20; move.Tick += Move_Tick; //字母下落控制 xq.Interval = 20; xq.Tick += Xq_Tick; //飞机 plan.Tag = "plan"; plan.Size = new Size(80,80); plan.Location = new Point(lb.Width / 2 - plan.Width / 2, lb.Height - plan.Height); plan.Image = Image.FromFile(@"../../img/YP03.png"); plan.SizeMode = PictureBoxSizeMode.StretchImage; lb.Controls.Add(plan); } //暂停事件 private void Zt_Click(object sender, EventArgs e) { zm.Stop(); fly.Stop(); xq.Stop(); move.Stop(); } //开始事件 private void Ks_Click(object sender, EventArgs e) { zm.Start(); fly.Start(); xq.Start(); move.Start(); } int xuetiao = 130; //下落 private void Xq_Tick(object sender, EventArgs e) { foreach (Control item in lb.Controls) { if (item.GetType().Name== "Label") { item.Top += 3; } if (item.Top>=lb.Height) { item.Dispose(); xuetiao -= 10; xt.Width = xuetiao; dl++; if (xt.Width==0) { zm.Stop(); fly.Stop(); xq.Stop(); move.Stop(); MessageBox.Show("Game over!!"); } } //子弹上升 if (item.GetType().Name == "PictureBox") { if (item.Tag.ToString() == "zd") { item.Top -= 3; foreach (Control bz in lb.Controls) { if (bz.Tag.ToString() == "bj") { if (item.Top <= bz.Top + bz.Height && item.Left + item.Width / 2 == bz.Width / 2 + bz.Left) { bz.Dispose(); item.Dispose(); x += 10; df.Text = x.ToString() + "分"; PictureBox xg = new PictureBox(); xg.Size = new Size(50, 50); xg.Location = new Point(item.Left + item.Width / 2 - xg.Width / 2, item.Top + item.Height - xg.Height); xg.Tag = 0; xg.Image = imageList2.Images[0]; lb.Controls.Add(xg); Timer bofang = new Timer(); bofang.Interval = 70; bofang.Tag = xg; bofang.Tick += Bofang_Tick; bofang.Start(); } } } } } } } private void Bofang_Tick(object sender, EventArgs e) { Timer bf =(Timer)sender; PictureBox pictureBox = (PictureBox)bf.Tag; pictureBox.Image = imageList2.Images[(int)pictureBox.Tag]; pictureBox.Tag = (int)pictureBox.Tag + 1; if ((int)pictureBox.Tag>=32) { bf.Dispose(); pictureBox.Dispose(); } } //动画移动事件 private void Move_Tick(object sender, EventArgs e) { bird.Left += 3; if (bird.Left>lb.Width) { bird.Left = -bird.Width; } } //动画事件 int index=0; private void Fly_Tick(object sender, EventArgs e) { index++; bird.Image = imageList1.Images[index]; if (index>=10) { index = -1; } } //字母下落控制 private void Zm_Tick(object sender, EventArgs e) { if (bird.Left>=0&&bird.Left<=lb.Width-bird.Width) { Label xl = new Label(); xl.Text = ((char)r.Next(97, 123)).ToString(); xl.Font = new Font("", r.Next(15, 30)); xl.Tag = "xl"; xl.AutoSize = true; xl.ForeColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)); xl.Top = bird.Height; xl.Left = bird.Left + bird.Width / 2 - xl.Width / 2; lb.Controls.Add(xl); } } private void Form1_KeyPress(object sender, KeyPressEventArgs e) { foreach (Control item in lb.Controls) { if (item.GetType().Name== "Label") { if (item.Text==e.KeyChar.ToString()&&item.Tag.ToString()=="xl") { plan.Left = item.Left + item.Width / 2 - plan.Width / 2; item.Tag = "bj"; //建立子弹 PictureBox bullet = new PictureBox(); bullet.Tag = "zd"; bullet.Size = new Size(8, 30); bullet.Image = Image.FromFile(@"../../img/zd.png"); bullet.Location = new Point(plan.Left + plan.Width / 2 - bullet.Width / 2, plan.Top - bullet.Height); bullet.SizeMode = PictureBoxSizeMode.StretchImage; lb.Controls.Add(bullet); return; } } } } } }