你的位置:首页 > Java教程

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

游戏介绍

image

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

详细介绍

ClassDiagram

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也是刚刚入门,希望这个系列能是我们大家一起学习的机会,希望大家多多提意见。:)

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