C#飞机大战案例详细教程

飞机大战游戏开发稳定版

实现效果如下

  • 子弹碰撞敌机
  • 己方碰撞敌机
  • 己方飞机碰撞补给
  • 飞机跟随鼠标移动
  • 爆炸动画
  • 爆炸音效

需要拓展的请自行拓展,欢迎大佬点评!

运行界面截图

游戏截图

运行界面截图
设计截图
实现功能:1.英雄机跟随鼠标移动
2.敌方飞机的随机样式
3.子弹打中敌机,敌机碰到英雄机,吃到补给
4后续功能逐渐完善中Longing…
只实现简单功能,后续功能持续更新
错误肯定有,大家请指正!!!

话不多说上代码!!!!

本项目的全局变量

// 游戏区域
        Panel Game = new Panel();
        // 己方飞机
        PictureBox plane = new PictureBox();
        // 随机数
        Random ran = new Random();
        // 尾翼火
        PictureBox fr1 = new PictureBox();
        // 尾翼火
        PictureBox fr2 = new PictureBox();
        // 补给箱
        Timer f1tim = new Timer();
        // 上升计时器
        Timer timertop = new Timer();
        // 创建敌机计时器
        Timer enem_timer = new Timer();
        // 敌机随机
         int conve = 0;

窗体加载事件

        private void Form1_Load(object sender, EventArgs e)
        {           
            // 窗体位置
            this.Size = new Size(500, 600);
            this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
            this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
            this.BackColor = Color.White;
            // 游戏区域Panel-Game
            Game.Size = new Size(300, 540);
            this.BackColor = Color.Pink;
            Game.BackColor = Color.White;
            Game.Location = new Point(10, 10);
            this.Controls.Add(Game);
            // 添加飞机-plean在游戏区域
            plane.Size = new Size(60, 60);
            plane.Tag = "plean";
            plane.Top = Game.Height - plane.Height;
            plane.Left = Game.Width / 2 - plane.Width / 2;
            plane.BackgroundImage = Image.FromFile(@"../../img/plane_1.png");
            plane.BackgroundImageLayout = ImageLayout.Stretch;
            Game.Controls.Add(plane);
            fr1.Image = imageList2.Images[0];
            fr2.Image = imageList2.Images[0];
            fr1.Tag = 0;
            fr2.Tag = 0;
            fr1.Size = new Size(11,25);
            fr2.Size = new Size(11,25);
            Game.Controls.Add(fr1);
            Game.Controls.Add(fr2);
            f1tim.Interval = 30;
            f1tim.Start();
            f1tim.Tick += F1tim_Tick;
            // 鼠标移动事件
            Game.MouseMove += Game_MouseMove;
            // 上升计时器
            timertop.Interval = 10;
            timertop.Start();
            timertop.Tick += Timertop_Tick;
            // 创建敌机计时器          
            enem_timer.Interval = 1000;
            enem_timer.Start();
            enem_timer.Tick += Em_timer_Tick;
            // 补给箱计时器
            Timer dazhao = new Timer();
            dazhao.Interval = 10000;
            dazhao.Start();
            dazhao.Tick += Dazhao_Tick;
           // 创建子弹
            butttim.Interval = 200;
            butttim.Start();
            butttim.Tick += Butttim_Tick;
        }

创建敌机

        int conve = 0;
	  // 创建敌机
        List<PictureBox> Enemys = new List<PictureBox>();
        private void Em_timer_Tick(object sender, EventArgs e)
        {
            PictureBox enemyimg = new PictureBox();
            enemyimg.Size = new Size(50,50);
            enemyimg.Tag = "enemyimg";
            // 敌机图片随机
            if (conve>=10)
            {
                enemyimg.Image = Image.FromFile(@"../../img/Enemy" + ran.Next(1, 5)+".png");
            }
            else
            {
                enemyimg.Image = Image.FromFile(@"../../img/Enemy3_"+ran.Next(1,4)+".png");
            }           
            enemyimg.SizeMode = PictureBoxSizeMode.StretchImage;
            enemyimg.Location = new Point(ran.Next(Game.Width-enemyimg.Width),0);
            Game.Controls.Add(enemyimg);
            Enemys.Add(enemyimg);         
        }

