int groundX = 0;//底面的x坐标 /*初始化游戏*/ public void InitialGame() { SingleObject.GetSingle().AddGameObject(new Bird(50, 200, 0)); pipeUp = new Pipe(100, -600, pipeDirection.Up); pipeDown = new Pipe(100, 400, pipeDirection.Down); d = () => { }; //用匿名函数初始化委托 d += GroundMove; //地面动 }
private void Form1_MouseDown(object sender, MouseEventArgs e) { /*点击鼠标左键移动*/ System.Media.SoundPlayer sndPlayer = new System.Media.SoundPlayer(Application.StartupPath + @"/Sound/sfx_wing.wav"); sndPlayer.Play(); SingleObject.GetSingle().singleBird.currentSpeed = 10f;//首先设置当前速度 SingleObject.GetSingle().Move(); }
/*使控件的整个画面无效并重新绘制窗口,而重新绘制窗口时会调用Draw方法使动画播放*/ private void timer1_Tick(object sender, EventArgs e) { this.Invalidate(); /*判断是否游戏结束并且小鸟落地,落地就停止timer,让小鸟翅膀停止播放*/ if (gameOver == true && SingleObject.GetSingle().singleBird.y >= 510) { timer1.Stop(); timer2.Stop(); } }
/*分数增加*/ private void GradeAdd() { if (SingleObject.GetSingle().singleBird.x == pipeDown.x) { grade++; System.Media.SoundPlayer sndPlayer = new System.Media.SoundPlayer(Application.StartupPath + @"/Sound/sfx_point.wav"); sndPlayer.Play(); /*显示分数*/ this.label2.Text = grade.ToString(); } }
private void Form1_KeyDown(object sender, KeyEventArgs e) { /*按下空格键移动*/ System.Media.SoundPlayer sndPlayer = new System.Media.SoundPlayer(Application.StartupPath + @"/Sound/sfx_wing.wav"); sndPlayer.Play(); if (e.KeyCode == Keys.Space) { SingleObject.GetSingle().singleBird.currentSpeed = 10f;//首先设置当前速度 SingleObject.GetSingle().Move(); } }
/*绘制窗体事件*/ private void Form_Paint(object sender, PaintEventArgs e) { /*绘制窗体的时候,画鸟*/ SingleObject.GetSingle().DrawGameObject(e.Graphics); /*绘制管道*/ pipeDown.DrawPipe(e.Graphics); pipeUp.DrawPipe(e.Graphics); /*地面移动*/ e.Graphics.DrawImage(groundImg, groundX, 540); d();//改变groundX的值 }
private void timer3_Tick(object sender, EventArgs e) { PipeMove(); //管道移动 GradeAdd(); //分数增加 /*碰撞检测,检测鸟和管道的矩形是否相交*/ var pipeUpJudge = new Rectangle(pipeUp.x + 10, 0, pipeUp.pipeWidth, pipeUp.pipeHeight - 37); var pipeDownJudge = new Rectangle(pipeDown.x + 10, pipeDown.y + 20, pipeDown.pipeWidth, pipeDown.pipeHeight); var birdJudge = SingleObject.GetSingle().singleBird.GetRectangle(); if (pipeUpJudge.IntersectsWith(birdJudge) || pipeDownJudge.IntersectsWith(birdJudge)) { GameOver(); } }
/*控制小鸟下落的重复执行事件*/ private void timer2_Tick(object sender, EventArgs e) { //首先要获得小鸟下降的高度 var height = Gravity.GetHeight(SingleObject.GetSingle().singleBird.currentSpeed, SingleObject.GetSingle().singleBird.durationTime * 0.001f);//将毫秒转换成帧 Time.DeltaTime //获得小鸟下落后的新坐标 int y = SingleObject.GetSingle().singleBird.y + (int)height; //对y做限定 //限定了小鸟的y坐标 最高就顶着窗体的头 y = y < 0 ? 0 : y; //限定小鸟 不让他掉落到地面的下面 y = y > 510 ? 510 : y; //将新坐标y,赋值给小鸟的坐标 SingleObject.GetSingle().singleBird.y = y; //v=v0+at; SingleObject.GetSingle().singleBird.currentSpeed = SingleObject.GetSingle().singleBird.currentSpeed + SingleObject.GetSingle().singleBird.durationTime *Gravity._g * 0.001f; //当我们点击鼠标或者敲击空格的时候 应该让小鸟有一个向上的固定值 }
/*重新开始*/ private void restartBtn_Click(object sender, EventArgs e) { System.Media.SoundPlayer sndPlayer = new System.Media.SoundPlayer(Application.StartupPath + @"/Sound/btn_press.wav"); sndPlayer.Play(); /*增加点击和按键事件*/ SingleObject.GetSingle().singleBird.x = 50; SingleObject.GetSingle().singleBird.y = 200; pipeDown.x = 300; pipeUp.x = 300; InitialGame(); this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown); this.timer3.Start(); this.timer2.Start(); this.timer1.Start(); gameOver = false; /*移除gameover和按钮*/ this.Controls.Remove(restartBtn); this.Controls.Remove(overLabel); /*分数归0*/ grade = 0; this.label2.Text = "0"; }