首先,了解下WinForm做游戏的基本思路:
做游戏需要的最基本的两个元素,一个是屏幕,另一个就是在屏幕的移动的对象了。
然后,了解下parint事件,WinForm的对象都是继承至Control类的,而Control类中包含一个事件PaintEventHandler Paint,paint翻译过来就是喷绘,类似于绘画,当容器刷新时,就等于重新喷绘一次图像,就会触发此事件。
有了这些,就可以开始做游戏了。
先是定义一个元素(本文是小鸡),这个元素包含一张图片,和X坐标和Y坐标,然后将元素按其坐标,添加进屏幕(WinForm窗口或者其他容器,本文使用PictureBox)中,这样就屏幕就会在刚才定义的X坐标和Y坐标处,出现一个元素的图像。
然后,定义一个定时器timer,每30毫秒运行一次,每次运行都要刷新屏幕。自然屏幕刷新就会触发paint事件啦,本文中的paint事件为GamepictureBox_Paint
那么怎么移动小鸡呢?很简单,在定时器timer的事件里(本文为timer1_Tick)将元素的X坐标改变一下就可以了,然后timer里会进行容器刷新,容器刷新就会触发
paint事件,然后在paint事件里,重新定位下小鸡的X坐标就行了。
不多说了,上代码。
Form页面代码如下:
<span >namespace<span > Chicken{
<span >public <span >partial <span >class<span > MyG : Form
{
Element Chicken;<span >//<span >小鸡类
Road GameRoad;<span >//<span >陆块类
<span >public <span >int RoadCount;<span >//<span >陆块数
<span >public <span >int Length;<span >//<span >陆块长度
<span >int EndX;<span >//<span >设置终点X
EventHandler TimerHandler;<span >//<span >时间控制手柄
<span >bool TimerHandlerbool;<span >//<span >是否已传递时间手柄
EventHandler AgainGame;<span >//<span >时间控制手柄
<span >int<span > GamePicX;
<span >int<span > GamePicY;
<span >public<span > MyG()
{
InitializeComponent();
Initial(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">20</span>,Resources.Bird.W<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>th + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">10</span>);<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">陆块长度为小鸡长度加10 50个陆块</span>
<span > }
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> Initial(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> Rcount,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Len) { AgainGame </span>+= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> EventHandler(AgainGame_Start);<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">实例化重新开始手柄</span> TimerHandler += <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> EventHandler(Timer_Enabled);<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">实例化<a href="https://m.jb51.cc/tag/shijian/" target="_blank" >时间</a>手柄</span> RoadCount = Rcount;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">陆块数</span> Length = Len;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">陆块长度</span> TimerHandlerbo<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a> = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">false</span>;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">未已传递<a href="https://m.jb51.cc/tag/shijian/" target="_blank" >时间</a>手柄</span> Chicken = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> Element(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">100</span>-<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">Resources.Bird.Height); GameRoad </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Road(RoadCount,Len); GamePicX </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; GamePicY </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; Point p </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Point(); p.Offset(GamePicX,GamePicY); Gamepicture<a href="https://www.jb51.cc/tag/Box/" target="_blank" >Box</a>.Location </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> p; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> InitialLand(Graphics g) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">Pen pen = new Pen(<a href="https://m.jb51.cc/tag/color/" target="_blank" >color</a>.Green);</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> i = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>; i < GameRoad.<a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>Road.Count; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { RoadItem Item </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> GameRoad.<a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>Road[i]; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (Item.type == <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1</span>)<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">如果类型为1 是陆块是陆地</span>
<span > {
Image <a href="https://m.jb51.cc/tag/img/" target="_blank" >img</a> </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> GameRoad.Land<a href="https://m.jb51.cc/tag/img/" target="_blank" >img</a><a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>[Item.imageIndex]; g.DrawImage(<a href="https://m.jb51.cc/tag/img/" target="_blank" >img</a>,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Rectangle( Item.start.X,Item.end.Y,Item.end.X </span>-<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Item.start.X,<a href="https://m.jb51.cc/tag/img/" target="_blank" >img</a>.Height ) );</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">画陆块</span>
<span > }
}
EndX = GameRoad.ListRoad.ElementAt(RoadCount - <span >1).end.X;<span >//<span >设置终点X
<span >this.GamepictureBox.WIDth =<span > EndX;
}
<span >/// <span >
<span >///<span > 时间控制函数
<span >/// <span >
<span >/// <span ><param name="sender">
<span >/// <span ><param name="e">
<span >private <span >voID Timer_Enabled(<span >object<span > sender,EventArgs e)
{
TimerHandler -= <span >new<span > EventHandler(Timer_Enabled);
timer1.Enabled = <span >false<span >;
Dead D = <span >new<span > Dead(AgainGame);
D.Show();
}
<span >/// <span >
<span >///<span > 游戏开始控制函数
<span >/// <span >
<span >/// <span ><param name="sender">
<span >/// <span ><param name="e">
<span >private <span >voID AgainGame_Start(<span >object<span > sender,EventArgs e)
{
AgainGame -= <span >new<span > EventHandler(AgainGame_Start);
Initial(RoadCount,Length);
timer1.Enabled = <span >true<span >;
}
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> timer1_Tick(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">object</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> sender,EventArgs e) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><a href="https://m.jb51.cc/tag/shezhi/" target="_blank" >设置</a>屏幕移动</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> ((Chicken.x + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.Gamepicture<a href="https://www.jb51.cc/tag/Box/" target="_blank" >Box</a>.Location.X) > <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.W<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>th / <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">2</span> &&<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.Gamepicture<a href="https://www.jb51.cc/tag/Box/" target="_blank" >Box</a>.W<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>th + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.Gamepicture<a href="https://www.jb51.cc/tag/Box/" target="_blank" >Box</a>.Location.X) > <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">.W<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>th) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> OffX = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (Chicken.I<a href="https://www.jb51.cc/tag/ssp/" target="_blank" >ssp</a>eedUp) { OffX </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">2</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } GamePicX </span>= GamePicX -<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> OffX; Point p </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Gamepicture<a href="https://www.jb51.cc/tag/Box/" target="_blank" >Box</a>.Location; p.Offset(GamePicX,GamePicY); Gamepicture<a href="https://www.jb51.cc/tag/Box/" target="_blank" >Box</a>.Location </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> p; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (Chicken.x + Chicken.bmp.W<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>th / <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">2</span> >=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> EndX) { timer1.Enabled </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">false</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; Replay R </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Replay(AgainGame); R.Show(); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> CurrentRoadsIndex = Chicken.x / Length;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><a href="https://www.jb51.cc/tag/huoqu/" target="_blank" >获取</a>当前为第几个陆块</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (CurrentRoadsIndex >= RoadCount) { CurrentRoadsIndex = RoadCount - <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1</span>; }<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">如果大于定义总陆块数 <a href="https://m.jb51.cc/tag/shezhi/" target="_blank" >设置</a>为最大数</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (CurrentRoadsIndex < <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>) { CurrentRoadsIndex = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (GameRoad.<a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>Road.ElementAt(CurrentRoadsIndex).type == <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>)<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">如果当前陆块为空 </span>
<span > {
<span >//<span > Y坐标等于空陆块Y坐标
<span >if ((Chicken.y + Chicken.bmp.Height) ==<span > GameRoad.ListRoad.ElementAt(CurrentRoadsIndex).start.Y)
{
<span >int DepthEndX = GameRoad.ListRoad.ElementAt(CurrentRoadsIndex).end.X;<span >//<span >X下落点为当前陆块的X
<span >if (CurrentRoadsIndex + <span >1 <= RoadCount - <span >1)<span >//<span >如果下一个陆块存在
<span > {
<span >if (GameRoad.ListRoad.ElementAt(CurrentRoadsIndex + <span >1).type == <span >0)<span >//<span >如果下一个陆块也是空
<span > {
DepthEndX = GameRoad.ListRoad.ElementAt(CurrentRoadsIndex + <span >1).end.X;<span >//<span >X下落点为下一个陆块的X
<span > }
}
<span >if (Chicken.x + Chicken.bmp.WIDth < DepthEndX)<span >//<span >对象的坐标加对象的宽度 小于空陆块的尾坐标
<span > {
Chicken.IsFal<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ng </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">true</span>;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">下降</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (!<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">TimerHandlerbo<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>) { Chicken.GetHandler(TimerHandler);</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">传递<a href="https://m.jb51.cc/tag/shijian/" target="_blank" >时间</a>控制手柄</span> TimerHandlerbo<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a> = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">true</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } } } } Gamepicture<a href="https://www.jb51.cc/tag/Box/" target="_blank" >Box</a>.Refresh(); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> Gamepicture<a href="https://www.jb51.cc/tag/Box/" target="_blank" >Box</a>_Paint(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">object</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> sender,PaintEventArgs e) { Chicken.Draw(e.Graphics); InitialLand(e.Graphics); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> MyG_KeyDown(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">object</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> sender,KeyEventArgs e) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (e.KeyData ==<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Keys.Right) { Chicken.IsRuning </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">true</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (e.KeyData == Keys.Space &&<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Chicken.IsRuning) { Chicken.I<a href="https://www.jb51.cc/tag/ssp/" target="_blank" >ssp</a>eedUp </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">true</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (e.KeyData ==<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Keys.Up) { Chicken.IsJum<a href="https://m.jb51.cc/tag/Ping/" target="_blank" >Ping</a> </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">true</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> CurrentRoadsIndex = Chicken.point.X / Length;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">当前陆块</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (e.KeyData ==<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Keys.<a href="https://m.jb51.cc/tag/left/" target="_blank" >left</a>) { Chicken.Back </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">true</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> MyG_KeyUp(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">object</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> sender,KeyEventArgs e) { Chicken.IsRuning </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">false</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; Chicken.I<a href="https://www.jb51.cc/tag/ssp/" target="_blank" >ssp</a>eedUp </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">false</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; Chicken.Back </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">false</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> Gamepicture<a href="https://www.jb51.cc/tag/Box/" target="_blank" >Box</a>_MouseDown(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">object</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> sender,MouseEventArgs e) { Chicken.x </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> e.X; Chicken.y </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> e.Y; }}
}
元素类,定义几个变量来控制对象,注释还算比较多,就不一一解释了,如下:
<span >namespace<span > Chicken{
<span >class<span > Element
{
<span >public <span >int<span > x;
<span >public <span >int<span > y;
<span >public <span >int JumpHeight = <span >0;<span >//<span >跳跃高度
<span >private <span >bool Jumptop = <span >false;<span >//<span >是否跳到最高点
<span >public <span >int FallHeight = <span >0;<span >//<span >跳跃高度
<span >public <span >bool FallDepth = <span >false;<span >//<span >是否跳到最高点
<span >public <span >int BasicSpeed = <span >1;<span >//<span >基本速度
<span >public <span >bool IsRuning = <span >false;<span >//<span >是否移动
<span >public <span >bool Back = <span >false;<span >//<span >是否后退
<span >public <span >bool IsJumPing = <span >false;<span >//<span >是否跳跃
<span >public <span >bool IsspeedUp = <span >false;<span >//<span >是否加速
<span >public <span >bool IsFalling = <span >false;<span >//<span >是否降落
<span >public Image bmp;<span >//<span >对象图形
<span >public Image img;<span >//<span >对象图形 暂不使用
<span >public Point point;<span >//<span >坐标 暂不使用
<span >public<span > EventHandler TimerHandler;
<span >public Element(<span >int x,<span >int<span > y)
{
bmp =<span > Resources.Bird;
<span >this.x =<span > x;
<span >this.y =<span > y;
}
<span >public Element(<span >int x,<span >int<span > y,Image img)
{
bmp =<span > Resources.Bird;
<span >this.x =<span > x;
<span >this.y =<span > y;
<span >this.img =<span > img;
}
<span >public <span >voID<span > Draw(Graphics G)
{
G.DrawImage(bmp,x,y);
Move();
}
<span >public <span >voID<span > Move()
{
<span >if<span > (IsFalling)
{
IsspeedUp = <span >false<span >;
IsJumPing = <span >false<span >;
IsRuning = <span >false<span >;
<span >if (!<span >FallDepth)
{
<span >this.y += BasicSpeed <span >2<span >;
FallHeight += BasicSpeed <span >2<span >;
}
<span >if (FallHeight == <span >50<span >)
{
FallDepth = <span >true<span >;
IsFalling = <span >false<span >;
TimerHandler(<span >null,<span >null<span >);
}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">如果下降了50 则下降完成 不在下降</span>
<span >
} </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (Back) { bmp </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Resources.BirdBack; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.x -=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> BasicSpeed; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (I<a href="https://www.jb51.cc/tag/ssp/" target="_blank" >ssp</a>eedUp) { bmp </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Resources.Bird; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.x += BasicSpeed*<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">3</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">else</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (IsRuning) { bmp </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Resources.Bird; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.x +=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> BasicSpeed; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (IsJum<a href="https://m.jb51.cc/tag/Ping/" target="_blank" >Ping</a>) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (!<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">Jump<a href="https://m.jb51.cc/tag/top/" target="_blank" >top</a>) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.y += BasicSpeed * (-<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">2</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); JumpHeight </span>+= BasicSpeed * (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">2</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.y += BasicSpeed * <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">2</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; JumpHeight </span>+= BasicSpeed * (-<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">2</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (JumpHeight == <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">30</span>) { Jump<a href="https://m.jb51.cc/tag/top/" target="_blank" >top</a> = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">true</span>; }<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">如果跳跃了30 则跳跃到<a href="https://m.jb51.cc/tag/dingbu/" target="_blank" >顶部</a> 不在上升</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (JumpHeight == <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>) { Jump<a href="https://m.jb51.cc/tag/top/" target="_blank" >top</a> = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">false</span>; IsJum<a href="https://m.jb51.cc/tag/Ping/" target="_blank" >Ping</a> = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">false</span>; }<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">如果回到地面 不在下降 跳跃结束</span>
<span > }
} </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> GetHandler(EventHandler TimerHandler) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.TimerHandler =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> TimerHandler; }}
}
然后,创建陆块类,如下:
<span >using<span > System.Text;<span >using<span > System.Drawing;
<span >using<span > Chicken.PropertIEs;
<span >namespace<span > Chicken
{
<span >class<span > Road
{
<span >private Random rand = <span >new<span > Random();
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a><Image> Land<a href="https://m.jb51.cc/tag/img/" target="_blank" >img</a><a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a> = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a><Image><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">(); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a><RoadItem> <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>Road = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a><RoadItem><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">(); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> RoadY = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">100</span>;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">陆地的Y坐标</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">///</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"><summary></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">///</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> 构建陆地 </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">///</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"></summary></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">///</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"><p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>="Number"></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">陆块<a href="https://www.jb51.cc/tag/shuliang/" target="_blank" >数量</a> 必须大于2</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"></param></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">///</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"><p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>="Length"></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">陆块长度</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"></param></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> Road(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> Number,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Length) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (Length < <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">2</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; RoadItem StartItem </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> RoadItem(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,Length); StartItem.imageIndex </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><a href="https://m.jb51.cc/tag/xuanze/" target="_blank" >选择</a>陆块的图像 0为第<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>平地 1为第二个左倾斜 2为第三个右倾斜</span> StartItem.type = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>Road.Add(StartItem);</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">先<a href="https://www.jb51.cc/tag/tianjia/" target="_blank" >添加</a><a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>陆块 第<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>路块必须是陆地</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> i = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>; i < Number - <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">2</span>; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> Temp = rand.Next(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">3</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> Index = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><a href="https://m.jb51.cc/tag/xuanze/" target="_blank" >选择</a>陆块的图像 0为第<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>平地 1为第二个左倾斜 2为第三个右倾斜 <a href="https://m.jb51.cc/tag/zheli/" target="_blank" >这里</a>暂时不<a href="https://m.jb51.cc/tag/shiyong/" target="_blank" >使用</a></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> Ang = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (Temp == <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { Ang </span>= -<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">20</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; Index </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">2</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">else</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (Temp == <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { Ang </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; Index </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> { Ang </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">20</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; Index </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } RoadItem CItem </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> RoadItem(Ang,Length); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">CItem.imageIndex = Index;<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" >获取</a><a href="https://www.jb51.cc/tag/suiji/" target="_blank" >随机</a>陆块的<a href="https://www.jb51.cc/tag/tupian/" target="_blank" >图片</a> 这样<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" >获取</a>Y值需要写<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>一元一次方程<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" >获取</a></span> CItem.imageIndex = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><a href="https://m.jb51.cc/tag/zheli/" target="_blank" >这里</a><a href="https://m.jb51.cc/tag/shezhi/" target="_blank" >设置</a>全为第<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>图像 这样<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" >获取</a>Y值比较<a href="https://m.jb51.cc/tag/fangbian/" target="_blank" >方便</a></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (rand.Next(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">4</span>) == <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1</span>)<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">4分之1的可能性为空陆块</span> CItem.type = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> CItem.type </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>Road.Add(CItem);</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><a href="https://www.jb51.cc/tag/tianjia/" target="_blank" >添加</a>中<a href="https://www.jb51.cc/tag/jiande/" target="_blank" >间的</a>陆块 <a href="https://www.jb51.cc/tag/tianjia/" target="_blank" >添加</a>进陆块列表</span>
<span > }
RoadItem EndItem </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> RoadItem(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,Length); EndItem.imageIndex </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><a href="https://m.jb51.cc/tag/xuanze/" target="_blank" >选择</a>陆块的图像 0为第<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>平地 1为第二个左倾斜 2为第三个右倾斜 </span> EndItem.type = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>Road.Add(EndItem);</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><a href="https://www.jb51.cc/tag/tianjia/" target="_blank" >添加</a>最后<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>陆块</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> i = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>; i < <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>Road.Count; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { RoadItem DrawItem </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>Road[i]; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (i == <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { DrawItem.start </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> Point(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,RoadY); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> { DrawItem.start </span>= <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>Road[i - <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">].end; } DrawItem.end </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> Point(DrawItem.start.X +<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> DrawItem.length,RoadY); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">为每<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>陆块 定义 起始和终止向量坐标</span>
<span >
Land<a href="https://m.jb51.cc/tag/img/" target="_blank" >img</a><a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>.Add(Resources.land); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">为陆块<a href="https://m.jb51.cc/tag/shiyong/" target="_blank" >使用</a>的<a href="https://www.jb51.cc/tag/tupian/" target="_blank" >图片</a>列表 赋值</span>
<span > }
}
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> RoadItem{ </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> angle; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> length;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">陆块长度</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> type;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">0为空,1为陆地</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> imageIndex = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><a href="https://m.jb51.cc/tag/shiyong/" target="_blank" >使用</a>的<a href="https://www.jb51.cc/tag/tupian/" target="_blank" >图片</a></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">///</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"><summary></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">///</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> 构建路块 </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">///</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"></summary></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">///</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"><p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>="angle"></param></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">///</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"><p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>="length"></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">陆块长度</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"></param></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> RoadItem(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> angle,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> length) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.angle =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> angle; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.length =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> length; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> Point start;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">陆块起始坐标</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> Point end;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">陆块终止坐标</span>
<span >
}
}
辅助类,这两个类是两个窗口,我闲MessageBox不太好看,就换了个窗口,但貌似也没好看到那里去。。。哈哈
<span >namespace<span > Chicken{
<span >public <span >partial <span >class<span > Replay : Form
{
EventHandler Again;
<span >public<span > Replay(EventHandler Again)
{
<span >this.Again =<span > Again;
InitializeComponent();
}
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> <a href="https://m.jb51.cc/tag/button/" target="_blank" >button</a>1_C<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ck(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">object</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> sender,EventArgs e) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">.Close(); Again(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); }}
}
<span >namespace<span > Chicken{
<span >public <span >partial <span >class<span > Dead : Form
{
EventHandler Again;
<span >public<span > Dead(EventHandler Again)
{
<span >this.Again =<span > Again;
InitializeComponent();
}
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> <a href="https://m.jb51.cc/tag/button/" target="_blank" >button</a>1_C<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ck(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">object</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> sender,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); }}
}
这两个类是死亡窗口和重新开始窗口。源代码下载地址href="http://download.csdn.net/detail/kiba518/4355712">http://download.csdn.net/detail/kiba518/4355712
源代码中,和文中的代码稍微有点不一样,如果我记得没错是这里,如下:
= EndX)
是修改,如果鸡身的一半以上超过终点,到达终点,游戏结束。这个源码上传时没修改这里。
不过不影响运行啦,但是还有一些小BUG。。
如果想升级这个游戏也很简答,比如,定义一个炮d类,随机发一个。
当炮d的矩形和小鸡的矩形相碰撞了,就死亡啦,矩形相撞有函数的,有兴趣的朋友可以自己扩展。
补充:上是跳跃,左右可以移动,空格是加速,鼠标全屏飞。。。。
开发环境:VS2008。
代码很简单,可以复制到别的环境中运行。
总结以上是内存溢出为你收集整理的C# WinForm 开发游戏——小鸡快跑全部内容,希望文章能够帮你解决C# WinForm 开发游戏——小鸡快跑所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)