在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#游戏开发 → C# 实现飞机大战 游戏源码(可以直接运行)

C# 实现飞机大战 游戏源码(可以直接运行)

C#游戏开发

下载此实例
  • 开发语言:C#
  • 实例大小:10.76M
  • 下载次数:123
  • 浏览次数:2881
  • 发布时间:2019-05-20
  • 实例类别:C#游戏开发
  • 发 布 人:678678678
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 飞机 飞机大战

实例介绍

【实例简介】windows应用

【实例截图】

from clipboard

from clipboard


【核心代码】


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GameAir
{
    public partial class gameconser : Form
    {
        //游戏准备结束的imge
        public Image imgGamePrepare = Image.FromFile("..\\..\\image\\gamestart.jpg");
        public Image imgGameOver = Image.FromFile("..\\..\\image\\gameover.jpg");
        //背景的image
        public Image imgBackGround = Image.FromFile("..\\..\\image\\back1.png");
        //我军飞机的image
        public Image imgMyPlane = Image.FromFile(@"..\..\image\plane.png");
        public Image imgMyPlaneLeft = Image.FromFile(@"..\..\image\left.png");
        public Image imgMyPlaneRight = Image.FromFile(@"..\..\image\right.png");
        //敌机的image
        public Image imgEnemyPlane1 = Image.FromFile(@"..\..\image\enemyplane1.png");
        public Image imgEnemyPlane2 = Image.FromFile(@"..\..\image\enemyplane2.png");
        //子弹的image
        public Image imgBullet1 = Image.FromFile(@"..\..\image\MB_ultimate1.png");
        public Image imgBullet2 = Image.FromFile(@"..\..\image\MB_ultimate2.png");
        //爆炸的imge集合
        public Image[] imgExplode = {
            Image.FromFile(@"..\..\image\explode1.png"),
            Image.FromFile(@"..\..\image\explode2.png"),
            Image.FromFile(@"..\..\image\explode3.png"),
            Image.FromFile(@"..\..\image\explode4.png")
            };
        //补给对象的img
        public Image imgSupply = Image.FromFile(@"..\..\image\bulletbox1.png");
        //定义音乐对象
        public Sound soundBackGround = new Sound(@"..\..\music\zengjia.mp3");
        public Sound soundPrepare = new Sound(@"..\..\music\gamebegin.mp3");
        public Sound soundMyBullt = new Sound(@"..\..\music\mybullt.mp3");
        public Sound soundEnemyBullt = new Sound(@"..\..\music\bullt.mp3");
        public Sound soundExplode = new Sound(@"..\..\music\missileexplode.mp3");
        public Sound soundOver = new Sound(@"..\..\music\gameover.mp3");

        //随机数
        public Random rnd = new Random();

        //定义游戏状态类
        public GameState gameState = GameState.Prepare;
        //定义背景类
        public BackGround background;
        //定义我军飞机类
        public MyPlane myplane;
        //定义敌机对象集合
        public List<EnemyPlane> listEnemyPlane=new List<EnemyPlane>();
        //定义子弹对象列表
        public List<Bullet> listBullt = new List<Bullet>();
        //定义爆炸对象列表
        public List<Explode> listExplode = new List<Explode>();
        //定义补给对象列表
        public List<Supply> listSupply = new List<Supply>();
        //定义生命值
        public StateBar stateBar;        

        //构造函数
        public gameconser()
        {
            InitializeComponent();

            this.Text = "飞机大战";
            this.BackColor = Color.Black;
            this.Width = 1000;
            this.Height = 500;
            this.StartPosition = FormStartPosition.CenterScreen;//指定窗口初始位置
            this.MaximizeBox = false;//最大化
            this.MinimizeBox = false;//最小化
            this.FormBorderStyle = FormBorderStyle.FixedDialog;//指定边框样式
            this.DoubleBuffered = true;//双缓冲技术:使用双缓冲技术绘制窗体,解决闪烁问题
            background = new BackGround(0, 0, 1000, 500, 1, this);
            myplane = new MyPlane(10, 200, 80, 30, 10, 100, true, this);
            stateBar = new StateBar(30, 20, 50, 15, true, this);
        }

        //设置子线程线程
        private void gameconser_Load(object sender, EventArgs e)
        {           
            //Thread t = new Thread(new ThreadStart(Run));//创建子线程
            //t.IsBackground = true;//设置为后台线程
            //t.Start();//开始线程
        }

        //画图
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);//调用基类的Onpaint方法
            if(gameState==GameState.Prepare)//y游戏准备状态
            {
                e.Graphics.DrawImage(imgGamePrepare, 0, 0, 1000, 500);
                soundPrepare.RepeatPlay();
                return;
            }
            if (gameState == GameState.Fail)//y游戏结束状态
            {
                Thread.Sleep(1000);//暂停一秒
                e.Graphics.DrawImage(imgGameOver, 0, 0, 1000, 500);
                soundOver.RepeatPlay();
                return;
            }
            background.DrawMe(e.Graphics);//调用背景的DrawMe
            myplane.DrawMe(e.Graphics);//调用我军飞机的DrawMe
            stateBar.DrawMe(e.Graphics);//画出生命值
            for(int i=0;i<listEnemyPlane.Count;i  )//根据集合内的敌机数逐个调用DrawMe
            {
                listEnemyPlane[i].DrawMe(e.Graphics);                
            }
            for (int i = 0; i < listBullt.Count; i  )//画子弹
            {
                listBullt[i].DrawMe(e.Graphics);
            }
            for (int i = 0; i < listExplode.Count; i  )//画爆炸效果图
            {
                listExplode[i].DrawMe(e.Graphics);
                soundExplode.AsyncPlay();
            }
            for (int i = 0; i < listSupply.Count; i  )//画补给图
            {
                listSupply[i].DrawMe(e.Graphics);
            }
        }

        //每33毫秒刷新
        public void Run()
        {
            while (true)
            {
                Thread.Sleep(33);//让当前线程休眠33毫秒
                GreatEnemyPlane();//创建敌机
                GreatSupply();//创建补给
                this.Invalidate();//刷新窗体(使窗体重绘)
            }
        }

        //按键按下
        private void gameconser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && gameState == GameState.Prepare)//enter键按下开始
            {
                gameState = GameState.Start;
                Thread t = new Thread(new ThreadStart(Run));//创建子线程
                t.IsBackground = true;//设置为后台线程
                t.Start();//开始线程
                soundBackGround.RepeatPlay();
            }
            myplane.KeyDown(e);
        }

        //按键松开
        private void gameconser_KeyUp(object sender, KeyEventArgs e)
        {
            myplane.KeyUp(e);
        }

        //根据随机数创建敌机
        public void GreatEnemyPlane()
        {
            if(rnd.Next(100)%46==0)//百分之二创建从右方出来的一型敌机
            {
                listEnemyPlane.Add(new EnemyPlane(1000, rnd.Next(450), 60, 20, rnd.Next(4, 10), 1, true, Direction.Left, this));
            }
            if (rnd.Next(100) % 46 == 0)//百分之二创建从右方出来的二型敌机
            {
                listEnemyPlane.Add(new EnemyPlane(1000, rnd.Next(450), 60, 20, rnd.Next(4, 10), 2, true, Direction.Left, this));
            }
            if (rnd.Next(1000) % 580 == 0)//千分之一创建从下方出来的一型敌机
            {
                listEnemyPlane.Add(new EnemyPlane(rnd.Next(300, 900), 500, 60, 20, rnd.Next(2, 5), 1, true, Direction.Up, this));
            }
            if (rnd.Next(1000) % 580 == 0)//千分之一创建从下方出来而且向左上方移动的二型敌机
            {
                listEnemyPlane.Add(new EnemyPlane(rnd.Next(600, 900), 500, 60, 20, rnd.Next(2, 5), 2, true, Direction.UpLeft, this));
            }
        }

        //随机创建补给
        public void GreatSupply()
        {
            if (rnd.Next(100) % 56 == 0)
            {
                listSupply.Add(new Supply(rnd.Next(100, 700), 0, 10, 30, rnd.Next(2, 5), true, Direction.Down, this));
            }
        }
    }
}


标签: 飞机 飞机大战

实例下载地址

C# 实现飞机大战 游戏源码(可以直接运行)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 浪子易清 发表于: 2019-12-14 15:48 51
为什么我源码无法打开,exe程序也不能运行???

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有1条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警