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

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

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

设计界面拖控件:
得分控件:lable,
开始/继续游戏图片PictureBox,>小鸟图片PictureBox(默认隐藏)
地面图片:PictureBox
C#小项目飞翔的小鸟游戏详细教程(Flying bird),基于Winform框架_第1张图片
C#小项目飞翔的小鸟游戏详细教程(Flying bird),基于Winform框架_第2张图片
C#小项目飞翔的小鸟游戏详细教程(Flying bird),基于Winform框架_第3张图片

代码奉上,请各位大佬点评

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<PictureBox> piclist = new List<PictureBox>(); // 存储管子的泛型集合
        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 <= gangzi2Right && bird1Top <= gangzi2Bottom)
            {
                // 已经发生碰撞
                return true;
            }
            else
            {
                // 没有碰撞
                return false;
            }

        }
        // 空格按键控制小鸟跳
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar==(char)Keys.Space)
            {
                speed++;
                 Birdimg.Top -= speed;
            }
        }
        // 鼠标单击、小鸟跳
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button==MouseButtons.Left)
            {
                    speed++;
                    Birdimg.Top -= speed;
            }
        }
        // 计时器控制管子移动
        private void timer2_Tick(object sender, EventArgs e)
        {
            foreach (Control item in this.Controls)
            {
                if (item.Tag.ToString()=="gangzi"|| item.Tag.ToString() == "jilu"||item.Tag.ToString()=="gangzi1")
                {
                    // 管子的left小于-管子的宽度,管子出屏幕,管子消失
                    if (item.Left<=-item.Width)
                    {
                        item.Dispose();
                    }
                    // 管子的left小于-管子的宽度/2,加分,变tag以防多加
                    else if (item.Left<=-item.Width/2)
                    {
                        if (item.Tag.ToString() == "gangzi")
                        {
                              score+=1;
                            label1.Text = score.ToString();
                        }
                        item.Tag = "jilu";
                    }
                    item.Left -= 6;
                }
    
            }
        }
        // 创建管子计时器
        private void timer3_Tick(object sender, EventArgs e)
        {
            Guan();

        }
        // 开始游戏/game over界面
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (pictureBox2.Tag.ToString()=="开始游戏")
            {
                pictureBox2.BackgroundImage = Image.FromFile(@"../../img/text_game_over.png");
                pictureBox2.Tag = "游戏结束";
                pictureBox2.Visible = false;
                pictureBox3.Visible = true;
                Birdimg.Visible = true;
                // 鸟移动计时器
                timer1.Interval = 40;
                timer1.Start();
                // 管子移动计时器
                timer2.Interval = 20;
                timer2.Start();
                // 管子创建
                timer3.Interval = 1500;
                timer3.Start();
            }
            else
            {
                score = 0;
                label1.Text = score.ToString();
                pictureBox2.Visible = false;
                pictureBox3.Visible = true;
                Birdimg.Visible = true;
                qingping();
                Birdimg.Location = new Point(23,130);
                this.KeyPress +=Form1_KeyPress;
                this.MouseClick += Form1_MouseClick;
                // 鸟移动计时器
                timer1.Interval = 40;
                timer1.Start();
                // 管子移动计时器
                timer2.Interval = 30;
                timer2.Start();
                // 管子创建
                timer3.Interval = 1500;
                timer3.Start();
            }
        }

        // 暂停开始小按钮
        private void pictureBox3_Click(object sender, EventArgs e)
        {
            if (pictureBox3.Tag.ToString()=="暂停")
            {
                timer1.Stop();
                timer2.Stop();
                timer3.Stop();
                pictureBox3.Tag = "开始";
                pictureBox3.BackgroundImage = Image.FromFile(@"../../img/button_resume.png");
            }
            else
            {
                timer1.Start();
                timer2.Start();
                timer3.Start();
                pictureBox3.Tag = "暂停";
                pictureBox3.BackgroundImage = Image.FromFile(@"../../img/button_pause.png");
            }
        }
        // 清屏幕
        private void qingping()
        {
            foreach (PictureBox item in piclist)
            {
                item.Dispose();
            }
        }
    }
}

你可能感兴趣的:(C#经典游戏开发)