基于C# WinForms窗体——飞机大战

原文:基于C# WinForms窗体——飞机大战

基于C# WinForms窗体——飞机大战基于C# WinForms窗体——飞机大战基于C# WinForms窗体——飞机大战

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.Media; // 添加音乐需引入此命名空间
using System.Reflection;
namespace 飞机大战__
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Random ran = new Random(); // 创建随机对象
        PictureBox youxiqtupian = new PictureBox(); // 创建游戏开始图片
        Label youxiqu = new Label();// 创建游戏区对象
        Label kaiguanqu = new Label();// 创建开关区对象
        PictureBox player = new PictureBox();// 创建飞机对象
        PictureBox weiqi1 = new PictureBox();// 尾气对象1
        PictureBox weiqi2 = new PictureBox(); // 尾气对象2
        Timer dijiTimer = new Timer(); // 敌机生成定时器
        Timer xiaLuoTimer = new Timer();// 敌机下落定时器
        Timer zidanTimer = new Timer(); // 子弹生成定时器
        Timer xiangShangTimer = new Timer(); // 子弹向上定时器
        SoundPlayer baozhasound = new SoundPlayer(); // 爆炸音乐对象
        SoundPlayer jiemiansound = new SoundPlayer(); // 游戏界面音乐对象
        int num = 1; // 敌机随机初始飞机名
        int num1 = 10; // 飞机移动的数,子弹移动的数
        int feng = 0;//分数存储器

        Label kaiguan = new Label(); //开关对象
        Label defeng = new Label();// 得分对象
        Label xuetiao = new Label();//血条对象
        List<PictureBox> zdList = new List<PictureBox>();// 存储生成的子弹
        List<PictureBox> feijiList = new List<PictureBox>();// 存储飞机和尾气
        List<PictureBox> dijiList = new List<PictureBox>();// 存储生成敌机
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Size = new Size(1080, 700);
            this.Text = "飞机大战";
            this.BackColor = Color.FromArgb(80,00,80);
            this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2-this.Width/2, Screen.PrimaryScreen.WorkingArea.Height / 2-this.Height/2);
            // 创建游戏区图片,同样的位置
            youxiqtupian.Size = new Size(800, 600);
            youxiqtupian.Tag = "jiemianTu";
            youxiqtupian.Image = Image.FromFile(@"../../img/youxikaishi.bmp");
            youxiqtupian.SizeMode = PictureBoxSizeMode.StretchImage;
            youxiqtupian.Location = new Point(20, 20);
            this.Controls.Add(youxiqtupian);
            // 创建游戏区
            youxiqu.Size = new Size(800, 600);
            youxiqu.BackColor = Color.White;
            youxiqu.Location = new Point(20, 20);
            this.Controls.Add(youxiqu);  
            // 创建开关区
            kaiguanqu.Size = new Size(200, 600);
            kaiguanqu.BackColor = Color.FromArgb(180, 15, 123);
            kaiguanqu.Location = new Point(840, 20);
            this.Controls.Add(kaiguanqu);
            // 创建飞机对象
            playerChuan();// 调用创建方法
            youxiqu.Controls.Add(player);
            feijiList.Add(player);
            //敌机生成定时器
            dijiTimer.Interval = 1500;
            dijiTimer.Tick += DijiTimer_Tick;
            // 敌机下落定时器
            xiaLuoTimer.Interval = 20;
            xiaLuoTimer.Tick += XiaLuoTimer_Tick;
            // 子弹生成定时器
            zidanTimer.Interval = 150;
            zidanTimer.Tick += ZidanTimer_Tick;
            // 子弹向上移动
            xiangShangTimer.Interval = 10;
            xiangShangTimer.Tick += XiangShangTimer_Tick;
            baozhasound.SoundLocation = @"../../music/game_over.wav";// 爆炸音乐路径
            jiemiansound.SoundLocation = @"../../music/game_music.wav";//界面音乐路径
            jiemiansound.PlayLooping(); //游戏界面音乐开启
            // 创建飞机尾气 两个
            // 尾气对象1
            weiqi1.Size = new Size(15,30);
            weiqi1.Tag = 0;
            weiqi1.Location =new Point(player.Left + player.Width / 2 - weiqi1.Width / 2 - 10,player.Top + player.Height + weiqi1.Height / 2 - 15);
            weiqi1.Image = imageList1.Images[0];
            weiqi1.SizeMode = PictureBoxSizeMode.StretchImage;
            youxiqu.Controls.Add(weiqi1);
            feijiList.Add(weiqi1);
            // 尾气对象2
            weiqi2.Size = new Size(15, 30);
            weiqi2.Tag = 0;
            weiqi2.Location = new Point(player.Left + player.Width / 2 + weiqi2.Width / 2 - 5, player.Top + player.Height + weiqi2.Height / 2 - 15);
            weiqi2.Image = imageList1.Images[0];
            weiqi2.SizeMode = PictureBoxSizeMode.StretchImage;
            youxiqu.Controls.Add(weiqi2);
            feijiList.Add(weiqi2);
            //创建尾气定时器
            Timer weiqiTimer = new Timer();
            weiqiTimer.Interval = 10;
            weiqiTimer.Tick += WeiqiTimer_Tick;
            weiqiTimer.Start();
            //创建开始游戏,暂停游戏按钮
            kaiguan.Size = new Size(150, 30);
            kaiguan.Location = new Point(30, 20);
            kaiguan.AutoSize = true;
            kaiguan.Cursor =Cursors.Hand; //鼠标变为手型
            kaiguan.Text = "开始游戏";
            kaiguan.Font = new Font("楷体",18);
            kaiguan.ForeColor = Color.FromArgb(97, 177, 48);
            kaiguan.BackColor = Color.FromArgb(191, 143, 243);
            kaiguan.Click += Kaiguan_Click1;
            kaiguanqu.Controls.Add(kaiguan);
            // 创建得分对象
            defeng.Size = new Size(150, 30);
            defeng.Location = new Point(30, 60);
            defeng.Text = "得分:" +feng + "分";
            defeng.Font = new Font("宋体", 20);
            defeng.ForeColor = Color.Yellow;
            kaiguanqu.Controls.Add(defeng);
            // 血量字体
            Label xlZi = new Label();
            xlZi.Size = new Size(150, 30);
            xlZi.Location = new Point(30, 90);
            xlZi.Text = "飞机血量";
            xlZi.Font = new Font("楷体", 20);
            xlZi.ForeColor = Color.Red;
            kaiguanqu.Controls.Add(xlZi);
            // 创建血条对象
            xuetiao.Size = new Size(160, 15);
            xuetiao.Location = new Point(30, 120);
            xuetiao.BackColor = Color.Red;
            kaiguanqu.Controls.Add(xuetiao);
            //创建血条底部对象
            Label xueTiaodi = new Label();
            xueTiaodi.Size = new Size(160,15);
            xueTiaodi.Location = new Point(30, 120);
            xueTiaodi.BackColor = Color.White;
            kaiguanqu.Controls.Add(xueTiaodi);
            /*// 在游戏区添加鼠标移动事件
            youxiqu.MouseMove += Form1_MouseMove;*/
            // 键盘事件
            this.KeyDown += Form1_KeyDown;

        }
        //游戏开关,暂停点击
        private void Kaiguan_Click1(object sender, EventArgs e)
        {
            if (kaiguan.Text == "开始游戏")
            {
                jiemiansound.Stop(); //游戏界面音乐关闭
                youxiqtupian.Dispose(); //游戏开始图片消失
                dijiTimer.Start(); //敌机生成
                xiaLuoTimer.Start();//敌机下落
                zidanTimer.Start();// 子弹生成
                xiangShangTimer.Start(); // 子弹上升
                kaiguan.BackColor = Color.Red;
                kaiguan.ForeColor = Color.White;

                kaiguan.Text = "暂停游戏";
            }
            else if (kaiguan.Text == "暂停游戏")
            {
                dijiTimer.Stop(); //敌机暂停生成
                xiaLuoTimer.Stop();//敌机暂停下落
                zidanTimer.Stop();// 子弹暂停生成
                xiangShangTimer.Stop(); // 子弹暂停上升
                ziDqing(); //子弹清除
                kaiguan.BackColor = Color.FromArgb(191, 143, 243);
                kaiguan.ForeColor = Color.FromArgb(97, 177, 48);
                kaiguan.Text = "开始游戏";
            }
        }

        // 尾气动画
        int weiQi1 = 0,weiQi2 = 0; // 存储图片数
        private void WeiqiTimer_Tick(object sender, EventArgs e)
        {   // 尾气1添加图片
            weiqi1.Image = imageList1.Images[weiQi1];
            weiQi1++;
            if (weiQi1>=imageList1.Images.Count)
            {
                weiQi1 = 0;
            }
            // 尾气2添加图片
            weiqi2.Image = imageList1.Images[weiQi2];
            weiQi2++;
            if (weiQi2 >=imageList1.Images.Count)
            {
                weiQi2 = 0;
            }
        }
        /*//游戏区鼠标移动事件
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
           player.Left = MousePosition.X - this.Left - youxiqu.Left - player.Width / 2;
           player.Top = MousePosition.Y - this.Top - youxiqu.Top - player.Height / 2;
        }*/

        //敌机生成的定时器
        private void DijiTimer_Tick(object sender, EventArgs e)
        {
            //创建敌机对象
            PictureBox diji = new PictureBox(); // 创建敌机对象  
            diji.Size = new Size(70, 60);
            diji.Tag = "diji";
            num = ran.Next(1, 5);
            diji.Image = Image.FromFile(@"../../img/diji/Enemy" + num + ".png");
            if (num==4)
            {
                diji.Size =new Size(100, 80);
            }
            diji.SizeMode = PictureBoxSizeMode.StretchImage;// 图片自适应大小
            diji.Location = new Point(ran.Next(youxiqu.Width - diji.Width), 0); //敌机初始位置
            youxiqu.Controls.Add(diji);
            dijiList.Add(diji);
        }
        // 敌机下落的定时器
        private void XiaLuoTimer_Tick(object sender, EventArgs e)
        {
            foreach (Control diji in youxiqu.Controls)
            {
                if (diji.Tag.ToString()=="diji")
                {
                    diji.Top += 5;
                    if (diji.Top>youxiqu.Height) // 敌机超过游戏区高度
                    {
                        diji.Dispose(); //敌机释放(删除)
                        xuetiao.Width -= 20;
                        if (xuetiao.Width<=0)
                        {
                            diJiqing();// 敌机清除
                            dijiTimer.Stop();// 敌机创建关闭
                            ziDqing();//子弹清除
                            zidanTimer.Stop();// 子弹创建关闭
                            //创建弹框
                            if ((MessageBox.Show("游戏结束,你的得分:" + feng, "Game Over", MessageBoxButtons.RetryCancel) == DialogResult.Retry))
                            {  
                                feng = 0;
                                defeng.Text = "得分" + feng + "分";
                                xuetiao.Width = 160;
                                kaiguan.Text = "游戏开始";
                                dijiTimer.Start(); //敌机生成
                                xiaLuoTimer.Start();//敌机下落
                                zidanTimer.Start();// 子弹生成
                                xiangShangTimer.Start(); // 子弹上升
                                playerChuan();// 飞机创建
                                weiqi1.Location = new Point(player.Left + player.Width / 2 - weiqi1.Width / 2 - 10, player.Top + player.Height + weiqi1.Height / 2 - 15);
                                weiqi2.Location = new Point(player.Left + player.Width / 2 + weiqi2.Width / 2 - 5, player.Top + player.Height + weiqi2.Height / 2 - 15);
                                ziDqing(); // 子弹清除
                                diJiqing(); //敌机清除
                            }
                            else
                            {
                                this.Close();
                            }
                        }
                    }
                    foreach (Control player in youxiqu.Controls)
                    {
                        if (player.Tag.ToString() == "player")
                        {   //敌机碰到飞机
                            if (diji.Left >= player.Left && diji.Left <= player.Left + player.Width && diji.Top >= player.Top && diji.Top <= player.Top + player.Height)
                            {
                                diJiqing();// 调用清除敌机方法
                                ziDqing(); // 调用子弹清除方法
                                zidanTimer.Stop();// 子弹暂停生成
                                xiangShangTimer.Stop(); // 子弹暂停上升
                                dijiTimer.Stop();// 敌机生成清除
                                kaiguan.Text = "游戏开始";
                                DialogResult jieshukuang = MessageBox.Show("总得分为:" + feng + "分," + "是否重新开始", "游戏结束", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information);
                                if (jieshukuang == DialogResult.Retry) // 点击确定按钮
                                {
                                    feng = 0;
                                    defeng.Text = "得分" + feng + "分";
                                    xuetiao.Width = 160;
                                    kaiguan.Text = "游戏开始";
                                    dijiTimer.Start(); //敌机生成
                                    xiaLuoTimer.Start();//敌机下落
                                    zidanTimer.Start();// 子弹生成
                                    xiangShangTimer.Start(); // 子弹上升
                                    playerChuan();// 飞机创建
                                    weiqi1.Location = new Point(player.Left + player.Width / 2 - weiqi1.Width / 2 - 10, player.Top + player.Height + weiqi1.Height / 2 - 15);
                                    weiqi2.Location = new Point(player.Left + player.Width / 2 + weiqi2.Width / 2 - 5, player.Top + player.Height + weiqi2.Height / 2 - 15);
                                    ziDqing(); // 子弹清除
                                    diJiqing(); //敌机清除

                                }
                                else if (jieshukuang == DialogResult.Cancel)//点击取消按钮
                                {
                                    this.Close();// 关闭窗体
                                }
                            }
                        }
                    }
                }
            }
        }
        // 子弹生成定时器
        private void ZidanTimer_Tick(object sender, EventArgs e)
        {
            PictureBox zidan = new PictureBox();// 创建子弹
            zidan.Tag = "zd";
            zidan.Size = new Size(8, 30);
            zidan.Left = player.Left +player.Width / 2 - zidan.Width / 2;
            zidan.Top = player.Top - zidan.Height;
            zidan.Image = Image.FromFile(@"../../img/Ammo1.png");
            zidan.SizeMode = PictureBoxSizeMode.StretchImage; // 图片自适应大小
            youxiqu.Controls.Add(zidan);
            zdList.Add(zidan); //子弹添加到子弹泛型对象中
        }

        // 子弹向上
        private void XiangShangTimer_Tick(object sender, EventArgs e)
        {
            foreach (Control zidan  in youxiqu.Controls)
            {
                if (zidan.Tag.ToString()=="zd") // 获取子弹
                {
                    zidan.Top -= num1;
                    if (zidan.Top<0) // 子弹超过游戏区
                    {
                        zidan.Dispose();
                    }
                    foreach (Control diji in youxiqu.Controls)
                    { 
                        if (diji.Tag.ToString() == "diji")  // 获取敌机
                        {
                            // 子弹碰撞敌机
                            if (zidan.Top >= diji.Top && zidan.Top <= diji.Top + diji.Height && zidan.Left>=diji.Left && zidan.Left<=diji.Left+diji.Width)
                            {
                                zidan.Dispose(); // 子弹清除
                                diji.Dispose(); // 敌机清除
                                baozhasound.Play(); // 开启爆炸声
                                if (diji.Width==100)
                                {
                                    feng += 10;
                                }
                                else
                                {
                                    feng += 1;
                                }
                                feng++;
                                defeng.Text = "得分:" + feng + "分";
                                // 创建爆炸对象
                                PictureBox baozha = new PictureBox();
                                baozha.Tag = 0;
                                baozha.Size = new Size(50, 50);
                                baozha.Image = imageList2.Images[0];
                                baozha.SizeMode = PictureBoxSizeMode.StretchImage; // 图片自适应大小
                                baozha.Location = new Point(diji.Left + diji.Width / 2 - baozha.Width / 2, diji.Top - diji.Height / 2 + baozha.Height / 2);
                                youxiqu.Controls.Add(baozha);
                                // 爆炸定时器
                                Timer baozhaTimer = new Timer();
                                baozhaTimer.Tag = baozha;
                                baozhaTimer.Interval = 10;
                                baozhaTimer.Tick += BaozhaTimer_Tick; ;
                                baozhaTimer.Start(); // 爆炸开启
                            }
                        }
                    }
                }
            }
        }
        // 爆炸动画
        private void BaozhaTimer_Tick(object sender, EventArgs e)
        {
            Timer baozhaTimer = sender as Timer;
            PictureBox baoZha = baozhaTimer.Tag as PictureBox;
            baoZha.Image = imageList2.Images[(int)baoZha.Tag];
            baoZha.Tag = (int)baoZha.Tag + 1;
            if ((int)baoZha.Tag>31)
            {
                baozhaTimer.Dispose();
                baoZha.Dispose();
            }
        }
        //键盘事件
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //飞机向左
            if (e.KeyCode == Keys.A || e.KeyCode == Keys.Left)
            {
                player.Left -= num1;
                weiqi1.Left -= num1; // 尾气1向左
                weiqi2.Left -= num1; // 尾气2向左  
                if (player.Left == 0)
                {
                    player.Left = num1;
                    weiqi1.Left = player.Left + player.Width / 2 - weiqi1.Width / 2 - 10;
                    weiqi2.Left = player.Left + player.Width / 2 + weiqi2.Width / 2 - 5;
                }
            }
            // 飞机向右
            if (e.KeyCode==Keys.D || e.KeyCode==Keys.Right)
            {
                player.Left += num1;
                weiqi1.Left += num1;
                weiqi2.Left += num1;
                if (player.Left + player.Width >= youxiqu.Width)
                {
                    player.Left = youxiqu.Width - player.Width;
                    weiqi1.Left = player.Left + player.Width / 2 - weiqi1.Width / 2 - 10;
                    weiqi2.Left = player.Left + player.Width / 2 + weiqi2.Width / 2 - 5;
                }
            }
            // 飞机向上
            if (e.KeyCode==Keys.W|| e.KeyCode==Keys.Up)
            {
                player.Top -= num1;
                weiqi1.Top -= num1;
                weiqi2.Top -= num1;
                if (player.Top == 0)
                {
                    player.Top = num1;
                    weiqi1.Top = player.Top + player.Height + weiqi1.Height / 2 - 15;
                    weiqi2.Top = player.Top + player.Height + weiqi2.Height / 2 - 15;
                }
            }
            // 飞机向下
            if (e.KeyCode==Keys.S || e.KeyCode==Keys.Down)
            {
                player.Top += num1;
                weiqi1.Top += num1;
                weiqi2.Top += num1;
                if (player.Top + player.Height + weiqi1.Height >= youxiqu.Height)
                {
                    player.Top = youxiqu.Height - player.Height - weiqi1.Height;
                    weiqi1.Top = player.Top + player.Height + weiqi1.Height / 2 - 15;
                    weiqi2.Top = player.Top + player.Height + weiqi2.Height / 2 - 15;
                }
            }
        }
        //子弹消除方法
        private void ziDqing()
        {
            foreach (PictureBox zidan in zdList)
            {
                zidan.Dispose();
            }
        }
        //飞机消除方法
        private void feiJiqing()
        {
            foreach (PictureBox feiji in feijiList)
            {
                feiji.Dispose();
            }
        }
        // 敌机消除方法
        private void diJiqing()
        {
            foreach (PictureBox diji in dijiList)
            {
                diji.Dispose();
            }
        }

        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        // 飞机创建方法
        private void playerChuan()
        {
            player.Size = new Size(70, 60);
            player.Tag = "player";
            player.Left = youxiqu.Width / 2 - player.Width / 2; // 飞机初始位置
            player.Top = youxiqu.Height - player.Height - 20;
            player.Image = Image.FromFile(@"../../img/RP03.png");
            player.SizeMode = PictureBoxSizeMode.StretchImage; // 图片自适应大小

        }
    }
}

基于C# WinForms窗体——飞机大战

上一篇:WINUSB使用(附STM32Demo)


下一篇:windows下配置ngnix服务器经常出现503问题解决办法