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

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

Java中Redisson 的原理深度解析

《Java中Redisson的原理深度解析》Redisson是一个高性能的Redis客户端,它通过将Redis数据结构映射为Java对象和分布式对象,实现了在Java应用中方便地使用Redis,本文... 目录前言一、核心设计理念二、核心架构与通信层1. 基于 Netty 的异步非阻塞通信2. 编解码器三、

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

一篇文章彻底搞懂macOS如何决定java环境

《一篇文章彻底搞懂macOS如何决定java环境》MacOS作为一个功能强大的操作系统,为开发者提供了丰富的开发工具和框架,下面:本文主要介绍macOS如何决定java环境的相关资料,文中通过代码... 目录方法一:使用 which命令方法二:使用 Java_home工具(Apple 官方推荐)那问题来了,

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

Nginx分布式部署流程分析

《Nginx分布式部署流程分析》文章介绍Nginx在分布式部署中的反向代理和负载均衡作用,用于分发请求、减轻服务器压力及解决session共享问题,涵盖配置方法、策略及Java项目应用,并提及分布式事... 目录分布式部署NginxJava中的代理代理分为正向代理和反向代理正向代理反向代理Nginx应用场景

vite搭建vue3项目的搭建步骤

《vite搭建vue3项目的搭建步骤》本文主要介绍了vite搭建vue3项目的搭建步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1.确保Nodejs环境2.使用vite-cli工具3.进入项目安装依赖1.确保Nodejs环境