C# · 12月 20, 2021

C# WinForm 开发游戏——小鸡快跑

首先,了解下WinForm做游戏的基本思路:

做游戏需要的最基本的两个元素,一个是屏幕,另一个就是在屏幕的移动的对象了。

然后,了解下parint事件,WinForm的对象都是继承至Control类的,而Control类中包含一个事件PaintEventHandler Paint,paint翻译过来就是喷绘,类似于绘画,当容器刷新时,就等于重新喷绘一次图像,就会触发此事件。

有了这些,就可以开始做游戏了。

先是定义一个元素(本文是小鸡),这个元素包含一张图片,和X坐标和Y坐标,然后将元素按其坐标,添加进屏幕(WinForm窗口或者其他容器,本文使用Picture@R_106_2419@)中,这样就屏幕就会在刚才定义的X坐标和Y坐标处,出现一个元素的图像。

然后,定义一个定时器timer,每30毫秒运行一次,每次运行都要刷新屏幕。自然屏幕刷新就会触发paint事件啦,本文中的paint事件为Gamepicture@R_106_2419@_Paint

那么怎么移动小鸡呢?很简单,在定时器timer的事件里(本文为timer1_Tick)将元素的X坐标改变一下就可以了,然后timer里会进行容器刷新,容器刷新就会触发

paint事件,然后在paint事件里,重新定位下小鸡的X坐标就行了。

不多说了,上代码。

Form页面代码如下:

