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

本文主要是介绍java声控框架,[Java教程]声控飞机游戏移植HTML5《一》winform版本介绍与代码分析_星空网...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

2011-12-30

0

游戏介绍

bc91bb04e6e9c61e24c974e4440db8f2.gif

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

详细介绍

bc91bb04e6e9c61e24c974e4440db8f2.gif

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.com。

这篇关于java声控框架,[Java教程]声控飞机游戏移植HTML5《一》winform版本介绍与代码分析_星空网...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/430227

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

Python一次性将指定版本所有包上传PyPI镜像解决方案

《Python一次性将指定版本所有包上传PyPI镜像解决方案》本文主要介绍了一个安全、完整、可离线部署的解决方案,用于一次性准备指定Python版本的所有包,然后导出到内网环境,感兴趣的小伙伴可以跟随... 目录为什么需要这个方案完整解决方案1. 项目目录结构2. 创建智能下载脚本3. 创建包清单生成脚本4

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF