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

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

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

开发

蛇的每个关节的大小为 10px。 蛇由光标键控制。 最初,蛇具有三个关节。 游戏立即开始。 游戏结束后,我们在窗口中心显示"Game Over"消息。

board.vb

  1. Imports Gtk
  2. Imports Cairo
  3. NameSpace BoardSpace
  4. Public Class Board
  5. Inherits DrawingArea
  6. Const WIDTH As Integer = 300
  7. Const HEIGHT As Integer = 300
  8. Const DOT_SIZE As Integer = 10
  9. Const ALL_DOTS As Integer = 900
  10. Const RAND_POS As Integer = 30
  11. Const DELAY As Integer = 140
  12. Dim x(ALL_DOTS) As Integer
  13. Dim y(ALL_DOTS) As Integer
  14. Dim dots As Integer
  15. Dim apple_x As Integer
  16. Dim apple_y As Integer
  17. Dim left As Boolean = False
  18. Dim right As Boolean = True
  19. Dim up As Boolean = False
  20. Dim down As Boolean = False
  21. Dim inGame As Boolean = True
  22. Dim dot As ImageSurface
  23. Dim apple As ImageSurface
  24. Dim head As ImageSurface
  25. Public Sub New
  26. MyBase.New
  27. ModifyBg(StateType.Normal, New Gdk.Color(0, 0, 0))
  28. Me.InitGame
  29. End Sub
  30. Private Sub InitGame
  31. dots = 3
  32. For z As Integer = 0 To dots-1
  33. x(z) = 50 - z*10
  34. y(z) = 50
  35. Next
  36. Try
  37. dot = New ImageSurface("dot.png")
  38. head = New ImageSurface("head.png")
  39. apple = New ImageSurface("apple.png")
  40. Catch
  41. Console.WriteLine("Images not found")
  42. Environment.Exit(1)
  43. End Try
  44. Me.LocateApple
  45. Dim timer As New GLib.TimeoutHandler(AddressOf Me.OnTimer)
  46. GLib.Timeout.Add(100, timer)
  47. AddHandler Me.ExposeEvent, AddressOf Me.OnExpose
  48. End Sub
  49. Protected Sub OnExpose(ByVal sender As Object, ByVal e As ExposeEventArgs)
  50. Dim cc As Cairo.Context = Gdk.CairoHelper.Create(sender.GdkWindow)
  51. If inGame
  52. Me.DrawObjects(cc)
  53. Else
  54. Me.GameOver(cc)
  55. End If
  56. Dim disposeTarget As IDisposable = CType(cc.Target, IDisposable)
  57. disposeTarget.Dispose
  58. Dim disposeContext As IDisposable = CType(cc, IDisposable)
  59. disposeContext.Dispose
  60. End Sub
  61. Private Sub DrawObjects(ByVal cc As Cairo.Context)
  62. cc.SetSourceSurface(apple, apple_x, apple_y)
  63. cc.Paint
  64. For z As Integer = 0 to dots - 1
  65. If z = 0
  66. cc.SetSourceSurface(head, x(z), y(z))
  67. cc.Paint
  68. Else
  69. cc.SetSourceSurface(dot, x(z), y(z))
  70. cc.Paint
  71. End If
  72. Next
  73. End Sub
  74. Private Sub GameOver(ByVal cc As Cairo.Context)
  75. Dim message As String = "Game Over"
  76. Dim x As Integer = Allocation.Width / 2
  77. Dim y As Integer = Allocation.Height / 2
  78. cc.SetSourceRGB(1, 1, 1)
  79. cc.SetFontSize(18)
  80. Dim extents As TextExtents = cc.TextExtents(message)
  81. cc.MoveTo(x - extents.Width/2, y)
  82. cc.ShowText(message)
  83. inGame = False
  84. End Sub
  85. Private Sub CheckApple
  86. If x(0) = apple_x And y(0) = apple_y
  87. dots += 1
  88. Me.LocateApple
  89. End If
  90. End Sub
  91. Private Sub Move
  92. For z As Integer = dots To 1 Step -1
  93. x(z) = x(z - 1)
  94. y(z) = y(z - 1)
  95. Next
  96. If left
  97. x(0) -= DOT_SIZE
  98. End If
  99. If right
  100. x(0) += DOT_SIZE
  101. End If
  102. If up
  103. y(0) -= DOT_SIZE
  104. End If
  105. If down
  106. y(0) += DOT_SIZE
  107. End If
  108. End Sub
  109. Private Sub CheckCollision
  110. For z As Integer = dots To 1 Step -1
  111. If z > 4 And x(0) = x(z) And y(0) = y(z)
  112. inGame = False
  113. End If
  114. Next
  115. If y(0) > HEIGHT
  116. inGame = False
  117. End If
  118. If y(0) < 0
  119. inGame = False
  120. End If
  121. If x(0) > WIDTH
  122. inGame = False
  123. End If
  124. If x(0) < 0
  125. inGame = False
  126. End If
  127. End Sub
  128. Private Sub LocateApple
  129. Dim rand As New Random
  130. Dim r As Integer = rand.Next(RAND_POS)
  131. apple_x = r * DOT_SIZE
  132. r = rand.Next(RAND_POS)
  133. apple_y = r * DOT_SIZE
  134. End Sub
  135. Private Function OnTimer As Boolean
  136. If inGame
  137. Me.CheckApple
  138. Me.CheckCollision
  139. Me.Move
  140. Me.QueueDraw
  141. Return True
  142. Else
  143. Return False
  144. End If
  145. End Function
  146. Public Sub OnKeyDown(ByVal e As Gdk.EventKey)
  147. Dim key As Integer = e.KeyValue
  148. If key = Gdk.Key.Left AndAlso Not right
  149. left = True
  150. up = False
  151. down = False
  152. End If
  153. If key = Gdk.Key.Right AndAlso Not left
  154. right = True
  155. up = False
  156. down = False
  157. End If
  158. If key = Gdk.Key.Up AndAlso Not down
  159. up = True
  160. right = False
  161. left = False
  162. End If
  163. If key = Gdk.Key.Down AndAlso Not up
  164. down = True
  165. right = False
  166. left = False
  167. End If
  168. End Sub
  169. End Class
  170. End Namespace

