安康金利来蛋糕店在线订购预定速递鲜花生日 http://www.jinghaizx.com/jhxzp/10542.html身为漫威迷,最近又把《钢铁侠》和《复仇者联盟》系列又重温了一遍,真的是印证了那句话:“读书百遍,其意自现”。看电影一个道理,每看一遍,都有不懂的感受~不知道大伙是不是也有同样的感受,对于好的电影,真的是回味无穷!
本篇博文也是因《复仇者联盟1》的启发,C#语言实现的一个小游戏,所以游戏命名就叫“钢铁侠VS太空侵略者》了!
先上一个游戏原型图:
TalkisCheap,ShowmetheCode!
代码方面没有太难的操作,主要依赖于Timer控件:
分别用来监控游戏中Ironman子弹移动,侵略者左右移动,往下移动,侵略者子弹移动,子弹碰撞,以及观察者监控(主要校验生命值),具体代码如下:
侵略者界面生成:
privatevoidCreateControl(Formp){PictureBoxpb=newPictureBox();pb.Location=newPoint(x,y);pb.Size=newSize(width,height);pb.BackgroundImage=Properties.Resources.invader;pb.BackgroundImageLayout=ImageLayout.Stretch;pb.Name="Alien";p.Controls.Add(pb);}publicvoidCreateSprites(Formp){for(inti=0;irows;i++){for(intj=0;jcolumns;j++){CreateControl(p);x+=width+space;}y+=height+space;x=;}}
键盘事件绑定:
privatevoidPressed(objectsender,KeyEventArgse){if(e.KeyCode==Keys.A
e.KeyCode==Keys.Left){moveLeft=true;}elseif(e.KeyCode==Keys.D
e.KeyCode==Keys.Right){moveRight=true;}elseif(e.KeyCode==Keys.Spacegame!fired){Missile();fired=true;}}privatevoidReleased(objectsender,KeyEventArgse){if(e.KeyCode==Keys.A
e.KeyCode==Keys.Left){moveLeft=false;}elseif(e.KeyCode==Keys.D
e.KeyCode==Keys.Right){moveRight=false;}elseif(e.KeyCode==Keys.Space){fired=false;}}
Ironman左右移动:
privatevoidPlayerMove(objectsender,EventArgse){if(moveLeftPlayer.Location.X=0){Player.Left--;}elseif(moveRightPlayer.Location.X=limit){Player.Left++;}}
子弹发射:
privatevoidFireBullet(objectsender,EventArgse){foreach(Controlcinthis.Controls){if(cisPictureBoxc.Name=="Bullet"){PictureBoxbullet=(PictureBox)c;bullet.Top-=5;if(bullet.Location.Y=0){this.Controls.Remove(bullet);}foreach(Controlctinthis.Controls){if(ctisPictureBoxct.Name=="Laser"){PictureBoxlaser=(PictureBox)ct;if(bullet.Bounds.IntersectsWith(laser.Bounds)){this.Controls.Remove(bullet);this.Controls.Remove(laser);pts++;Score(pts);}}}foreach(Controlctrlinthis.Controls){if(ctrlisPictureBoxctrl.Name=="Alien"){PictureBoxalien=(PictureBox)ctrl;if(bullet.Bounds.IntersectsWith(alien.Bounds)!Touched(alien)){this.Controls.Remove(bullet);this.Controls.Remove(alien);aliens.Remove(alien);pts+=5;Score(pts);CheckForWinner();}elseif(bullet.Bounds.IntersectsWith(alien.Bounds)Touched(alien)){this.Controls.Remove(bullet);this.Controls.Remove(alien);delay.Add(alien);pts+=5;Score(pts);CheckForWinner();}}}}}}
子弹
privatevoidMissile(){PictureBoxbullet=newPictureBox();bullet.Location=newPoint(Player.Location.X+Player.Width/2,Player.Location.Y-20);bullet.Size=newSize(5,20);bullet.BackgroundImage=Properties.Resources.bullet;bullet.BackgroundImageLayout=ImageLayout.Stretch;bullet.Name="Bullet";this.Controls.Add(bullet);}
侵略者移动:
privatevoidAlienMove(){foreach(PictureBoxalieninaliens){alien.Location=newPoint(alien.Location.X+left,alien.Location.Y+top);SetDirection(alien);Collided(alien);}}privatevoidCollided(PictureBoxa){if(a.Bounds.IntersectsWith(Player.Bounds)){gameOver();}}
子弹移动效果:
privatevoidBeam(PictureBoxa){PictureBoxlaser=newPictureBox();laser.Location=newPoint(a.Location.X+a.Width/3,a.Location.Y+20);laser.Size=newSize(5,20);laser.BackgroundImage=Properties.Resources.laser;laser.BackgroundImageLayout=ImageLayout.Stretch;laser.Name="Laser";this.Controls.Add(laser);}privatevoidStrikeSpan(objectsender,EventArgse){Randomr=newRandom();intpick;if(aliens.Count0){pick=r.Next(aliens.Count);Beam(aliens[pick]);}}privatevoidDetectLaser(objectsender,EventArgse){foreach(Controlcinthis.Controls){if(cisPictureBoxc.Name=="Laser"){PictureBoxlaser=(PictureBox)c;laser.Top+=5;if(laser.Location.Y=limit){this.Controls.Remove(laser);}if(laser.Bounds.IntersectsWith(Player.Bounds)){this.Controls.Remove(laser);LoseLife();}}}}
主要核心代码如上,下面看下运行效果图:
效果图一
效果图二
何以解忧唯有撸码,欢迎有兴趣的朋友,联系我一起探讨。如需源码,也请联系我,源码免费赠送,最后感谢您的耐心观看,赠人玫瑰,手留余香,觉得本文有些许意思和启发的,记得