创建子弹

 // 创建子弹计时器
        bool panduan = false;
        private void Butttim_Tick(object sender, EventArgs e)
        {
            if (panduan)
            {
                doublebull();
            }
            else
            {
             SoundPlayer player = new SoundPlayer(@"../../music/bullet.wav");
             player.Play();
             PictureBox butt = new PictureBox();
             butt.Tag = "zidan";
             butt.Image = Image.FromFile(@"../../img/Ammo1.png");
             butt.Size = new Size(6, 30);
             butt.SizeMode = PictureBoxSizeMode.StretchImage;
             butt.Left = plane.Left + plane.Width / 2 - butt.Width / 2;
             butt.Top = plane.Top - butt.Height;
             Game.Controls.Add(butt);
            }
        }
// 子弹
        private void doublebull()
        {
            SoundPlayer player = new SoundPlayer(@"../../music/bullet.wav");
            player.Play();
            PictureBox butt = new PictureBox();
            PictureBox butt2 = new PictureBox();
            butt.Tag = "zidan";
            butt.Image = Image.FromFile(@"../../img/Ammo1.png");
            butt.Size = new Size(6, 30);
            butt.SizeMode = PictureBoxSizeMode.StretchImage;
            butt.Left = plane.Left + plane.Width / 4 - butt.Width / 4;
            butt.Top = plane.Top - butt.Height;
            butt2.Tag = "zidan";
            butt2.Image = Image.FromFile(@"../../img/Ammo1.png");
            butt2.Size = new Size(6, 30);
            butt2.SizeMode = PictureBoxSizeMode.StretchImage;
            butt2.Left = plane.Left + plane.Width / 2 - butt.Width / 2;
            butt2.Top = plane.Top - butt.Height;
            Game.Controls.Add(butt2);
            Game.Controls.Add(butt);
        }

飞机跟随鼠标移动

 // 飞机跟随鼠标移动
        private void Game_MouseMove(object sender, MouseEventArgs e)
        {
            Cursor.Hide();
            plane.Left = MousePosition.X - this.Left - Game.Left - plane.Width / 2;
            plane.Top = MousePosition.Y - this.Top - Game.Top - plane.Height / 2;
            fr1.Location = new Point(plane.Left + plane.Width / 5, plane.Top + plane.Height);
            fr2.Location = new Point(plane.Left + plane.Width / 2, plane.Top + plane.Height);
        }

