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

2021-01-22 15:13

阅读:857

标签:创建   href   interval   ali   def   val   自适应   chm   点击   

原文:基于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 zdList = new List();// 存储生成的子弹
        List feijiList = new List();// 存储飞机和尾气
        List dijiList = new List();// 存储生成敌机
        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= player.Left && diji.Left = player.Top && diji.Top = diji.Top && zidan.Top =diji.Left && zidan.Left31)
            {
                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窗体——飞机大战

标签:创建   href   interval   ali   def   val   自适应   chm   点击   

原文地址:https://www.cnblogs.com/lonelyxmas/p/12079058.html


评论


亲,登录后才可以留言!