前言
相信大家一定玩過打飛機小游戲,但是你們會自己手寫一個C#打飛機的小游戲嗎,讓我們來看看吧
開發環境
開發環境:Windows10 64位
Visual Studio 2019
截圖



核心原始碼
爆炸物件
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
namespace PlaneGame
{
/// <summary>
/// 爆炸影片的物件
/// </summary>
public class Boom
{
//小飛機爆炸的圖片陣列
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; }
public int Width { get; set; }
public int Height { get; set; }
public Boom(Enemy enemyPlane)
{
this.enemyPlane = enemyPlane;
//圖片根據飛機的型別確定以后
if (this.enemyPlane.Type==0)
{
this.imgs = this.imgs0;
}
else if (this.enemyPlane.Type==1)
{
this.imgs = this.imgs1;
}
else if(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>
public void Draw(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;
namespace PlaneGame
{
/// <summary>
/// 玩家飛機的子彈
/// </summary>
public class Bullet:GameObject
{
public Bullet(int x,int y):base(x,y,Properties.Resources.bullet1)
{
}
/// <summary>
/// 子彈移動的方法
/// </summary>
public void Move()
{
//玩家的子彈是向上運行的 所以它的縱坐標應該是減小的
this.Y = this.Y - 40;
if (this.Y<0)
{
//說明這顆子彈已經跑到螢屏外邊去了,我們要在記憶體當中移除它
DataUtil.bulletList.Remove(this);
}
}
//但是繼承下來的Draw不能夠滿足我們的方法
//在c#里面,所有的虛方法都可以被重寫
public override void Draw(Graphics g)
{
this.Move();
base.Draw(g); //調父類方法
}
}
}
敵軍
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
namespace PlaneGame
{
/// <summary>
/// 敵人的飛機
/// </summary>
public class Enemy
{
public int X { get; set; }
public int Y { get; set; }
public Image img { get; set; }
public int Width { get; set; }
public int Height { get; set; }
//還應該有一個特殊的屬性叫type 0代表小飛機,1代表中飛機,2代表大飛機
public int Type { get; set; }
public int Speed { get; set; } //飛機的移動速度
public int Life { get; set; } //飛機的生命值
public int Score { get; set; } //當前的飛向值多少分
public Enemy(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;
}
else if (this.Type==1)
{
//中飛機
this.img = Properties.Resources.enemy1;
this.Speed = 3;
this.Life = 2;
this.Score = 30;
}
else if (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>
public void Move()
{
this.Y = this.Y + this.Speed;
//當飛機移動到螢屏外邊的時候,應該讓自己消失
if (this.Y>850)
{
//把自己移除掉
DataUtil.enemyList.Remove(this);
}
}
//把自己畫出來
public void Draw(Graphics g)
{
this.Move();
g.DrawImage(this.img, this.X, this.Y, this.Width, this.Height);
}
/// <summary>
/// 獲取游戲物件的矩形
/// </summary>
/// <returns>矩形</returns>
public Rectangle getRectangle()
{
return new Rectangle(this.X,this.Y,this.Width,this.Height);
}
/// <summary>
/// 是否死亡
/// </summary>
/// <returns></returns>
public bool IsDie()
{
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); //把自己移除
return true;
}
else
{
//說明你被打了,但是你是一個殘血的狀態 要更改飛機的圖片
if (this.Type==1)
{
//說明是中飛機
this.img = Properties.Resources.enemy1_hit;
}
else if (this.Type==2)
{
//說明是大飛機
this.img = Properties.Resources.enemy2_hit;
}
return false;
}
}
}
}
玩家的飛機
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace PlaneGame
{
/// <summary>
/// 玩家飛機的物件
/// </summary>
public class Hero:GameObject
{
public Hero(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>
public void Fire()
{
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);
}
else if (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);
}
}
}
}
原文鏈接
https://gitee.com/shadow22/aircraft_wars
推薦閱讀
《JAVA學生管理系統》
《C#圖書管理系統》
《C#酒店管理系統》
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/293352.html
標籤:java
上一篇:Springboot整合PageOffice 實作word在線編輯保存。
下一篇:Java基礎知識之泛型簡單介紹