控制下落上升计时器(主要逻辑)


       // 子弹上升计时器
        private void Timertop_Tick(object sender, EventArgs e)
        {
            foreach (Control item in Game.Controls)
            {
                // 敌机下落
                if (item.Tag.ToString()=="enemyimg")
                {
                    item.Top += 3;
                    if (item.Top>Game.Height)
                    {
                        item.Dispose();
                    }
                    // 敌机与己方飞机相撞  
                    if (item.Left >= plane.Left && 
                        item.Left <= plane.Left + plane.Width &&
                        item.Top >= plane.Top && 
                        item.Top <= plane.Top + plane.Height)
                    {
                            item.Dispose();
                        plane.Dispose();
                        fr1.Dispose();
                        fr2.Dispose();
                        f1tim.Stop();
                        timertop.Stop();
                        enem_timer.Stop();
                        butttim.Stop();
                        SoundPlayer player = new SoundPlayer(@"../../music/use_bomb.wav");
                        player.Play();
                        baozha(item);
                        MessageBox.Show("Game Over");             
                    }
                }
                // 子弹上升
                if (item.Tag.ToString()=="zidan")
                {
                    item.Top -= 10;
                    if (item.Top<-item.Height)
                    {
                        item.Dispose();                        
                    }
                    // 子弹敌机碰撞
                    foreach (Control enemy in Game.Controls)
                    {
                        if (enemy.Tag.ToString()=="enemyimg")
                        {
                            if (item.Top <= enemy.Top + enemy.Height &&
                            item.Left >= enemy.Left &&
                            item.Left + item.Width <= enemy.Left + enemy.Width)
                            {
                              item.Dispose();
                              enemy.Dispose();
                                SoundPlayer player = new SoundPlayer(@"../../music/enemy0_down.wav");
                                player.Play();
                                conve+=1;
                                baozha(enemy);
                            }                          
                        }
                      
                    } 
                    
                }
                // 补给清屏                
                if (item.Tag.ToString()=="bj")
                {
                    item.Top += 3;                    
                     if(item.Left >= plane.Left &&
                        item.Left <= plane.Left + plane.Width &&
                        item.Top >= plane.Top &&
                        item.Top <= plane.Top + plane.Height)
                     {
                        item.Dispose();
                        SoundPlayer player = new SoundPlayer(@"../../music/get_double_laser.wav");
                        player.Play();
                        qingpingmu();                              
                     }                                        
                }
                // 补给双倍子弹
                if (item.Tag.ToString() == "doubutt")
                {
                    item.Top += 3;
                    if (item.Left >= plane.Left &&
                        item.Left <= plane.Left + plane.Width &&
                        item.Top >= plane.Top &&
                        item.Top <= plane.Top + plane.Height)
                    {
                        item.Dispose();
                        SoundPlayer player = new SoundPlayer(@"../../music/get_double_laser.wav");
                        player.Play();
                        panduan = true;                       
                    }
                }
            }
        }

补给箱子(清屏幕,双倍子弹)

 // 补给箱子

        private void Dazhao_Tick(object sender, EventArgs e)
        {
            int suiji = ran.Next(0,2);
            PictureBox buji = new PictureBox();
            buji.SizeMode = PictureBoxSizeMode.StretchImage;      
            buji.Size = new Size(30,51);
            buji.Location = new Point(ran.Next(Game.Width-buji.Width),-buji.Width);
            Game.Controls.Add(buji);
            if (suiji==0)
            {
                buji.Tag = "bj";
                buji.Image = Image.FromFile(@"../../img/prop_type_1.png");
            }
            else
            {
                buji.Tag = "doubutt";
                buji.Image = Image.FromFile(@"../../img/prop_type_0.png");
            }
        }

爆炸动画计时器

 // 爆炸动画
        private void Bomtim_Tick(object sender, EventArgs e)
        {
            Timer bomtim = sender as Timer;
            PictureBox bomimgs = bomtim.Tag as PictureBox;
            bomimgs.Image = imageList1.Images[(int)bomimgs.Tag];
            bomimgs.Tag = (int)bomimgs.Tag + 1;
            if ((int)bomimgs.Tag>=31)
            {
                bomimgs.Dispose();
                bomtim.Dispose();
            }          
        }

清空屏幕方法

 private void qingpingmu ()
        {

            foreach (PictureBox enemy in Enemys)
            {
                enemy.Dispose();
            }
        }

爆炸图片方法


         // 爆炸图片
        private void baozha(Control control)
        {
            PictureBox bomimgs = new PictureBox();
            bomimgs.Tag = 0;
            bomimgs.Size = new Size(50, 50);
            bomimgs.Location = new Point
           (control.Left + control.Width / 2 - bomimgs.Width / 2,
           control.Top + control.Height / 2 - bomimgs.Height / 2);
            bomimgs.Image = imageList1.Images[0];
            Game.Controls.Add(bomimgs);
            // 创建爆炸图片切换计时器
            Timer bomtim = new Timer();
            bomtim.Tag = bomimgs;
            bomtim.Interval = 20;
            bomtim.Start();
            bomtim.Tick += Bomtim_Tick;
        }
  • 19
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值