public static int x = 180;
public static int y = 530;//坐标
public static int health = 100; //血量
private const int PLANE_OFFSET = 12;//移动速度
public static Image myPlaneImg=Resource.plane;//我方飞机图片
static List<Keys> keys = new List<Keys>();//键盘键列表,用于控制飞机移动
static Image gameOver = Resource.gameover;
public static bool isGetGun = false;//是否得到shotgun的标志
public static bool isGetBlood = false;//是否得到bloodbox的标志
public static bool isGameOver = false; //游戏是否结束的标志
public static int score = 0; //得分
调用Graphics类的DrawImage函数显示我方飞机。
/// <summary>
/// 显示我方飞机
/// </summary>
/// <param name="g"></param>
public static void MyPlaneShow(Graphics g)
{
if (health > 0)
{
g.DrawImage(myPlaneImg, x, y);
}
else if (health <= 0 || score <= 0)
{
isGameOver = true;
g.DrawImage(myPlaneImg, 0, -300);
g.DrawImage(gameOver, 10, 260);
}
else if (isGetBlood && health <= 90)
{
health += 10;
}
}
通过Keys类定义键盘事件函数。
成员名称
说明
KeyCode
从键值提取键代码的位屏蔽
Modifiers
从键值提取修饰符的位屏蔽
None
没有按任何键
LButton
鼠标左按钮
RButton
鼠标石按钮
Cancel
Cancel 键
MButton
鼠标中按钮(三个按钮的鼠标)
XButton1
第一个X鼠标按钮(五个按钮的鼠标)
XButton2
第二个 X 鼠标按钮(五个按钮的鼠标)
Back
Backspace 键
这里先用熟悉的WASD键来控制我方飞机移动。
/// <summary>
/// 显示我方飞机
/// </summary>
/// <param name="g"></param>
public static void MyPlaneShow(Graphics g)
{
if (health > 0)
{
g.DrawImage(myPlaneImg, x, y);
}
else if (health <= 0 || score <= 0)
{
isGameOver = true;
g.DrawImage(myPlaneImg, 0, -300);
g.DrawImage(gameOver, 10, 260);
}
else if (isGetBlood && health <= 90)
{
health += 10;
}
}
/// <summary>
/// 松开键盘键
/// </summary>
/// <param name="key"></param>
public static void Keyup(Keys key)
{
keys.Remove(key);
}
/// <summary>
/// 按下键盘键
/// </summary>
/// <param name="key"></param>
public static void Keydown(Keys key)
{
if (!keys.Contains(key))
{
keys.Add(key);
}
}
/// <summary>
/// 判断按键是否被按下
/// </summary>
/// <param name="key"></param>
/// <returns>是则返回true 不是则返回false</returns>
public static bool IsKeyDown(Keys key)
{
return keys.Contains(key);
}
/// <summary>
/// 用键盘控制我方飞机移动
/// </summary>
public static void MyPlaneMove()
{
if(isGameOver)
{
return;
}
if (IsKeyDown(Keys.A))
{
myPlaneImg = Resource.planeLeft;
if (x < 5)
x = 5;
x -= PLANE_OFFSET;
}
if (IsKeyDown(Keys.D))
{
myPlaneImg = Resource.planeRight;
if (x > 370)
x = 370;
x += PLANE_OFFSET;
}
if (IsKeyDown(Keys.W))
{
if (y < 5)
y = 5;
y -= PLANE_OFFSET;
}
if (IsKeyDown(Keys.S))
{
if (y > 530)
y = 530;
y += PLANE_OFFSET;
}
}
4、敌方飞机
创建一个Fighter实体类,定义字段如下。
Image redImg;
Image greenImg;
Image yellowImg;
public Image fighterImg;//敌机图片
private const int FIGHTER_OFFSET = 4;//敌机图片移动速度
public int _x = 0;
public int _y = 0;//敌机图片移动起始的坐标
public static List<Fighter> fighters = new List<Fighter>();//敌机对象列表
private int fi;//敌机图片索引
List<Image> imgList = new List<Image>();//敌机图片列表
public bool flag = false;//碰撞的标志
通过Random产生随机数,用于定义随机出现的敌机x以及y点坐标。
/// <summary>
/// 随机产生敌机
/// </summary>
public static void ProduceFighter()
{
Random rad = new Random();
if (rad.Next(18) == 0)
{
Fighter f = new Fighter(rad.Next(0, 350), rad.Next(0, 3));
fighters.Add(f);
}
}
public Fighter(int x,int i)
{
_x = x;//横坐标
fi = i;
redImg = Resource.fighterRed;
greenImg = Resource.fighterGreen;
yellowImg = Resource.fighterYellow;
switch (fi)
{
case 0:
fighterImg = redImg;break;
case 1:
fighterImg = greenImg;break;
case 2:
fighterImg = yellowImg;break;
default:
break;
}
imgList.Add(redImg);
imgList.Add(greenImg);
imgList.Add(yellowImg);
}
通过Graphics绘制敌机图片。
/// <summary>
/// 出现敌机
/// </summary>
public void FighterShow(Graphics g)
{
g.DrawImage(fighterImg,_x,_y);
}
public void fMove()
{
_y += FIGHTER_OFFSET;
}
/// <summary>
/// 敌机移动函数
/// </summary>
public static void FighterMove(Graphics g)//通过定时位置让图片发生偏移
{
for (int i = 0; i < fighters.Count; i++)
{
fighters[i].FighterShow(g);
fighters[i].fMove();
if (fighters[i]._y > 650)
{
fighters.Remove(fighters[i]);
}
}
}
5、子弹及碰撞检测
创建一个MyBullet实体类,定义字段如下。
private int x;//子弹横坐标
private int y;//子弹纵坐标
private const int BULLET_OFFSET = 18;//移动速度
public int Angle;//子弹角度
private Image bulImg;//定义子弹图片
private const double PI = Math.PI;
public static List<MyBullet> mybulList = new List<MyBullet>();//子弹对象集合
static Bitmap bm = new Bitmap(Resource.bomb4);//爆炸图片
public bool isHit = false;//碰撞的标志
public static int x = 180;
public static int y = 530;//坐标
public static int health = 100; //血量
private const int PLANE_OFFSET = 12;//移动速度
public static Image myPlaneImg=Resource.plane;//我方飞机图片
static List<Keys> keys = new List<Keys>();//键盘键列表,用于控制飞机移动
static Image gameOver = Resource.gameover;
public static bool isGetGun = false;//是否得到shotgun的标志
public static bool isGetBlood = false;//是否得到bloodbox的标志
public static bool isGameOver = false; //游戏是否结束的标志
public static int score = 0; //得分
调用Graphics类的DrawImage函数显示我方飞机。
/// <summary>
/// 显示我方飞机
/// </summary>
/// <param name="g"></param>
public static void MyPlaneShow(Graphics g)
{
if (health > 0)
{
g.DrawImage(myPlaneImg, x, y);
}
else if (health <= 0 || score <= 0)
{
isGameOver = true;
g.DrawImage(myPlaneImg, 0, -300);
g.DrawImage(gameOver, 10, 260);
}
else if (isGetBlood && health <= 90)
{
health += 10;
}
}
通过Keys类定义键盘事件函数。
成员名称
说明
KeyCode
从键值提取键代码的位屏蔽
Modifiers
从键值提取修饰符的位屏蔽
None
没有按任何键
LButton
鼠标左按钮
RButton
鼠标石按钮
Cancel
Cancel 键
MButton
鼠标中按钮(三个按钮的鼠标)
XButton1
第一个X鼠标按钮(五个按钮的鼠标)
XButton2
第二个 X 鼠标按钮(五个按钮的鼠标)
Back
Backspace 键
这里先用熟悉的WASD键来控制我方飞机移动。
/// <summary>
/// 显示我方飞机
/// </summary>
/// <param name="g"></param>
public static void MyPlaneShow(Graphics g)
{
if (health > 0)
{
g.DrawImage(myPlaneImg, x, y);
}
else if (health <= 0 || score <= 0)
{
isGameOver = true;
g.DrawImage(myPlaneImg, 0, -300);
g.DrawImage(gameOver, 10, 260);
}
else if (isGetBlood && health <= 90)
{
health += 10;
}
}
/// <summary>
/// 松开键盘键
/// </summary>
/// <param name="key"></param>
public static void Keyup(Keys key)
{
keys.Remove(key);
}
/// <summary>
/// 按下键盘键
/// </summary>
/// <param name="key"></param>
public static void Keydown(Keys key)
{
if (!keys.Contains(key))
{
keys.Add(key);
}
}
/// <summary>
/// 判断按键是否被按下
/// </summary>
/// <param name="key"></param>
/// <returns>是则返回true 不是则返回false</returns>
public static bool IsKeyDown(Keys key)
{
return keys.Contains(key);
}
/// <summary>
/// 用键盘控制我方飞机移动
/// </summary>
public static void MyPlaneMove()
{
if(isGameOver)
{
return;
}
if (IsKeyDown(Keys.A))
{
myPlaneImg = Resource.planeLeft;
if (x < 5)
x = 5;
x -= PLANE_OFFSET;
}
if (IsKeyDown(Keys.D))
{
myPlaneImg = Resource.planeRight;
if (x > 370)
x = 370;
x += PLANE_OFFSET;
}
if (IsKeyDown(Keys.W))
{
if (y < 5)
y = 5;
y -= PLANE_OFFSET;
}
if (IsKeyDown(Keys.S))
{
if (y > 530)
y = 530;
y += PLANE_OFFSET;
}
}
4、敌方飞机
创建一个Fighter实体类,定义字段如下。
Image redImg;
Image greenImg;
Image yellowImg;
public Image fighterImg;//敌机图片
private const int FIGHTER_OFFSET = 4;//敌机图片移动速度
public int _x = 0;
public int _y = 0;//敌机图片移动起始的坐标
public static List<Fighter> fighters = new List<Fighter>();//敌机对象列表
private int fi;//敌机图片索引
List<Image> imgList = new List<Image>();//敌机图片列表
public bool flag = false;//碰撞的标志
通过Random产生随机数,用于定义随机出现的敌机x以及y点坐标。
/// <summary>
/// 随机产生敌机
/// </summary>
public static void ProduceFighter()
{
Random rad = new Random();
if (rad.Next(18) == 0)
{
Fighter f = new Fighter(rad.Next(0, 350), rad.Next(0, 3));
fighters.Add(f);
}
}
public Fighter(int x,int i)
{
_x = x;//横坐标
fi = i;
redImg = Resource.fighterRed;
greenImg = Resource.fighterGreen;
yellowImg = Resource.fighterYellow;
switch (fi)
{
case 0:
fighterImg = redImg;break;
case 1:
fighterImg = greenImg;break;
case 2:
fighterImg = yellowImg;break;
default:
break;
}
imgList.Add(redImg);
imgList.Add(greenImg);
imgList.Add(yellowImg);
}
通过Graphics绘制敌机图片。
/// <summary>
/// 出现敌机
/// </summary>
public void FighterShow(Graphics g)
{
g.DrawImage(fighterImg,_x,_y);
}
public void fMove()
{
_y += FIGHTER_OFFSET;
}
/// <summary>
/// 敌机移动函数
/// </summary>
public static void FighterMove(Graphics g)//通过定时位置让图片发生偏移
{
for (int i = 0; i < fighters.Count; i++)
{
fighters[i].FighterShow(g);
fighters[i].fMove();
if (fighters[i]._y > 650)
{
fighters.Remove(fighters[i]);
}
}
}
5、子弹及碰撞检测
创建一个MyBullet实体类,定义字段如下。
private int x;//子弹横坐标
private int y;//子弹纵坐标
private const int BULLET_OFFSET = 18;//移动速度
public int Angle;//子弹角度
private Image bulImg;//定义子弹图片
private const double PI = Math.PI;
public static List<MyBullet> mybulList = new List<MyBullet>();//子弹对象集合
static Bitmap bm = new Bitmap(Resource.bomb4);//爆炸图片
public bool isHit = false;//碰撞的标志