java声控框架,[Java教程]声控飞机游戏移植HTML5《一》winform版本介绍与代码分析

编程入门 行业动态 更新时间:2024-10-12 03:19:06

java声控<a href=https://www.elefans.com/category/jswz/34/1770644.html style=框架,[Java教程]声控飞机游戏移植HTML5《一》winform版本介绍与代码分析"/>

java声控框架,[Java教程]声控飞机游戏移植HTML5《一》winform版本介绍与代码分析

声控飞机游戏移植HTML5《一》winform版本介绍与代码分析

2011-12-30

0

游戏介绍

这是一个声控飞机游戏,具体玩法模仿iphone上面的ahhhpa,飞机会自动下降,你可以声控飞机上升与发射子弹,不过这里声控自己可以设置,支持中文和英文。

详细介绍

Element  游戏元素类,提供游戏元素基本操作与属性 public abstract class Element { #region 字段 /// /// 元素坐标X /// private int x; /// /// 元素坐标Y /// private int y; /// /// 是否还“活着” /// private bool alive = true; /// /// 显示用的图片 /// private Image img; /// /// 移动的速度 /// private int speed; /// /// 宽度 /// private int width; /// /// 高度 /// private int height; #endregion #region 属性 public int Width { get { return width; } set { width = value; } } public int Height { get { return height; } set { height = value; } } /// /// 显示用的图片 /// 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; } } /// /// 元素坐标X /// public int X { get { return x; } set { x = value; } } /// /// 元素坐标Y /// public int Y { get { return y; } set { y = value; } } #endregion #region 函数 /// /// 显示自己 /// /// public abstract void Show(Graphics g); /// /// 获取自己的范围 /// public Rectangle GetScope() { Rectangle rt = new Rectangle(x, y, width, height); return rt; } /// /// 构造函数 /// /// x坐标 /// y坐标 public Element(int x, int y, int speed) { this.x = x; this.y = y; this.speed = speed; this.alive = true; } /// /// 死亡 /// 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; } /// /// 血量值 /// public int bloodvalue { get; set; } /// /// 飞机的种类 /// 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); } /// 角色类型 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 rb { get; set; } public List 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(); bl = new List(); 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(recognizer_SpeechRecognized);//其次触发的事件 recognizer.SpeechHypothesized += new EventHandler(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 toplist; public Collection() { toplist = new List(); } } 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也是刚刚入门,希望这个系列能是我们大家一起学习的机会,希望大家多多提意见。:)

游戏代码下载地址    点此下载

0

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:admin@shaoqun。

更多推荐

java声控框架,[Java教程]声控飞机游戏移植HTML5《一》winform版本介绍与代码分析

本文发布于:2024-02-05 09:37:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1744731.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   飞机   版本   代码   教程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!