C#实现的打飞机游戏(课程设计)

简介: C#实现的打飞机游戏(课程设计)

前言

相信大家一定玩过打飞机小游戏,但是你们会自己手写一个C#打飞机的小游戏吗,让我们来看看吧

开发环境

开发环境:Windows10 64位

Visual Studio 2019

截图

在这里插入图片描述

核心源码

爆炸对象

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespacePlaneGame
{
   ///<summary>
   /// 爆炸动画的对象
   ///</summary>
   publicclassBoom
   {
       //小飞机爆炸的图片数组
       public Image[] imgs0 = {
                              Properties.Resources.enemy0_down11,
                              Properties.Resources.enemy0_down2,
                              Properties.Resources.enemy0_down3,
                              Properties.Resources.enemy0_down4
                              };
       //中飞机爆炸的图片动画
       public Image[] imgs1 = {
                              Properties.Resources.enemy1_down11,
                              Properties.Resources.enemy1_down2,
                              Properties.Resources.enemy1_down3,
                              Properties.Resources.enemy1_down4
                              };
       //大飞机爆炸的图片数组
       public Image[] imgs2 ={
                              Properties.Resources.enemy2_down11,
                              Properties.Resources.enemy2_down2,
                              Properties.Resources.enemy2_down3,
                              Properties.Resources.enemy2_down4,
                              Properties.Resources.enemy2_down5,
                              Properties.Resources.enemy2_down6
                            };
       /**
        * 敌机在哪里死亡,爆炸就在哪里产生
        */

       public Enemy enemyPlane { get; set; }
       public Image[] imgs { get; set; }
       publicint Width { get; set; }
       publicint Height { get; set; }
       publicBoom(Enemy enemyPlane)
       {
           this.enemyPlane = enemyPlane;
           //图片根据飞机的类型确定以后
           if (this.enemyPlane.Type==0)
           {
               this.imgs = this.imgs0;
           }
           elseif (this.enemyPlane.Type==1)
           {
               this.imgs = this.imgs1;
           }
           elseif(this.enemyPlane.Type==2)
           {
               this.imgs = this.imgs2;
           }
           this.Width = this.imgs[0].Width;
           this.Height = this.imgs[0].Height;
       }


       ///<summary>
       /// 绘画游戏对象
       ///</summary>
       ///<param name="g"></param>
       publicvoidDraw(Graphics g)
       {
           for (int i = 0; i < this.imgs.Length; i++)
           {
               g.DrawImage(this.imgs[i],this.enemyPlane.X,this.enemyPlane.Y,this.Width,this.Height);
           }
           //当爆炸动画播放完成以后,就要移除自己
           DataUtil.boomList.Remove(this);
       }
   }
}

子弹

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespacePlaneGame
{
   ///<summary>
   /// 玩家飞机的子弹
   ///</summary>
   publicclassBullet:GameObject
   {
       publicBullet(int x,int y):base(x,y,Properties.Resources.bullet1)
       {
       }

       ///<summary>
       /// 子弹移动的方法
       ///</summary>
       publicvoidMove()
       {
           //玩家的子弹是向上运行的  所以它的纵坐标应该是减小的
           this.Y = this.Y - 40;
           if (this.Y<0)
           {
               //说明这颗子弹已经跑到屏幕外边去了,我们要在内存当中移除它

               DataUtil.bulletList.Remove(this);
           }
       }
       
       //但是继承下来的Draw不能够满足我们的方法
       //在c#里面,所有的虚方法都可以被重写

       publicoverridevoidDraw(Graphics g)
       {
           this.Move();
           base.Draw(g);  //调父类方法
       }

       
   }
}