<span style=”color: #0000ff;”>namespace<span style=”color: #000000;”> Chicken
{
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>partial <span style=”color: #0000ff;”>class<span style=”color: #000000;”> MyG : Form
{
Element Chicken;<span style=”color: #008000;”>//<span style=”color: #008000;”>小鸡类
Road GameRoad;<span style=”color: #008000;”>//<span style=”color: #008000;”>陆块类
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>int RoadCount;<span style=”color: #008000;”>//<span style=”color: #008000;”>陆块数
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>int Length;<span style=”color: #008000;”>//<span style=”color: #008000;”>陆块长度
<span style=”color: #0000ff;”>int EndX;<span style=”color: #008000;”>//<span style=”color: #008000;”>设置终点X
EventHandler TimerHandler;<span style=”color: #008000;”>//<span style=”color: #008000;”>时间控制手柄
<span style=”color: #0000ff;”>bool TimerHandlerbool;<span style=”color: #008000;”>//<span style=”color: #008000;”>是否已传递时间手柄
EventHandler AgainGame;<span style=”color: #008000;”>//<span style=”color: #008000;”>时间控制手柄
<span style=”color: #0000ff;”>int<span style=”color: #000000;”> GamePicX;
<span style=”color: #0000ff;”>int<span style=”color: #000000;”> GamePicY;
<span style=”color: #0000ff;”>public<span style=”color: #000000;”> MyG()
{
InitializeComponent();

Initial(</span><span style=”color: #800080;”&gt;20</span>,Resources.Bird.Width + <span style=”color: #800080;”&gt;10</span>);<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;陆块长度为小鸡长度加10 50个陆块</span>

<span style=”color: #000000;”> }

</span><span style=”color: #0000ff;”&gt;private</span> <span style=”color: #0000ff;”&gt;void</span> Initial(<span style=”color: #0000ff;”&gt;int</span> Rcount,<span style=”color: #0000ff;”&gt;int</span><span style=”color: #000000;”&gt; Len) { AgainGame </span>+= <span style=”color: #0000ff;”&gt;new</span> EventHandler(AgainGame_Start);<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;实例化重新开始手柄</span> TimerHandler += <span style=”color: #0000ff;”&gt;new</span> EventHandler(Timer_Enabled);<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;实例化时间手柄</span> RoadCount = Rcount;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;陆块数</span> Length = Len;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;陆块长度</span> TimerHandlerbool = <span style=”color: #0000ff;”&gt;false</span>;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;未已传递时间手柄</span> Chicken = <span style=”color: #0000ff;”&gt;new</span> Element(<span style=”color: #800080;”&gt;0</span>,<span style=”color: #800080;”&gt;100</span>-<span style=”color: #000000;”&gt;Resources.Bird.Height); GameRoad </span>= <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; Road(RoadCount,Len); GamePicX </span>= <span style=”color: #800080;”&gt;0</span><span style=”color: #000000;”&gt;; GamePicY </span>= <span style=”color: #800080;”&gt;0</span><span style=”color: #000000;”&gt;; Point p </span>= <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; Point(); p.Offset(GamePicX,GamePicY); Gamepicture@R_106_2<a href=”https://www.jb51.cc/tag/419/” target=”_blank” class=”keywords”>419</a>@.Location </span>=<span style=”color: #000000;”&gt; p; } </span><span style=”color: #0000ff;”&gt;private</span> <span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; InitialLand(Graphics g) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;Pen pen = new Pen(Color.Green);</span> <span style=”color: #0000ff;”&gt;for</span> (<span style=”color: #0000ff;”&gt;int</span> i = <span style=”color: #800080;”&gt;0</span>; i < GameRoad.ListRoad.Count; i++<span style=”color: #000000;”&gt;) { RoadItem Item </span>=<span style=”color: #000000;”&gt; GameRoad.ListRoad[i]; </span><span style=”color: #0000ff;”&gt;if</span> (Item.type == <span style=”color: #800080;”&gt;1</span>)<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;如果类型为1 是陆块是陆地</span>

<span style=”color: #000000;”> {

Image img </span>=<span style=”color: #000000;”&gt; GameRoad.LandImgList[Item.imageIndex]; g.DrawImage(img,</span><span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; Rectangle( Item.start.X,Item.end.Y,Item.end.X </span>-<span style=”color: #000000;”&gt; Item.start.X,img.Height ) );</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;画陆块</span>

<span style=”color: #000000;”> }
}
EndX = GameRoad.ListRoad.ElementAt(RoadCount – <span style=”color: #800080;”>1).end.X;<span style=”color: #008000;”>//<span style=”color: #008000;”>设置终点X
<span style=”color: #0000ff;”>this.Gamepicture@R_106_2419@.Width =<span style=”color: #000000;”> EndX;
}
<span style=”color: #808080;”>/// <span style=”color: #808080;”>
<span style=”color: #808080;”>///<span style=”color: #008000;”> 时间控制函数
<span style=”color: #808080;”>/// <span style=”color: #808080;”>
<span style=”color: #808080;”>/// <span style=”color: #808080;”><param name=”sender”>
<span style=”color: #808080;”>/// <span style=”color: #808080;”><param name=”e”>
<span style=”color: #0000ff;”>private <span style=”color: #0000ff;”>void Timer_Enabled(<span style=”color: #0000ff;”>object<span style=”color: #000000;”> sender,EventArgs e)
{
TimerHandler -= <span style=”color: #0000ff;”>new<span style=”color: #000000;”> EventHandler(Timer_Enabled);
timer1.Enabled = <span style=”color: #0000ff;”>false<span style=”color: #000000;”>;
Dead D = <span style=”color: #0000ff;”>new<span style=”color: #000000;”> Dead(AgainGame);
D.Show();
}
<span style=”color: #808080;”>/// <span style=”color: #808080;”>
<span style=”color: #808080;”>///<span style=”color: #008000;”> 游戏开始控制函数
<span style=”color: #808080;”>/// <span style=”color: #808080;”>
<span style=”color: #808080;”>/// <span style=”color: #808080;”><param name=”sender”>
<span style=”color: #808080;”>/// <span style=”color: #808080;”><param name=”e”>
<span style=”color: #0000ff;”>private <span style=”color: #0000ff;”>void AgainGame_Start(<span style=”color: #0000ff;”>object<span style=”color: #000000;”> sender,EventArgs e)
{
AgainGame -= <span style=”color: #0000ff;”>new<span style=”color: #000000;”> EventHandler(AgainGame_Start);
Initial(RoadCount,Length);
timer1.Enabled = <span style=”color: #0000ff;”>true<span style=”color: #000000;”>;
}

</span><span style=”color: #0000ff;”&gt;private</span> <span style=”color: #0000ff;”&gt;void</span> timer1_Tick(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,EventArgs e) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;设置屏幕移动</span> <span style=”color: #0000ff;”&gt;if</span> ((Chicken.x + <span style=”color: #0000ff;”&gt;this</span>.Gamepicture@R_106_2<a href=”https://www.jb51.cc/tag/419/” target=”_blank” class=”keywords”>419</a>@.Location.X) > <span style=”color: #0000ff;”&gt;this</span>.Width / <span style=”color: #800080;”&gt;2</span> &amp;&amp;<span style=”color: #000000;”&gt; (</span><span style=”color: #0000ff;”&gt;this</span>.Gamepicture@R_106_2<a href=”https://www.jb51.cc/tag/419/” target=”_blank” class=”keywords”>419</a>@.Width + <span style=”color: #0000ff;”&gt;this</span>.Gamepicture@R_106_2<a href=”https://www.jb51.cc/tag/419/” target=”_blank” class=”keywords”>419</a>@.Location.X) > <span style=”color: #0000ff;”&gt;this</span><span style=”color: #000000;”&gt;.Width) { </span><span style=”color: #0000ff;”&gt;int</span> OffX = <span style=”color: #800080;”&gt;1</span><span style=”color: #000000;”&gt;; </span><span style=”color: #0000ff;”&gt;if</span><span style=”color: #000000;”&gt; (Chicken.IsSpeedUp) { OffX </span>= <span style=”color: #800080;”&gt;2</span><span style=”color: #000000;”&gt;; } GamePicX </span>= GamePicX -<span style=”color: #000000;”&gt; OffX; Point p </span>=<span style=”color: #000000;”&gt; Gamepicture@R_106_2<a href=”https://www.jb51.cc/tag/419/” target=”_blank” class=”keywords”>419</a>@.Location; p.Offset(GamePicX,GamePicY); Gamepicture@R_106_2<a href=”https://www.jb51.cc/tag/419/” target=”_blank” class=”keywords”>419</a>@.Location </span>=<span style=”color: #000000;”&gt; p; } </span><span style=”color: #0000ff;”&gt;if</span> (Chicken.x + Chicken.bmp.Width / <span style=”color: #800080;”&gt;2</span> >=<span style=”color: #000000;”&gt; EndX) { timer1.Enabled </span>= <span style=”color: #0000ff;”&gt;false</span><span style=”color: #000000;”&gt;; Replay R </span>= <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; Replay(AgainGame); R.Show(); } </span><span style=”color: #0000ff;”&gt;int</span> CurrentRoadsIndex = Chicken.x / Length;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;<a href=”https://www.jb51.cc/tag/huoqu/” target=”_blank” class=”keywords”>获取</a>当前为第几个陆块</span> <span style=”color: #0000ff;”&gt;if</span> (CurrentRoadsIndex >= RoadCount) { CurrentRoadsIndex = RoadCount – <span style=”color: #800080;”&gt;1</span>; }<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;如果大于定义总陆块数 设置为最大数</span> <span style=”color: #0000ff;”&gt;if</span> (CurrentRoadsIndex < <span style=”color: #800080;”&gt;0</span>) { CurrentRoadsIndex = <span style=”color: #800080;”&gt;0</span><span style=”color: #000000;”&gt;; } </span><span style=”color: #0000ff;”&gt;if</span> (GameRoad.ListRoad.ElementAt(CurrentRoadsIndex).type == <span style=”color: #800080;”&gt;0</span>)<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;如果当前陆块为空 </span>

<span style=”color: #000000;”> {
<span style=”color: #008000;”>//<span style=”color: #008000;”> Y坐标等于空陆块Y坐标
<span style=”color: #0000ff;”>if ((Chicken.y + Chicken.bmp.Height) ==<span style=”color: #000000;”> GameRoad.ListRoad.ElementAt(CurrentRoadsIndex).start.Y)
{
<span style=”color: #0000ff;”>int DepthEndX = GameRoad.ListRoad.ElementAt(CurrentRoadsIndex).end.X;<span style=”color: #008000;”>//<span style=”color: #008000;”>X下落点为当前陆块的X
<span style=”color: #0000ff;”>if (CurrentRoadsIndex + <span style=”color: #800080;”>1 <= RoadCount – <span style=”color: #800080;”>1)<span style=”color: #008000;”>//<span style=”color: #008000;”>如果下一个陆块存在
<span style=”color: #000000;”> {
<span style=”color: #0000ff;”>if (GameRoad.ListRoad.ElementAt(CurrentRoadsIndex + <span style=”color: #800080;”>1).type == <span style=”color: #800080;”>0)<span style=”color: #008000;”>//<span style=”color: #008000;”>如果下一个陆块也是空
<span style=”color: #000000;”> {
DepthEndX = GameRoad.ListRoad.ElementAt(CurrentRoadsIndex + <span style=”color: #800080;”>1).end.X;<span style=”color: #008000;”>//<span style=”color: #008000;”>X下落点为下一个陆块的X
<span style=”color: #000000;”> }
}
<span style=”color: #0000ff;”>if (Chicken.x + Chicken.bmp.Width < DepthEndX)<span style=”color: #008000;”>//<span style=”color: #008000;”>对象的坐标加对象的宽度 小于空陆块的尾坐标
<span style=”color: #000000;”> {

Chicken.IsFalling </span>= <span style=”color: #0000ff;”&gt;true</span>;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;下降</span> <span style=”color: #0000ff;”&gt;if</span> (!<span style=”color: #000000;”&gt;TimerHandlerbool) { Chicken.GetHandler(TimerHandler);</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;传递时间控制手柄</span> TimerHandlerbool = <span style=”color: #0000ff;”&gt;true</span><span style=”color: #000000;”&gt;; } } } } Gamepicture@R_106_2<a href=”https://www.jb51.cc/tag/419/” target=”_blank” class=”keywords”>419</a>@.Refresh(); } </span><span style=”color: #0000ff;”&gt;private</span> <span style=”color: #0000ff;”&gt;void</span> Gamepicture@R_106_2<a href=”https://www.jb51.cc/tag/419/” target=”_blank” class=”keywords”>419</a>@_Paint(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,PaintEventArgs e) { Chicken.Draw(e.Graphics); InitialLand(e.Graphics); } </span><span style=”color: #0000ff;”&gt;private</span> <span style=”color: #0000ff;”&gt;void</span> MyG_KeyDown(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,KeyEventArgs e) { </span><span style=”color: #0000ff;”&gt;if</span> (e.KeyData ==<span style=”color: #000000;”&gt; Keys.Right) { Chicken.IsRuning </span>= <span style=”color: #0000ff;”&gt;true</span><span style=”color: #000000;”&gt;; } </span><span style=”color: #0000ff;”&gt;if</span> (e.KeyData == Keys.Space &amp;&amp;<span style=”color: #000000;”&gt; Chicken.IsRuning) { Chicken.IsSpeedUp </span>= <span style=”color: #0000ff;”&gt;true</span><span style=”color: #000000;”&gt;; } </span><span style=”color: #0000ff;”&gt;if</span> (e.KeyData ==<span style=”color: #000000;”&gt; Keys.Up) { Chicken.IsJumping </span>= <span style=”color: #0000ff;”&gt;true</span><span style=”color: #000000;”&gt;; } </span><span style=”color: #0000ff;”&gt;int</span> CurrentRoadsIndex = Chicken.point.X / Length;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;当前陆块</span> <span style=”color: #0000ff;”&gt;if</span> (e.KeyData ==<span style=”color: #000000;”&gt; Keys.Left) { Chicken.Back </span>= <span style=”color: #0000ff;”&gt;true</span><span style=”color: #000000;”&gt;; } } </span><span style=”color: #0000ff;”&gt;private</span> <span style=”color: #0000ff;”&gt;void</span> MyG_KeyUp(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,KeyEventArgs e) { Chicken.IsRuning </span>= <span style=”color: #0000ff;”&gt;false</span><span style=”color: #000000;”&gt;; Chicken.IsSpeedUp </span>= <span style=”color: #0000ff;”&gt;false</span><span style=”color: #000000;”&gt;; Chicken.Back </span>= <span style=”color: #0000ff;”&gt;false</span><span style=”color: #000000;”&gt;; } </span><span style=”color: #0000ff;”&gt;private</span> <span style=”color: #0000ff;”&gt;void</span> Gamepicture@R_106_2<a href=”https://www.jb51.cc/tag/419/” target=”_blank” class=”keywords”>419</a>@_MouseDown(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,MouseEventArgs e) { Chicken.x </span>=<span style=”color: #000000;”&gt; e.X; Chicken.y </span>=<span style=”color: #000000;”&gt; e.Y; }}

}

元素类,定义几个变量来控制对象,注释还算比较多,就不一一解释了,如下:

<span style=”color: #0000ff;”>namespace<span style=”color: #000000;”> Chicken
{
<span style=”color: #0000ff;”>class<span style=”color: #000000;”> Element
{
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>int<span style=”color: #000000;”> x;
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>int<span style=”color: #000000;”> y;
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>int JumpHeight = <span style=”color: #800080;”>0;<span style=”color: #008000;”>//<span style=”color: #008000;”>跳跃高度
<span style=”color: #0000ff;”>private <span style=”color: #0000ff;”>bool JumpTop = <span style=”color: #0000ff;”>false;<span style=”color: #008000;”>//<span style=”color: #008000;”>是否跳到最高点
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>int FallHeight = <span style=”color: #800080;”>0;<span style=”color: #008000;”>//<span style=”color: #008000;”>跳跃高度
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>bool FallDepth = <span style=”color: #0000ff;”>false;<span style=”color: #008000;”>//<span style=”color: #008000;”>是否跳到最高点
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>int BasicSpeed = <span style=”color: #800080;”>1;<span style=”color: #008000;”>//<span style=”color: #008000;”>基本速度
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>bool IsRuning = <span style=”color: #0000ff;”>false;<span style=”color: #008000;”>//<span style=”color: #008000;”>是否移动
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>bool Back = <span style=”color: #0000ff;”>false;<span style=”color: #008000;”>//<span style=”color: #008000;”>是否后退
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>bool IsJumping = <span style=”color: #0000ff;”>false;<span style=”color: #008000;”>//<span style=”color: #008000;”>是否跳跃
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>bool IsSpeedUp = <span style=”color: #0000ff;”>false;<span style=”color: #008000;”>//<span style=”color: #008000;”>是否加速
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>bool IsFalling = <span style=”color: #0000ff;”>false;<span style=”color: #008000;”>//<span style=”color: #008000;”>是否降落
<span style=”color: #0000ff;”>public Image bmp;<span style=”color: #008000;”>//<span style=”color: #008000;”>对象图形
<span style=”color: #0000ff;”>public Image img;<span style=”color: #008000;”>//<span style=”color: #008000;”>对象图形 暂不使用
<span style=”color: #0000ff;”>public Point point;<span style=”color: #008000;”>//<span style=”color: #008000;”>坐标 暂不使用
<span style=”color: #0000ff;”>public<span style=”color: #000000;”> EventHandler TimerHandler;
<span style=”color: #0000ff;”>public Element(<span style=”color: #0000ff;”>int x,<span style=”color: #0000ff;”>int<span style=”color: #000000;”> y)
{
bmp =<span style=”color: #000000;”> Resources.Bird;
<span style=”color: #0000ff;”>this.x =<span style=”color: #000000;”> x;
<span style=”color: #0000ff;”>this.y =<span style=”color: #000000;”> y;
}
<span style=”color: #0000ff;”>public Element(<span style=”color: #0000ff;”>int x,<span style=”color: #0000ff;”>int<span style=”color: #000000;”> y,Image img)
{
bmp =<span style=”color: #000000;”> Resources.Bird;
<span style=”color: #0000ff;”>this.x =<span style=”color: #000000;”> x;
<span style=”color: #0000ff;”>this.y =<span style=”color: #000000;”> y;
<span style=”color: #0000ff;”>this.img =<span style=”color: #000000;”> img;
}
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>void<span style=”color: #000000;”> Draw(Graphics G)
{
G.DrawImage(bmp,x,y);
Move();
}
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>void<span style=”color: #000000;”> Move()
{
<span style=”color: #0000ff;”>if<span style=”color: #000000;”> (IsFalling)
{
IsSpeedUp = <span style=”color: #0000ff;”>false<span style=”color: #000000;”>;
IsJumping = <span style=”color: #0000ff;”>false<span style=”color: #000000;”>;
IsRuning = <span style=”color: #0000ff;”>false<span style=”color: #000000;”>;
<span style=”color: #0000ff;”>if (!<span style=”color: #000000;”>FallDepth)
{
<span style=”color: #0000ff;”>this.y += BasicSpeed <span style=”color: #800080;”>2<span style=”color: #000000;”>;
FallHeight += BasicSpeed <span style=”color: #800080;”>2<span style=”color: #000000;”>;
}
<span style=”color: #0000ff;”>if (FallHeight == <span style=”color: #800080;”>50<span style=”color: #000000;”>)
{
FallDepth = <span style=”color: #0000ff;”>true<span style=”color: #000000;”>;
IsFalling = <span style=”color: #0000ff;”>false<span style=”color: #000000;”>;
TimerHandler(<span style=”color: #0000ff;”>null,<span style=”color: #0000ff;”>null<span style=”color: #000000;”>);

}</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;如果下降了50 则下降完成 不在下降</span>

<span style=”color: #000000;”>

} </span><span style=”color: #0000ff;”&gt;if</span><span style=”color: #000000;”&gt; (Back) { bmp </span>=<span style=”color: #000000;”&gt; Resources.BirdBack; </span><span style=”color: #0000ff;”&gt;this</span>.x -=<span style=”color: #000000;”&gt; BasicSpeed; } </span><span style=”color: #0000ff;”&gt;if</span><span style=”color: #000000;”&gt; (IsSpeedUp) { bmp </span>=<span style=”color: #000000;”&gt; Resources.Bird; </span><span style=”color: #0000ff;”&gt;this</span>.x += BasicSpeed*<span style=”color: #800080;”&gt;3</span><span style=”color: #000000;”&gt;; } </span><span style=”color: #0000ff;”&gt;else</span> <span style=”color: #0000ff;”&gt;if</span><span style=”color: #000000;”&gt; (IsRuning) { bmp </span>=<span style=”color: #000000;”&gt; Resources.Bird; </span><span style=”color: #0000ff;”&gt;this</span>.x +=<span style=”color: #000000;”&gt; BasicSpeed; } </span><span style=”color: #0000ff;”&gt;if</span><span style=”color: #000000;”&gt; (IsJumping) { </span><span style=”color: #0000ff;”&gt;if</span> (!<span style=”color: #000000;”&gt;JumpTop) { </span><span style=”color: #0000ff;”&gt;this</span>.y += BasicSpeed * (-<span style=”color: #800080;”&gt;2</span><span style=”color: #000000;”&gt;); JumpHeight </span>+= BasicSpeed * (<span style=”color: #800080;”&gt;2</span><span style=”color: #000000;”&gt;); } </span><span style=”color: #0000ff;”&gt;else</span><span style=”color: #000000;”&gt; { </span><span style=”color: #0000ff;”&gt;this</span>.y += BasicSpeed * <span style=”color: #800080;”&gt;2</span><span style=”color: #000000;”&gt;; JumpHeight </span>+= BasicSpeed * (-<span style=”color: #800080;”&gt;2</span><span style=”color: #000000;”&gt;); } </span><span style=”color: #0000ff;”&gt;if</span> (JumpHeight == <span style=”color: #800080;”&gt;30</span>) { JumpTop = <span style=”color: #0000ff;”&gt;true</span>; }<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;如果跳跃了30 则跳跃到顶部 不在上升</span> <span style=”color: #0000ff;”&gt;if</span> (JumpHeight == <span style=”color: #800080;”&gt;0</span>) { JumpTop = <span style=”color: #0000ff;”&gt;false</span>; IsJumping = <span style=”color: #0000ff;”&gt;false</span>; }<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;如果回到地面 不在下降 跳跃结束</span>

<span style=”color: #000000;”> }

} </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; GetHandler(EventHandler TimerHandler) { </span><span style=”color: #0000ff;”&gt;this</span>.TimerHandler =<span style=”color: #000000;”&gt; TimerHandler; }}

}

然后,创建陆块类,如下:

<span style=”color: #0000ff;”>using<span style=”color: #000000;”> System.Text;
<span style=”color: #0000ff;”>using<span style=”color: #000000;”> System.Drawing;
<span style=”color: #0000ff;”>using<span style=”color: #000000;”> Chicken.Properties;

<span style=”color: #0000ff;”>namespace<span style=”color: #000000;”> Chicken
{
<span style=”color: #0000ff;”>class<span style=”color: #000000;”> Road
{
<span style=”color: #0000ff;”>private Random rand = <span style=”color: #0000ff;”>new<span style=”color: #000000;”> Random();

</span><span style=”color: #0000ff;”&gt;public</span> List<Image> LandImgList = <span style=”color: #0000ff;”&gt;new</span> List<Image><span style=”color: #000000;”&gt;(); </span><span style=”color: #0000ff;”&gt;public</span> List<RoadItem> ListRoad = <span style=”color: #0000ff;”&gt;new</span> List<RoadItem><span style=”color: #000000;”&gt;(); </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;int</span> RoadY = <span style=”color: #800080;”&gt;100</span>;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;陆地的Y坐标</span> <span style=”color: #808080;”&gt;///</span> <span style=”color: #808080;”&gt;<summary></span> <span style=”color: #808080;”&gt;///</span><span style=”color: #008000;”&gt; 构建陆地 </span><span style=”color: #808080;”&gt;///</span> <span style=”color: #808080;”&gt;</summary></span> <span style=”color: #808080;”&gt;///</span> <span style=”color: #808080;”&gt;<param name=”Number”&gt;</span><span style=”color: #008000;”&gt;陆块<a href=”https://www.jb51.cc/tag/shuliang/” target=”_blank” class=”keywords”>数量</a> 必须大于2</span><span style=”color: #808080;”&gt;</param></span> <span style=”color: #808080;”&gt;///</span> <span style=”color: #808080;”&gt;<param name=”Length”&gt;</span><span style=”color: #008000;”&gt;陆块长度</span><span style=”color: #808080;”&gt;</param></span> <span style=”color: #0000ff;”&gt;public</span> Road(<span style=”color: #0000ff;”&gt;int</span> Number,<span style=”color: #0000ff;”&gt;int</span><span style=”color: #000000;”&gt; Length) { </span><span style=”color: #0000ff;”&gt;if</span> (Length < <span style=”color: #800080;”&gt;2</span><span style=”color: #000000;”&gt;) </span><span style=”color: #0000ff;”&gt;return</span><span style=”color: #000000;”&gt;; RoadItem StartItem </span>= <span style=”color: #0000ff;”&gt;new</span> RoadItem(<span style=”color: #800080;”&gt;0</span><span style=”color: #000000;”&gt;,Length); StartItem.imageIndex </span>= <span style=”color: #800080;”&gt;0</span>;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;选择陆块的图像 0为第<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>平地 1为第二个左倾斜 2为第三个右倾斜</span> StartItem.type = <span style=”color: #800080;”&gt;1</span><span style=”color: #000000;”&gt;; ListRoad.Add(StartItem);</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;先<a href=”https://www.jb51.cc/tag/tianjia/” target=”_blank” class=”keywords”>添加</a><a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>陆块 第<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>路块必须是陆地</span> <span style=”color: #0000ff;”&gt;for</span> (<span style=”color: #0000ff;”&gt;int</span> i = <span style=”color: #800080;”&gt;0</span>; i < Number – <span style=”color: #800080;”&gt;2</span>; i++<span style=”color: #000000;”&gt;) { </span><span style=”color: #0000ff;”&gt;int</span> Temp = rand.Next(<span style=”color: #800080;”&gt;0</span>,<span style=”color: #800080;”&gt;3</span><span style=”color: #000000;”&gt;); </span><span style=”color: #0000ff;”&gt;int</span> Index = <span style=”color: #800080;”&gt;0</span>;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;选择陆块的图像 0为第<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>平地 1为第二个左倾斜 2为第三个右倾斜 这里暂时不使用</span> <span style=”color: #0000ff;”&gt;int</span> Ang = <span style=”color: #800080;”&gt;0</span><span style=”color: #000000;”&gt;; </span><span style=”color: #0000ff;”&gt;if</span> (Temp == <span style=”color: #800080;”&gt;0</span><span style=”color: #000000;”&gt;) { Ang </span>= -<span style=”color: #800080;”&gt;20</span><span style=”color: #000000;”&gt;; Index </span>= <span style=”color: #800080;”&gt;2</span><span style=”color: #000000;”&gt;; } </span><span style=”color: #0000ff;”&gt;else</span> <span style=”color: #0000ff;”&gt;if</span> (Temp == <span style=”color: #800080;”&gt;1</span><span style=”color: #000000;”&gt;) { Ang </span>= <span style=”color: #800080;”&gt;0</span><span style=”color: #000000;”&gt;; Index </span>= <span style=”color: #800080;”&gt;0</span><span style=”color: #000000;”&gt;; } </span><span style=”color: #0000ff;”&gt;else</span><span style=”color: #000000;”&gt; { Ang </span>= <span style=”color: #800080;”&gt;20</span><span style=”color: #000000;”&gt;; Index </span>= <span style=”color: #800080;”&gt;1</span><span style=”color: #000000;”&gt;; } RoadItem CItem </span>= <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; RoadItem(Ang,Length); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;CItem.imageIndex = Index;<a href=”https://www.jb51.cc/tag/huoqu/” target=”_blank” class=”keywords”>获取</a><a href=”https://www.jb51.cc/tag/suiji/” target=”_blank” class=”keywords”>随机</a>陆块的<a href=”https://www.jb51.cc/tag/tupian/” target=”_blank” class=”keywords”>图片</a> 这样<a href=”https://www.jb51.cc/tag/huoqu/” target=”_blank” class=”keywords”>获取</a>Y值需要写<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>一元一次方程<a href=”https://www.jb51.cc/tag/huoqu/” target=”_blank” class=”keywords”>获取</a></span> CItem.imageIndex = <span style=”color: #800080;”&gt;0</span>;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;这里设置全为第<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>图像 这样<a href=”https://www.jb51.cc/tag/huoqu/” target=”_blank” class=”keywords”>获取</a>Y值比较方便</span> <span style=”color: #0000ff;”&gt;if</span> (rand.Next(<span style=”color: #800080;”&gt;0</span>,<span style=”color: #800080;”&gt;4</span>) == <span style=”color: #800080;”&gt;1</span>)<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;4分之1的可能性为空陆块</span> CItem.type = <span style=”color: #800080;”&gt;0</span><span style=”color: #000000;”&gt;; </span><span style=”color: #0000ff;”&gt;else</span><span style=”color: #000000;”&gt; CItem.type </span>= <span style=”color: #800080;”&gt;1</span><span style=”color: #000000;”&gt;; ListRoad.Add(CItem);</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;<a href=”https://www.jb51.cc/tag/tianjia/” target=”_blank” class=”keywords”>添加</a>中<a href=”https://www.jb51.cc/tag/jiande/” target=”_blank” class=”keywords”>间的</a>陆块 <a href=”https://www.jb51.cc/tag/tianjia/” target=”_blank” class=”keywords”>添加</a>进陆块列表</span>

<span style=”color: #000000;”> }

RoadItem EndItem </span>= <span style=”color: #0000ff;”&gt;new</span> RoadItem(<span style=”color: #800080;”&gt;0</span><span style=”color: #000000;”&gt;,Length); EndItem.imageIndex </span>= <span style=”color: #800080;”&gt;0</span>;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;选择陆块的图像 0为第<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>平地 1为第二个左倾斜 2为第三个右倾斜 </span> EndItem.type = <span style=”color: #800080;”&gt;1</span><span style=”color: #000000;”&gt;; ListRoad.Add(EndItem);</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;<a href=”https://www.jb51.cc/tag/tianjia/” target=”_blank” class=”keywords”>添加</a>最后<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>陆块</span> <span style=”color: #0000ff;”&gt;for</span> (<span style=”color: #0000ff;”&gt;int</span> i = <span style=”color: #800080;”&gt;0</span>; i < ListRoad.Count; i++<span style=”color: #000000;”&gt;) { RoadItem DrawItem </span>=<span style=”color: #000000;”&gt; ListRoad[i]; </span><span style=”color: #0000ff;”&gt;if</span> (i == <span style=”color: #800080;”&gt;0</span><span style=”color: #000000;”&gt;) { DrawItem.start </span>= <span style=”color: #0000ff;”&gt;new</span> Point(<span style=”color: #800080;”&gt;0</span><span style=”color: #000000;”&gt;,RoadY); } </span><span style=”color: #0000ff;”&gt;else</span><span style=”color: #000000;”&gt; { DrawItem.start </span>= ListRoad[i – <span style=”color: #800080;”&gt;1</span><span style=”color: #000000;”&gt;].end; } DrawItem.end </span>= <span style=”color: #0000ff;”&gt;new</span> Point(DrawItem.start.X +<span style=”color: #000000;”&gt; DrawItem.length,RoadY); } </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;为每<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>陆块 定义 起始和终止向量坐标</span>

<span style=”color: #000000;”>

LandImgList.Add(Resources.land); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;为陆块使用的<a href=”https://www.jb51.cc/tag/tupian/” target=”_blank” class=”keywords”>图片</a>列表 赋值</span>

<span style=”color: #000000;”> }
}

</span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; RoadItem{ </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;int</span><span style=”color: #000000;”&gt; angle; </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;int</span> length;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;陆块长度</span> <span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;int</span> type;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;0为空,1为陆地</span> <span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;int</span> imageIndex = <span style=”color: #800080;”&gt;0</span>;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;使用的<a href=”https://www.jb51.cc/tag/tupian/” target=”_blank” class=”keywords”>图片</a></span> <span style=”color: #808080;”&gt;///</span> <span style=”color: #808080;”&gt;<summary></span> <span style=”color: #808080;”&gt;///</span><span style=”color: #008000;”&gt; 构建路块 </span><span style=”color: #808080;”&gt;///</span> <span style=”color: #808080;”&gt;</summary></span> <span style=”color: #808080;”&gt;///</span> <span style=”color: #808080;”&gt;<param name=”angle”&gt;</param></span> <span style=”color: #808080;”&gt;///</span> <span style=”color: #808080;”&gt;<param name=”length”&gt;</span><span style=”color: #008000;”&gt;陆块长度</span><span style=”color: #808080;”&gt;</param></span> <span style=”color: #0000ff;”&gt;public</span> RoadItem(<span style=”color: #0000ff;”&gt;int</span> angle,<span style=”color: #0000ff;”&gt;int</span><span style=”color: #000000;”&gt; length) { </span><span style=”color: #0000ff;”&gt;this</span>.angle =<span style=”color: #000000;”&gt; angle; </span><span style=”color: #0000ff;”&gt;this</span>.length =<span style=”color: #000000;”&gt; length; } </span><span style=”color: #0000ff;”&gt;public</span> Point start;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;陆块起始坐标</span> <span style=”color: #0000ff;”&gt;public</span> Point end;<span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;陆块终止坐标</span>

<span style=”color: #000000;”>

}

}

辅助类,这两个类是两个窗口,我闲Message@R_106_2419@不太好看,就换了个窗口,但貌似也没好看到那里去。。。哈哈

<span style=”color: #0000ff;”>namespace<span style=”color: #000000;”> Chicken
{
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>partial <span style=”color: #0000ff;”>class<span style=”color: #000000;”> Replay : Form
{
EventHandler Again;
<span style=”color: #0000ff;”>public<span style=”color: #000000;”> Replay(EventHandler Again)
{
<span style=”color: #0000ff;”>this.Again =<span style=”color: #000000;”> Again;
InitializeComponent();
}

</span><span style=”color: #0000ff;”&gt;private</span> <span style=”color: #0000ff;”&gt;void</span> button1_Click(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,EventArgs e) { </span><span style=”color: #0000ff;”&gt;this</span><span style=”color: #000000;”&gt;.Close(); Again(</span><span style=”color: #0000ff;”&gt;null</span>,<span style=”color: #0000ff;”&gt;null</span><span style=”color: #000000;”&gt;); }}

}

<span style=”color: #0000ff;”>namespace<span style=”color: #000000;”> Chicken
{
<span style=”color: #0000ff;”>public <span style=”color: #0000ff;”>partial <span style=”color: #0000ff;”>class<span style=”color: #000000;”> Dead : Form
{
EventHandler Again;
<span style=”color: #0000ff;”>public<span style=”color: #000000;”> Dead(EventHandler Again)
{
<span style=”color: #0000ff;”>this.Again =<span style=”color: #000000;”> Again;
InitializeComponent();
}

</span><span style=”color: #0000ff;”&gt;private</span> <span style=”color: #0000ff;”&gt;void</span> button1_Click(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,<span style=”color: #0000ff;”&gt;null</span><span style=”color: #000000;”&gt;); }}

}

这两个类是死亡窗口和重新开始窗口。源代码下载地址

源代码中,和文中的代码稍微有点不一样,如果我记得没错是这里,如下:

= EndX)

是修改,如果鸡身的一半以上超过终点,到达终点,游戏结束。这个源码上传时没修改这里。

不过不影响运行啦,但是还有一些小BUG。。

如果想升级这个游戏也很简答,比如,定义一个炮弹类,随机发一个。

当炮弹的矩形和小鸡的矩形相碰撞了,就死亡啦,矩形相撞有函数的,有兴趣的朋友可以自己扩展。

补充:上是跳跃,左右可以移动,空格是加速,鼠标全屏飞。。。。

开发环境:VS2008。

代码很简单,可以复制到别的环境中运行。