游戲介紹
這是一個聲控飛機游戲,具體玩法模仿iphone上面的ahhhpa,飛機會自動下降,你可以聲控飛機上升與發射子彈,不過這里聲控自己可以設置,支持中文和英文。
詳細介紹
Element 游戲元素類,提供游戲元素基本操作與屬性
public abstract class Element
{
#region 字段
/// <summary>
/// 元素坐標X
/// </summary>
private int x;
/// <summary>
/// 元素坐標Y
/// </summary>
private int y;
/// <summary>
/// 是否還“活着”
/// </summary>
private bool alive = true;
/// <summary>
/// 顯示用的圖片
/// </summary>
private Image img;
/// <summary>
/// 移動的速度
/// </summary>
private int speed;
/// <summary>
/// 寬度
/// </summary>
private int width;
/// <summary>
/// 高度
/// </summary>
private int height;
#endregion
#region 屬性
public int Width
{
get { return width; }
set { width = value; }
}
public int Height
{
get { return height; }
set { height = value; }
}
/// <summary>
/// 顯示用的圖片
/// </summary>
public Image Img
{
get { return img; }
set { img = value; }
}
public int Speed
{
get { return speed; }
set { speed = value; }
}
public bool Alive
{
get { return alive; }
set { alive = value; }
}
/// <summary>
/// 元素坐標X
/// </summary>
public int X
{
get
{
return x;
}
set
{
x = value;
}
}
/// <summary>
/// 元素坐標Y
/// </summary>
public int Y
{
get
{
return y;
}
set
{
y = value;
}
}
#endregion
#region 函數
/// <summary>
/// 顯示自己
/// </summary>
/// <param name="g"></param>
public abstract void Show(Graphics g);
/// <summary>
/// 獲取自己的范圍
/// </summary>
public Rectangle GetScope()
{
Rectangle rt = new Rectangle(x, y, width, height);
return rt;
}
/// <summary>
/// 構造函數
/// </summary>
/// <param name="x">x坐標</param>
/// <param name="y">y坐標</param>
public Element(int x, int y, int speed)
{
this.x = x; this.y = y; this.speed = speed;
this.alive = true;
}
/// <summary>
/// 死亡
/// </summary>
public void Death()
{
alive = false;
}
#endregion
}
RoadBlock 游戲障礙物類
public class RoadBlock : Element
{
public bool IfCatch { get; set; }
static Random rd = new Random();
static int XMax = FrmMain.GAMEWIDTH;
static int YMax = FrmMain.GAMEHEIGHT-70;
static int YMin=50;
//bool first = true;
public override void Show(System.Drawing.Graphics g)
{
Move();
g.DrawImage(Img, X, Y);
}
public void Move()
{
X -= 10;
if (X < 0)
{ Alive = false;
}
}
public void GetImage(int t)
{
Img = Image.FromFile(@"images/" + "roadblock/" + t + ".png");
Width = Img.Width;
Height = Img.Height;
}
private RoadBlock(int x, int y, int speed)
: base(x, y, speed)
{
IfCatch = false;
GetImage(3);
}
public static RoadBlock CreateRoadBlock()
{
int x, y;
x = rd.Next((int)(XMax / 2), XMax);
y = rd.Next(YMin, YMax);
return new RoadBlock(x, y, 10);
}
}
Plane 飛機類
public class Plane : Element
{
public BloodBox blood = new BloodBox(50, 50, 10);
public PlaneDirction dir { get; set; }
/// <summary>
/// 血量值
/// </summary>
public int bloodvalue { get; set; }
/// <summary>
/// 飛機的種類
/// </summary>
int type;
public int Type
{
get { return type; }
set { type = value; }
}
public Bullet Fire()
{
Bullet bl = new Bullet(X+78, Y+40, 1);
return bl;
}
public void Move(PlaneDirction pdir)
{
if (pdir == PlaneDirction.Up)
{
Y -= 40;
}
else
{
Y += 2;
}
//超出邊界檢測
if (X < 0) X = 0;
if (Y < 0) Y = 0;
if (X + this.Width > FrmMain.GAMEWIDTH )
X = FrmMain.GAMEWIDTH - this.Width;
if (Y + this.Height > FrmMain.GAMEHEIGHT)
Y = FrmMain.GAMEHEIGHT - this.Height;
}
public override void Show(Graphics g)
{
Move(PlaneDirction.Down);
g.DrawImage(Img, X, Y);
blood.Draw(g);
}
/// <param name="t">角色類型</param>
public void GetImage(int t)
{
type = t;
Img = Image.FromFile(@"images/plane/" + t + ".png");
Width = Img.Width;
Height = Img.Height;
}
public void Bleeding(int i)
{
bloodvalue -= i;
if (bloodvalue == 0)
{
this.Alive = false;
}
else
{
blood.NowLife = bloodvalue;
}
}
public Plane(int t, int x, int y, int speed)
: base(x, y, speed)
{
bloodvalue = 10;
GetImage(t);
}
}
Bullet 子彈類
public class Bullet : Element
{
public void Move()
{
X += 3;
if (X > FrmMain.GAMEWIDTH)
{ Alive = false; }
}
public override void Show(Graphics g)
{
Move();
g.DrawImage(Img, X, Y);
}
public void GetImage()
{
Img = Image.FromFile(@"images/bullet/" + "2.png");
Width = Img.Width;
Height = Img.Height;
}
public Bullet(int x, int y, int speed)
: base(x, y, speed)
{
GetImage();
}
}
BloodBox 血條類
public class BloodBox
{
int x = 0;
int y = 0;
//血條的單位長度
private const int WIDTH = 1;
//血條的高度
private const int HEIGHT = 10;
private int allLife, nowLife;
public BloodBox(int x, int y, int allLife)
{
this.x = x; this.y = y;
this.allLife = allLife;
this.nowLife = allLife;
}
public int NowLife
{
set { nowLife = value; }
}
public void Draw(System.Drawing.Graphics g)
{
g.DrawString("Life:"+nowLife, new Font("Arial", 10f), new SolidBrush(Color.Red), x - 30, y - 3);
g.DrawRectangle(new Pen(Color.Red), x+30, y, WIDTH * allLife, HEIGHT);
g.FillRectangle(new SolidBrush(Color.Red), x+30, y, WIDTH * nowLife, HEIGHT);
}
}
}
Controler 游戲全局控制類
public class Controler
{
public List<RoadBlock> rb { get; set; }
public List<Bullet> bl { get; set; }
public Plane p { get; set; }
public int score { get; set; }
public string Name { get; set; }
public Controler(int type)
{
Name = "無名";
rb = new List<RoadBlock>();
bl = new List<Bullet>();
p = new Plane(type, 50, 50, 0);
}
public bool Draw(Graphics g)
{
if (p.Alive == false)
{
return false;
}
else
{
p.Show(g);
}
for (int i = 0; i < rb.Count; i++)
{
if (rb[i].Alive == false)
{
if (rb[i].IfCatch == true)
{ score++; }
rb.Remove(rb[i]);
}
else
{
rb[i].Show(g);
}
}
for (int i = 0; i < bl.Count; i++)
{
if (bl[i].Alive == false)
{
bl.Remove(bl[i]);
}
else
{
bl[i].Show(g);
}
}
return true;
}
public void DoHitCheck()
{
if (p.Alive)
{
//檢測飛機與障礙物是否相交
for (int i = 0; i < rb.Count; i++)
{
if (p.GetScope().IntersectsWith(rb[i].GetScope()))
{
p.Bleeding(1);
rb[i].Alive = false;
}
}
//檢測障礙物與子彈是否相交
for (int i = 0; i < bl.Count; i++)
{
for (int j = 0; j < rb.Count; j++)
{
if (bl[i].GetScope().IntersectsWith(rb[j].GetScope()))
{
rb[j].IfCatch = true;
rb[j].Alive = false;
bl[i].Alive = false;
}
}
}
}
}
}
語音控制核心方法
#region Speech Recognition
private void InitializeSpeechRecognitionEngine(string up,string shoot)
{
recognizer.SetInputToDefaultAudioDevice();
Grammar customGrammar = CreateCustomGrammar(up,shoot);
recognizer.UnloadAllGrammars();
recognizer.LoadGrammar(customGrammar);
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);//其次觸發的事件
recognizer.SpeechHypothesized +=
new EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);//首先觸發的事件
}
private Grammar CreateCustomGrammar(string up,string shoot)
{
GrammarBuilder grammarBuilder = new GrammarBuilder();
grammarBuilder.Append(new Choices(up,shoot));
return new Grammar(grammarBuilder);
}
private void TurnSpeechRecognitionOn()
{
recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
private void TurnSpeechRecognitionOff()
{
if (recognizer != null)
{
recognizer.RecognizeAsyncStop();
}
}
private void recognizer_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)//先執行
{
// GuessText(e.Result.Text);
string text = e.Result.Text;
SpeechToAction(text);
}
private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)//后執行
{
}
private void SpeechToAction(string text)
{
if (text == up)
{
ct.p.Move(PlaneDirction.Up);
}
if (text == shoot)
{
ct.bl.Add(ct.p.Fire());
}
}
#endregion
排行榜相關操作
public class Top
{
public string Name { get; set; }
public int Score { get; set; }
}
public class Collection
{
public List<Top> toplist;
public Collection()
{
toplist = new List<Top>();
}
}
public class TopCollection : Collection
{
public void Add(Top value)
{
base.toplist.Add(value);
}
public Top this[int idx]
{
get { return (Top)base.toplist[idx]; }
set { base.toplist[idx] = value; }
}
}
游戲的結構還是很簡單,主要復雜的地方有 語音控制、游戲相關的操作如碰撞等。
以前一直在做C#方面的學習,最近開始接觸HTML5游戲,所以想把自己以前的這個游戲拿出來改成HTML5版本和大家分享。這里是第一篇,后面會逐漸把游戲改成HTML5。
HTML5也是剛剛入門,希望這個系列能是我們大家一起學習的機會,希望大家多多提意見。:)
游戲代碼下載地址 點此下載