基本信息
源码名称:c# 飞机大战小游戏源码
源码大小:4.44M
文件格式:.zip
开发语言:C#
更新时间:2020-01-15
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

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]);//从子弹列表中移出该子弹
                }
            }
        }
    }
}