基本信息
源码名称:C# 实现飞机大战 游戏源码(可以直接运行)
源码大小:10.76M
文件格式:.zip
开发语言:C#
更新时间:2019-05-20
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
windows应用
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));
}
}
}
}