首先,我们将定义一些在游戏中使用的全局变量。

WIDTHHEIGHT常数确定电路板的大小。 DOT_SIZE是苹果的大小和蛇的点。 ALL_DOTS常数定义了板上可能的最大点数。 RAND_POS常数用于计算苹果的随机位置。 DELAY常数确定游戏的速度。

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

这两个数组存储蛇的所有可能关节的 x,y 坐标。

InitGame方法初始化变量,加载图像并启动超时功能。

  1. If inGame
  2. Me.DrawObjects(cc)
  3. Else
  4. Me.GameOver(cc)
  5. End If

OnExpose方法内部,我们检查inGame变量。 如果为真,则绘制对象。 苹果和蛇的关节。 否则,我们显示"Game Over"文本。

  1. Private Sub DrawObjects(ByVal cc As Cairo.Context)
  2. cc.SetSourceSurface(apple, apple_x, apple_y)
  3. cc.Paint
  4. For z As Integer = 0 to dots - 1
  5. If z = 0
  6. cc.SetSourceSurface(head, x(z), y(z))
  7. cc.Paint
  8. Else
  9. cc.SetSourceSurface(dot, x(z), y(z))
  10. cc.Paint
  11. End If
  12. Next
  13. End Sub

DrawObjects方法绘制苹果和蛇的关节。 蛇的第一个关节是其头部,用红色圆圈表示。

  1. Private Sub CheckApple
  2. If x(0) = apple_x And y(0) = apple_y
  3. dots += 1
  4. Me.LocateApple
  5. End If
  6. End Sub

CheckApple方法检查蛇是否击中了苹果对象。 如果是这样,我们添加另一个蛇形关节并调用LocateApple方法,该方法将随机放置一个新的 Apple 对象。

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
  2. inGame = False
  3. End If

如果蛇击中了棋盘的底部,我们就结束了游戏。

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 坐标。

  1. If inGame
  2. Me.CheckApple
  3. Me.CheckCollision
  4. Me.Move
  5. Me.QueueDraw
  6. Return True
  7. Else
  8. Return False
  9. End If

每 140 毫秒,将调用OnTimer方法。 如果我们参与了游戏,我们将调用三种构建游戏逻辑的方法。 否则,我们返回False,它将停止计时器事件。

Board类的OnKeyDown方法中,我们确定按下的键。

  1. If key = Gdk.Key.Left AndAlso Not right
  2. left = True
  3. up = False
  4. down = False
  5. End If

如果单击左光标键,则将left变量设置为true。 在Move方法中使用此变量来更改蛇对象的坐标。 还要注意,当蛇向右行驶时,我们不能立即向左转。

nibbles.vb

  1. ' ZetCode Mono Visual Basic GTK# 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 Gtk
  10. Public Class GtkVBApp
  11. Inherits Window
  12. Dim WIDTH As Integer = 250
  13. Dim HEIGHT As Integer = 150
  14. Dim board As BoardSpace.Board
  15. Public Sub New
  16. MyBase.New("Nibbles")
  17. board = New BoardSpace.Board
  18. Me.Add(board)
  19. AddHandler Me.DeleteEvent, AddressOf Me.OnDelete
  20. Me.Resize(310, 310)
  21. Me.Move(300, 300)
  22. Me.ShowAll
  23. End Sub
  24. Private Sub OnDelete(ByVal sender As Object, _
  25. ByVal args As DeleteEventArgs)
  26. Application.Quit
  27. End Sub
  28. Protected Overrides Function OnKeyPressEvent(ByVal e As Gdk.EventKey) As Boolean
  29. board.OnKeyDown(e)
  30. Return True
  31. End Function
  32. Public Shared Sub Main
  33. Application.Init
  34. Dim app As New GtkVBApp
  35. Application.Run
  36. End Sub
  37. End Class

在这个类中,我们设置了贪食蛇游戏。

  1. Protected Overrides Function OnKeyPressEvent(ByVal e As Gdk.EventKey) As Boolean
  2. board.OnKeyDown(e)
  3. Return True
  4. End Function

在这个类中,我们捕获按键事件。 并将处理委托给板类的OnKeyDown方法。

贪食蛇 - 图1

图:贪食蛇

以下命令编译游戏。

  1. vbnc -r:/usr/lib/mono/gtk-sharp-2.0/gtk-sharp.dll
  2. -r:/usr/lib/mono/gtk-sharp-2.0/gdk-sharp.dll -r:/usr/lib/mono/2.0/Mono.Cairo.dll
  3. -r:/usr/lib/mono/gtk-sharp-2.0/glib-sharp.dll nibbles.vb board.vb

这是使用 GTK# 库和 Visual Basic 编程语言编写的贪食蛇电脑游戏。