C#小项目飞翔的小鸟游戏详细教程(Flying bird),基于Winform框架

您所在的位置:网站首页 游戏flying C#小项目飞翔的小鸟游戏详细教程(Flying bird),基于Winform框架

C#小项目飞翔的小鸟游戏详细教程(Flying bird),基于Winform框架

2024-07-15 04:52| 来源: 网络整理| 查看: 265

C#小项目飞翔的小鸟游戏详细教程(Flying bird),基于Winform框架

实现效果: 1.空格,鼠标左键控制小鸟跳 2.管道随机大小 3.小鸟与管道碰撞,小鸟碰到地面 4.小鸟煽动翅膀动画 5.开始暂停游戏 6.过一个管道得分增加

设计界面拖控件: 得分控件:lable, 开始/继续游戏图片PictureBox,>小鸟图片PictureBox(默认隐藏) 地面图片:PictureBox 游戏过程中界面 游戏结束界面 视图界面

代码奉上,请各位大佬点评 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; using System.Threading; namespace 小鸟撞杠子 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// /// 本项目的全局变量 /// /// int gap = 150; // 两根管子的间隔 List piclist = new List(); // 存储管子的泛型集合 Random ran = new Random(); // 随机数类 int i = 0; // 小鸟动画切换图片 int speed = 15; // 小鸟跳一次的高度 int score = 0; // 得分 // 窗体加载事件 private void Form1_Load(object sender, EventArgs e) { // 窗体固定单边框, this.FormBorderStyle = FormBorderStyle.FixedSingle; } private void timer1_Tick(object sender, EventArgs e) { // 小鸟掉地上检测 // 小鸟的top+小鸟的height大于等于地面的top if (Birdimg.Top+Birdimg.Height>=pictureBox1.Top) { timer1.Stop(); timer2.Stop(); timer3.Stop(); // 解绑键盘鼠标事件 this.KeyPress -= Form1_KeyPress; this.MouseClick -= Form1_MouseClick; // 显示结束动画 pictureBox2.Visible = true; } // 小鸟动画播放 i++; if (i>2) { i = 0; } Birdimg.BackgroundImage = Image.FromFile(@"../../img/bird0_" + i+".png"); // 小鸟移动 Birdimg.Left += 1; Birdimg.Top += 3; // 检测碰撞 foreach (Control item in this.Controls) { if (item.Tag.ToString()=="gangzi"||item.Tag.ToString()=="gangzi1") { // 调用碰撞方法传入实参(小鸟与杠子) bool iss = Penggangzi(Birdimg,item); if (iss==true) { timer1.Stop(); timer2.Stop(); timer3.Stop(); this.KeyPress -= Form1_KeyPress; this.MouseClick -= Form1_MouseClick; pictureBox2.Visible = true; MessageBox.Show("点击GAME OVER按钮继续游戏!!!"); } } } } // 管子生成方法 private void Guan() { int hei = ran.Next(80, 200); // 上方的管子 PictureBox pipe_down = new PictureBox(); pipe_down.Tag = "gangzi"; pipe_down.BackgroundImage = Image.FromFile(@"../../img/pipe_down.png"); pipe_down.Location = new Point(this.Width, 0); pipe_down.Size = new Size(60, hei); pipe_down.BackgroundImageLayout = ImageLayout.Stretch; piclist.Add(pipe_down); this.Controls.Add(pipe_down); // 下方的管子 PictureBox pipe_up = new PictureBox(); pipe_up.Tag = "gangzi1"; pipe_up.BackgroundImage = Image.FromFile(@"../../img/pipe_up.png"); pipe_up.Size = new Size(60, this.Height - hei - gap-pictureBox1.Height); pipe_up.Location = new Point(this.Width, this.Height - pipe_up.Height-pictureBox1.Height); pipe_up.BackgroundImageLayout = ImageLayout.Stretch; this.Controls.Add(pipe_up); piclist.Add(pipe_up); } // 碰撞检测方法 public bool Penggangzi(Control bird,Control gangzi) { int bird1Left = bird.Left; int bird1Right = bird1Left + bird.Width; int bird1Top = bird.Top; int bird1Bottom = bird1Top + bird.Height; int gangzi2Left = gangzi.Left; int gangzi2Right = gangzi2Left + gangzi.Width; int gangzi2Top = gangzi.Top; int gangzi2Bottom = gangzi2Top + gangzi.Height; // 小鸟的右边 大于等于 杠子的左边 // 小鸟的下边 大于等于 杠子的上边 // 小鸟的左边 小于等于 杠子的右边 // 小鸟的上边 小于等于 杠子的下边 if (bird1Right >= gangzi2Left && bird1Bottom >= gangzi2Top && bird1Left


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3