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 bird { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Random r = new Random(); private void Form1_Load(object sender, EventArgs e) { this.FormBorderStyle = FormBorderStyle.FixedSingle; this.Size = new Size(600,1000); this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2,Screen.PrimaryScreen.WorkingArea.Height/2-this.Height/2); this.BackgroundImage = Image.FromFile("../../img/beijing.png"); this.BackgroundImageLayout = ImageLayout.Stretch; tudi.Tag = "b"; tudi.Size = new Size(600,112); tudi.Location = new Point(0,this.Height-tudi.Height); tudi.BackgroundImage = Image.FromFile("../../img/tudi.png"); tudi.BackgroundImageLayout = ImageLayout.Stretch; this.Controls.Add(tudi); bird.Size = new Size(48,48); bird.Tag = "b"; bird.Location = new Point(this.Width/2-bird.Width/2,this.Height/2-bird.Height/2); bird.BackgroundImage = Image.FromFile("../../img/niao1.png"); // bird.BackgroundImageLayout = ImageLayout.Stretch; bird.BackColor = Color.Transparent;//使颜色与背景色融合 this.Controls.Add(bird); //创建鸟上升的timer flay.Interval = 10; // flay.Stop(); flay.Tick += Flay_Tick; //创建鸟下降的timer xa.Interval = 10; xa.Start(); xa.Tick += Xa_Tick; //创建柱子的timer Timer chuang = new Timer(); chuang.Start(); chuang.Interval = 2000; chuang.Tick += Chuang_Tick; //柱子移动的timer 柱子的left固定随机其top的大小 与其相对应的柱子top也要变换 yidon.Interval = 100; yidon.Start(); yidon.Tick += Yidon_Tick; //键盘事件 this.KeyDown += Form1_KeyDown; this.KeyUp += Form1_KeyUp; //判断碰撞timer pen.Interval = 10; pen.Start(); pen.Tick += Pen_Tick; } private void Chuang_Tick(object sender, EventArgs e) { int h = r.Next(300, 445); PictureBox shang = new PictureBox(); shang.Size = new Size(52, h); shang.Tag = "s"; shang.BackgroundImage = Image.FromFile("../../img/guanzi1.png"); shang.BackgroundImageLayout = ImageLayout.Stretch; shang.Location = new Point(this.Width, 0); this.Controls.Add(shang); PictureBox xia = new PictureBox(); xia.Tag = "x"; xia.Size = new Size(52, this.Height - tudi.Height - shang.Height - bird.Height); xia.BackgroundImage = Image.FromFile("../../img/guanzi2.png"); xia.BackgroundImageLayout = ImageLayout.Stretch; xia.Location = new Point(this.Width, this.Height - shang.Height - bird.Height); this.Controls.Add(xia); } private void Pen_Tick(object sender, EventArgs e) { foreach (Control item in this.Controls) { if (item.GetType().Name == "PictureBox") { if (item.Tag.ToString() == "s") { if (bird.Left+bird.Width>=item.Left&&bird.Top<=item.Height) { yidon.Stop(); xa.Stop(); flay.Stop(); pen.Stop(); MessageBox.Show("请重新开始游戏!"); } } } } foreach (Control it in this.Controls) { if (it.GetType().Name == "PictureBox") { if (it.Tag.ToString() == "x") { if (bird.Left + bird.Width >= it.Left && bird.Top+bird.Height >= it.Top/*&&bird.Left+bird.Width>=it.Left+it.Width*/) { yidon.Stop(); xa.Stop(); flay.Stop(); pen.Stop(); MessageBox.Show("请重新开始游戏!"); } } } } } private void Yidon_Tick(object sender, EventArgs e) { foreach (Control item in this.Controls) { if (item.GetType().Name== "PictureBox") { if (item.Tag.ToString()=="s"|| item.Tag.ToString() == "x") { item.Left -= 10; if (item.Left <= -52) { item.Dispose(); } } } } } private void Xa_Tick(object sender, EventArgs e) { flay.Stop(); bird.Top += 3; if (bird.Top+bird.Height>=888) { yidon.Stop(); xa.Stop(); MessageBox.Show("请重新开始游戏!"); } } Timer xa = new Timer(); PictureBox bird = new PictureBox(); Timer flay = new Timer(); PictureBox tudi = new PictureBox(); Timer yidon = new Timer(); Timer pen = new Timer(); private void Flay_Tick(object sender, EventArgs e) { bird.Top -= 3; } private void Form1_KeyDown(object sender, KeyEventArgs e) { xa.Stop(); flay.Start(); } private void Form1_KeyUp(object sender, KeyEventArgs e) { flay.Stop(); xa.Start(); } } }