原文: http://zetcode.com/gui/vbwinforms/nibbles/

在 Mono Winforms 编程教程的这一部分中,我们将创建贪食蛇游戏克隆。

贪食蛇游戏

贪食蛇是较旧的经典视频游戏。 它最初是在 70 年代后期创建的。 后来它被带到 PC 上。 在这个游戏中,玩家控制蠕虫。 目的是尽可能多地吃苹果。 蠕虫每次吃一个苹果,它的身体就会长大。 它必须避开墙壁和自己的身体。

开发

蠕虫每个关节的大小为 10 像素。 蠕虫由光标键控制。 最初,蠕虫具有三个关节。 通过按下光标键之一开始游戏。 如果游戏结束,我们将在棋盘中间显示Game Over消息。

board.vb

  1. Imports System
  2. Imports System.Collections
  3. Imports System.ComponentModel
  4. Imports System.Drawing
  5. Imports System.Data
  6. Imports System.Windows.Forms
  7. NameSpace BoardSpace
  8. public class Board
  9. Inherits UserControl
  10. Const WIDTH As Integer = 300
  11. Const HEIGHT As Integer = 300
  12. Const DOT_SIZE As Integer = 10
  13. Const ALL_DOTS As Integer = 900
  14. Const RAND_POS As Integer = 27
  15. Const DELAY As Integer = 140
  16. Dim x(ALL_DOTS) As Integer
  17. Dim y(ALL_DOTS) As Integer
  18. Dim dots As Integer
  19. Dim apple_x As Integer
  20. Dim apple_y As Integer
  21. Dim left As Boolean = False
  22. Dim right As Boolean = True
  23. Dim up As Boolean = False
  24. Dim down As Boolean = False
  25. Dim inGame As Boolean = True
  26. Private Dim timer As Timer
  27. Private Dim dot As Bitmap
  28. Private Dim apple As Bitmap
  29. Private Dim head As Bitmap
  30. Private Dim components As IContainer
  31. Public Dim BORDER_WIDTH As Integer
  32. Public Dim TITLEBAR_HEIGHT As Integer
  33. Public Sub New
  34. components = New Container
  35. Me.BackColor = Color.Black
  36. Me.DoubleBuffered = True
  37. Me.ClientSize = New Size(WIDTH, HEIGHT)
  38. Try
  39. dot = New Bitmap("dot.png")
  40. apple = New Bitmap("apple.png")
  41. head = New Bitmap("head.png")
  42. Catch e As Exception
  43. Console.WriteLine(e.Message)
  44. Environment.Exit(1)
  45. End Try
  46. Me.InitGame
  47. End Sub
  48. Private Sub InitGame
  49. dots = 3
  50. For z As Integer = 0 To dots-1
  51. x(z) = 50 - z*10
  52. y(z) = 50
  53. Next
  54. Me.LocateApple
  55. AddHandler Me.KeyUp, AddressOf Me.OnKeyUp
  56. timer = New Timer(Me.components)
  57. timer.Enabled = True
  58. timer.Interval = DELAY
  59. AddHandler timer.Tick, AddressOf Me.OnTick
  60. AddHandler Me.Paint, AddressOf Me.OnPaint
  61. End Sub
  62. Private Sub OnPaint(ByVal sender As Object, _
  63. ByVal e As PaintEventArgs)
  64. Dim g As Graphics = e.Graphics
  65. If inGame
  66. Me.DrawObjects(g)
  67. Else
  68. Me.GameOver(g)
  69. End If
  70. End Sub
  71. Private Sub DrawObjects(ByVal g As Graphics)
  72. g.DrawImage(apple, apple_x, apple_y)
  73. For z As Integer = 0 To dots-1
  74. If z = 0
  75. g.DrawImage(head, x(z), y(z))
  76. Else
  77. g.DrawImage(dot, x(z), y(z))
  78. End If
  79. Next
  80. End Sub
  81. Private Sub GameOver(ByVal g As Graphics)
  82. Dim msg As String = "Game Over"
  83. Dim rectF As RectangleF = RectangleF.op_Implicit(Me.ClientRectangle)
  84. Dim format As New StringFormat
  85. format.Alignment = StringAlignment.Center
  86. format.LineAlignment = StringAlignment.Center
  87. g.DrawString(msg, Font, Brushes.White, rectF , format)
  88. timer.Stop
  89. End Sub
  90. Private Sub CheckApple
  91. If x(0) = apple_x And y(0) = apple_y
  92. dots += 1
  93. Me.LocateApple
  94. End If
  95. End Sub
  96. Private Sub Move
  97. For z As Integer = dots To 1 Step -1
  98. x(z) = x(z - 1)
  99. y(z) = y(z - 1)
  100. Next
  101. If left
  102. x(0) -= DOT_SIZE
  103. End If
  104. If right
  105. x(0) += DOT_SIZE
  106. End If
  107. If up
  108. y(0) -= DOT_SIZE
  109. End If
  110. If down
  111. y(0) += DOT_SIZE
  112. End If
  113. End Sub
  114. Private Sub CheckCollision
  115. For z As Integer = dots To 1 Step -1
  116. If z > 4 And x(0) = x(z) And y(0) = y(z)
  117. inGame = False
  118. End If
  119. Next
  120. If y(0) >= HEIGHT - DOT_SIZE - TITLEBAR_HEIGHT
  121. inGame = False
  122. End If
  123. If y(0) < 0
  124. inGame = False
  125. End If
  126. If x(0) >= WIDTH - DOT_SIZE - BORDER_WIDTH:
  127. inGame = False
  128. End If
  129. If x(0) < 0
  130. inGame = False
  131. End If
  132. End Sub
  133. Private Sub LocateApple
  134. Dim rand As New Random
  135. Dim r As Integer = rand.Next(RAND_POS)
  136. apple_x = r * DOT_SIZE
  137. r = rand.Next(RAND_POS)
  138. apple_y = r * DOT_SIZE
  139. End Sub
  140. Private Sub OnTick(ByVal sender As Object, ByVal e As EventArgs)
  141. If inGame
  142. Me.CheckApple
  143. Me.CheckCollision
  144. Me.Move
  145. End If
  146. Me.Refresh
  147. End Sub
  148. Private Sub OnKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
  149. Dim key As Integer = e.KeyCode
  150. If key = Keys.Left And Not right
  151. left = True
  152. up = False
  153. down = False
  154. End If
  155. If key = Keys.Right And Not left
  156. right = True
  157. up = False
  158. down = False
  159. End If
  160. If key = Keys.Up And Not down
  161. up = True
  162. right = False
  163. left = False
  164. End if
  165. If key = Keys.Down And Not up
  166. down = True
  167. right = False
  168. left = False
  169. End If
  170. End Sub
  171. End Class
  172. End Namespace