敌军

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespacePlaneGame
{
   ///<summary>
   /// 敌人的飞机
   ///</summary>
   publicclassEnemy
   {
       publicint X { get; set; }
       publicint Y { get; set; }
       public Image img { get; set; }
       publicint Width { get; set; }
       publicint Height { get; set; }

       //还应该有一个特殊的属性叫type    0代表小飞机,1代表中飞机,2代表大飞机
       publicint Type { get; set; }
       publicint Speed { get; set; }   //飞机的移动速度
       publicint Life { get; set; }    //飞机的生命值
       publicint Score { get; set; }   //当前的飞向值多少分

       publicEnemy(int x,int y,int type)
       {
           this.X = x;
           this.Y = y;
           this.Type = type;
           //飞机的图片它没有固定下来,它是根据我们飞机的类型来决定的
           if (this.Type==0)
           {
               //小飞机
               this.img = Properties.Resources.enemy0;
               this.Speed = 5;
               this.Life = 1;
               this.Score = 10;
           }
           elseif (this.Type==1)
           {
               //中飞机
               this.img = Properties.Resources.enemy1;
               this.Speed = 3;
               this.Life = 2;
               this.Score = 30;
           }
           elseif (this.Type==2)
           {
               //大飞机
               this.img = Properties.Resources.enemy2;
               this.Speed = 2;
               this.Life = 4;
               this.Score = 50;
           }
           this.Width = this.img.Width;
           this.Height = this.img.Height;
       }

       ///<summary>
       /// 敌人飞机移动的方法
       ///</summary>
       publicvoidMove()
       {
           this.Y = this.Y + this.Speed;
           //当飞机移动到屏幕外边的时候,应该让自己消失
           if (this.Y>850)
           {
               //把自己移除掉
               DataUtil.enemyList.Remove(this);
           }
       }

       //把自己画出来
       publicvoidDraw(Graphics g)
       {
           this.Move();
           g.DrawImage(this.img, this.X, this.Y, this.Width, this.Height);
       }

       ///<summary>
       /// 获取游戏对象的矩形
       ///</summary>
       ///<returns>矩形</returns>
       public Rectangle getRectangle()
       {
           returnnew Rectangle(this.X,this.Y,this.Width,this.Height);
       }

       ///<summary>
       /// 是否死亡
       ///</summary>
       ///<returns></returns>
       publicboolIsDie()
       {
           this.Life--;
           if (this.Life<=0)
           {
               //你死了
               //播放死亡音乐
               System.Media.SoundPlayer sound = new System.Media.SoundPlayer(Properties.Resources.enemy0_down1);
               sound.Play();

               //播放爆炸动画
               Boom b = new Boom(this);
               DataUtil.boomList.Add(b);

               //计算得分
               DataUtil.Score += this.Score;      //在原来的得分之上,加上现在的得分

               DataUtil.enemyList.Remove(this);   //把自己移除

               returntrue;
           }
           else
           {
               //说明你被打了,但是你是一个残血的状态   要更改飞机的图片
               if (this.Type==1)
               {
                   //说明是中飞机
                   this.img = Properties.Resources.enemy1_hit;
               }
               elseif (this.Type==2)
               {
                   //说明是大飞机
                   this.img = Properties.Resources.enemy2_hit;
               }
               returnfalse;
           }
       }
   }
}

玩家的飞机

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespacePlaneGame
{
   ///<summary>
   /// 玩家飞机的对象
   ///</summary>
   publicclassHero:GameObject
   {
     
     

       publicHero(int x,int y):base(x,y,Properties.Resources.hero1)
       {
           DataUtil.IsTowBullet = false;      //默认情况下,它不是双排子弹
           DataUtil.IsSuperBullet = false;     //默认情况下,它不是超级子弹

           this.Width = this.img.Width / 2;
           this.Height = this.img.Height / 2;
       }


       ///<summary>
       /// 玩家飞机发射子弹的方式
       ///</summary>
       publicvoidFire()
       {
           if (DataUtil.IsTowBullet)
           {
               //说明要发双排子弹
               Bullet b_left = new Bullet(this.X, this.Y);
               Bullet b_right = new Bullet(this.X, this.Y);
               //修正左边子弹的位置
               b_left.X = b_left.X + this.Width / 4 - b_left.Width / 2;
               //修正右边子弹的位置
               b_right.X = b_right.X + this.Width / 4 * 3 - b_right.Width / 2;

               DataUtil.bulletList.Add(b_left);
               DataUtil.bulletList.Add(b_right);
           }
           elseif (DataUtil.IsSuperBullet)
           {
               Bullet b = new Bullet(this.X, this.Y);
               b.X = b.X - 20 + this.Width / 2 - b.Width / 2 ;
               b.img = Properties.Resources.bullet3;
               b.Height = this.Height;
               b.Width =  this.Width + 20;

               DataUtil.bulletList.Add(b);
           }
           else
           {
               //玩家飞机发射的子弹是有很多个的
               Bullet b = new Bullet(this.X, this.Y);
               //修正子弹的坐标
               b.X = b.X + this.Width / 2 - b.Width / 2;

               //用集全去装所有的玩家飞机子弹
               DataUtil.bulletList.Add(b);
           }
       }

   }
}


相关文章
|
9月前
|
C# 图形学
C#之四十九 游戏编程周每日总结
C#之四十九 游戏编程周每日总结
40 0
|
9天前
|
存储 缓存 C#
C#语言编写的仅有8KB大小的简易贪吃蛇开源游戏
C#语言编写的仅有8KB大小的简易贪吃蛇开源游戏
C#语言编写的仅有8KB大小的简易贪吃蛇开源游戏
|
2月前
|
程序员 C# Python
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码(2)
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码(2)
|
2月前
|
存储 程序员 C#
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码
|
11月前
|
SQL 数据管理 Java
C#宿舍管理系统(C#课程设计含源码)
C#宿舍管理系统(C#课程设计含源码)
122 0
C#宿舍管理系统(C#课程设计含源码)
|
2月前
|
存储 C# 开发工具
22.C# 中使用变量记录玩家创建的角色名:实现与游戏角色的互动
22.C# 中使用变量记录玩家创建的角色名:实现与游戏角色的互动
28 0
|
2月前
|
定位技术 C# 图形学
Unity和C#游戏编程入门:创建迷宫小球游戏示例
Unity和C#游戏编程入门:创建迷宫小球游戏示例
107 2
|
11月前
|
SQL 开发框架 .NET
基于ASP.NET实现的排课系统(C#课程设计)
基于ASP.NET实现的排课系统(C#课程设计)
117 0
|
11月前
|
程序员 C#
C#财务管理系统(C#课程设计)
C#财务管理系统(C#课程设计)
86 0