实例介绍
【实例简介】
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
namespace PlayPlane
{
class PlaneClass
{
Image zidan = Image.FromFile("bullet.png");//定义子弹图片
Image feiji = Image.FromFile("plane.png");//定义飞机图片
Image dj1 = Image.FromFile("enemy1.png");//定义敌机图片1
Image dj2 = Image.FromFile("enemy2.png");//定义敌机图片2
Image dj3 = Image.FromFile("enemy3.png");//定义敌机图片3
Image baozha = Image.FromFile("baozha.gif");//定义爆炸效果图片
bool pause = false;//表示是否暂停
//judge表示关卡数,score表示分数,宝箱表示拥有的特技数量
int judge = 0, score = 0, baoxian = 3;
int zidanshu = 1;//初始化子弹的数量
class Plane//飞机类
{
public int x;//飞机的X坐标
public int y;//飞机的Y坐标
public Image im;//飞机图片
public Plane(int x, int y, Image im)
{
this.x = x;//初始化飞机的X坐标
this.y = y;//初始化飞机的Y坐标
this.im = im;//初始化飞机图片
}
}
class Bullet//子弹类
{
public int x;//子弹的X坐标
public int y;//子弹的Y坐标
public Image im;//子弹图片
public Bullet(int x, int y, Image im)
{
this.x = x;//初始化子弹的X坐标
this.y = y;//初始化子弹的Y坐标
this.im = im;//初始化子弹图片
}
}
List<Bullet> bullet_lsit = new List<Bullet>();//子弹列表
class Enemy//敌机类
{
public int x;//敌机的X坐标
public int y;//敌机的Y坐标
public int speed;//敌机的纵向速度
public int xspeed;//敌机的横向速度
public Image im;//敌机图片
public Enemy(int x, int y, int speed, int xspeed, Image im)
{
this.x = x;//初始化敌机的X坐标
this.y = y;//初始化敌机的Y坐标
this.speed = speed;//初始化敌机的纵向速度
this.xspeed = xspeed;//初始化敌机的横向速度
this.im = im;//初始化敌机图片
}
}
List<Enemy> enemy_lsit = new List<Enemy>();//敌机列表
int x, y;
public void iniPlane(Form frm)
{
x = frm.Width / 2 - feiji.Width / 2;//设置飞机的初始X坐标
y = frm.Height - feiji.Height - 70;//设置飞机的初始Y坐标
}
//使用键盘控制飞机的上下左右移动及暂停、爆炸
public void keyControl(Form frm, KeyEventArgs e, Label lbl)
{
if (e.KeyCode == Keys.Up)//方向键↑
{
if (y >= 10)//如果Y坐标大于等于10,则将其减掉10
{
y -= 10;
}
}
if (e.KeyCode == Keys.Down)//方向键↓
{
//如果Y坐标小于等于窗体高度减去飞机高度减去30(标题栏 边框)
if (y <= frm.ClientSize.Height - feiji.Height - 30)
{
y = 10;//Y坐标加10
}
}
if (e.KeyCode == Keys.Left)//方向键←
{
if (x >= 10)//如果X坐标大于等于10,则将其减掉10
{
x -= 10;
}
}
if (e.KeyCode == Keys.Right)//方向键→
{
//如果X坐标小于等于窗体宽度减去飞机宽度减去30(标题栏 边框)
if (x <= frm.ClientSize.Width - feiji.Width - 30)
{
x = 10;//X坐标加10
}
}
if (e.KeyCode == Keys.Enter)//回车键——暂停
pause = !pause;
if (e.KeyCode == Keys.Space)//空格键——爆炸
{
if (baoxian > 0)//判断是否有宝箱(即爆炸特技)
{
baoxian--;//宝箱数量减一
//根据敌机速度,设置得分
for (int i = 0; i < enemy_lsit.Count; i )
{
if (enemy_lsit[i].speed == 2)//如果速度为2,则分数加2000
score = 2000;
else if (enemy_lsit[i].speed == 4)//如果速度为4,则分数加1000
score = 1000;
else if (enemy_lsit[i].speed == 7)//如果速度为7,则分数加500
score = 500;
enemy_lsit.Remove(enemy_lsit[i]);//清除指定敌机
enemy_lsit.Clear();//清空敌机列表
lbl.Text = "得分:" Convert.ToString(score);//显示得分
}
Graphics g = frm.CreateGraphics();//创建绘图对象
g.DrawImage(baozha, 0, 0, frm.Width, frm.Height);//绘制爆炸效果
}
}
}
//打飞机游戏的实现算法
public void execProgram(Graphics g, Form frm, Label lbl, Timer timer)
{
//判断是否暂停
if (pause)
{
//绘制暂停文字
g.DrawString("暂 停", new Font("微软雅黑", 22), Brushes.Red, new PointF(frm.Width / 2 - 50, frm.Height / 2 - 50));
return;
}
judge ;//关数加1
Plane plane = new Plane(x, y, feiji);//创建飞机对象
//击落敌机
for (int i = 0; i < enemy_lsit.Count; i )//遍历敌机列表
{
for (int j = 0; j < bullet_lsit.Count; j )//遍历子弹列表
{
if (Math.Abs(bullet_lsit[j].x - enemy_lsit[i].x) < (bullet_lsit[j].im.Width enemy_lsit[i].im.Width) / 2 && Math.Abs(bullet_lsit[j].y - enemy_lsit[i].y) < (bullet_lsit[j].im.Height enemy_lsit[i].im.Height) / 2)
{
if (enemy_lsit[i].speed == 2)//如果速度为2,则分数加2000
score = 2000;
else if (enemy_lsit[i].speed == 4)//如果速度为4,则分数加1000
score = 1000;
else if (enemy_lsit[i].speed == 7)//如果速度为7,则分数加500
score = 500;
enemy_lsit.Remove(enemy_lsit[i]);//清除指定敌机
lbl.Text = "得分:" Convert.ToString(score);//显示得分
break;
}
}
}
//碰撞敌机
for (int i = 0; i < enemy_lsit.Count; i )//遍历敌机列表
{
bool isCrashed = false;//判断游戏是否结束
//定义飞机坐标
int px = (x * 2 plane.im.Width) / 2, py = (y * 2 plane.im.Height) / 2;
//定义敌机坐标
int ex = (enemy_lsit[i].x * 2 enemy_lsit[i].im.Width) / 2, ey = (enemy_lsit[i].y * 2 enemy_lsit[i].im.Height) / 2;
//如果飞机坐标与敌机坐标重合度在一定范围
if (Math.Sqrt((px - ex) * (px - ex) (py - ey) * (py - ey)) <= (enemy_lsit[i].im.Width / 2 plane.im.Height / 2 - 20))
{
isCrashed = true;//指示游戏结束
}
if (isCrashed)
{
timer.Stop();//停止计时器
//提示游戏结束
g.DrawString("Game Over", new Font("微软雅黑",22), Brushes.Red, new PointF(frm.Width / 2 - 100, frm.Height / 2 - 50));
return;
}
}
if (judge == 100)//如果关数到达100,则重新开始
judge = 0;
g.DrawImage(Image.FromFile("bg.png"),0,0,302,501);//刷新
GC.Collect();//强制回收垃圾
//实时绘制飞机位置
g.DrawImage(feiji, new Point(x, y));
//指定要加载的敌机类型
if (judge % 50 == 0)//大型敌机
{
Enemy di = new Enemy(new Random().Next(10000) % 180, 0, 2, new Random().Next(10000) % 5 - 2, dj1);
enemy_lsit.Add(di);
}
if (judge % 30 == 0)//中型敌机
{
Enemy di = new Enemy(new Random().Next(10000) % 180, 0, 4, new Random().Next(10000) % 5 - 2, dj2);
enemy_lsit.Add(di);
}
if (judge % 20 == 0)//小型敌机
{
Enemy di = new Enemy(new Random().Next(10000) % 180, 0, 7, new Random().Next(10000) % 5 - 1, dj3);
enemy_lsit.Add(di);
}
//绘制子弹
if (judge % 6 == 0)
{
if (zidanshu == 1)//如果只有1个子弹
{
//初始化子弹
Bullet bul = new Bullet((x * 2 plane.im.Width) / 2 - 5, plane.y - zidan.Height, zidan);
bullet_lsit.Add(bul);//将子弹添加到子弹列表中
}
else if (zidanshu == 2)//如果只有2个子弹
{
//初始化第1个子弹
Bullet bul = new Bullet((x * 2 plane.im.Width / 3) / 2 - 5, plane.y - zidan.Height, zidan);
bullet_lsit.Add(bul);//将第1个子弹添加到子弹列表中
//初始化第2个子弹
Bullet bul1 = new Bullet((x * 2 (2 * plane.im.Width) / 3) / 2 20, plane.y - zidan.Height, zidan);
bullet_lsit.Add(bul1);//将第2个子弹添加到子弹列表中
}
}
//绘制敌机
for (int i = 0; i < enemy_lsit.Count; i )
{
//在指定位置出现敌机
g.DrawImage(enemy_lsit[i].im, new Point(enemy_lsit[i].x, enemy_lsit[i].y));
enemy_lsit[i].y = enemy_lsit[i].speed;//敌机的Y坐标
enemy_lsit[i].x = enemy_lsit[i].xspeed;//敌机的X坐标
//如果敌机的XY坐标不在窗体范围内
if (enemy_lsit[i].y > frm.Height || enemy_lsit[i].x < 0 - enemy_lsit[i].im.Width || enemy_lsit[i].x >= frm.Width)
{
enemy_lsit.Remove(enemy_lsit[i]);//从敌机列表中移出该敌机
}
}
//绘制连续发射的子弹
for (int i = 0; i < bullet_lsit.Count; i )
{
//在指定位置出现子弹
g.DrawImage(zidan, new Point(bullet_lsit[i].x, bullet_lsit[i].y));
bullet_lsit[i].y -= 20;//设置子弹循环向上移动(20像素)
if (bullet_lsit[i].y < -40)//如果子弹的Y坐标小于-40
{
bullet_lsit.Remove(bullet_lsit[i]);//从子弹列表中移出该子弹
}
}
}
}
}
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论