首先,我们将定义游戏中使用的常量。

WIDTHHEIGHT常数确定电路板的大小。 DOT_SIZE是苹果的大小和蠕虫的点。 ALL_DOTS常数定义了板上可能的最大点数。 (900 = 300 * 300 / 10 * 10RAND_POS常数用于计算苹果的随机位置。 DELAY常数确定游戏的速度。

  1. Dim x(ALL_DOTS) As Integer
  2. Dim y(ALL_DOTS) As Integer

这两个数组存储蠕虫的所有关节的 x,y 坐标。

Move方法中,我们有游戏的关键算法。 要了解它,请查看蠕虫如何移动。 您控制蠕虫的头部。 您可以使用光标键更改其方向。 其余关节在链上向上移动一个位置。 第二关节移动到第一个关节的位置,第三关节移动到第二个关节的位置,依此类推。

  1. For z As Integer = dots To 1 Step -1
  2. x(z) = x(z - 1)
  3. y(z) = y(z - 1)
  4. Next

该代码将关节向上移动。

  1. If left
  2. x(0) -= DOT_SIZE
  3. End If

将头向左移动。

CheckCollision方法中,我们确定蠕虫是否已经击中自己或撞墙之一。

  1. For z As Integer = dots To 1 Step -1
  2. If z > 4 And x(0) = x(z) And y(0) = y(z)
  3. inGame = False
  4. End If
  5. Next

如果蠕虫用头撞到关节之一,就结束游戏。

  1. If y(0) >= HEIGHT - DOT_SIZE - TITLEBAR_HEIGHT
  2. inGame = False
  3. End If

如果蠕虫到达了棋盘的底部,我们就结束了游戏。

下图有助于了解蠕虫对象与板子底部的碰撞。

贪食蛇 - 图1

图:碰撞

locateApple方法在表格上随机定位一个苹果。

  1. Dim rand As New Random
  2. Dim r As Integer = rand.Next(RAND_POS)

我们得到一个从 0 到RAND_POS-1的随机数。

  1. apple_x = r * DOT_SIZE
  2. ...
  3. apple_y = r * DOT_SIZE

这些行设置了apple对象的 x,y 坐标。

OnKeyUp方法中,我们确定了键击玩家击键的时间。

  1. If key = Keys.Left And Not right
  2. left = True
  3. up = False
  4. down = False
  5. End If

如果我们按左光标键,则将left变量设置为True。 在Move方法中使用此变量来更改蠕虫对象的坐标。 还要注意,当蠕虫向右移动时,我们不能立即向左转。

nibbles.vb

  1. ' ZetCode Mono Visual Basic Winforms tutorial
  2. '
  3. ' In this program, we create
  4. ' a Nibbles game clone
  5. '
  6. ' author jan bodnar
  7. ' last modified May 2009
  8. ' website www.zetcode.com
  9. Imports System
  10. Imports System.Drawing
  11. Imports System.Windows.Forms
  12. Public Class WinVBApp
  13. Inherits Form
  14. Public Sub New
  15. Me.Text = "Nibbles"
  16. Me.FormBorderStyle = FormBorderStyle.FixedSingle
  17. Dim borderWidth As Integer = (Me.Width - Me.ClientSize.Width) / 2
  18. Dim titleBarHeight As Integer = Me.Height - Me.ClientSize.Height - borderWidth
  19. Dim board As New BoardSpace.Board
  20. board.BORDER_WIDTH = borderWidth
  21. board.TITLEBAR_HEIGHT = titleBarHeight
  22. Me.Controls.Add(board)
  23. Me.CenterToScreen
  24. End Sub
  25. Public Shared Sub Main
  26. Application.Run(New WinVBApp)
  27. End Sub
  28. End Class

这是主要的类。

贪食蛇 - 图2

图:贪食蛇

这是使用 Mono Winforms 库和 Visual Basic 语言编写的贪食蛇游戏。