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

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

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

开发

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

board.vb

  1. Imports Qyoto
  2. NameSpace BoardSpace
  3. public class Board
  4. Inherits QFrame
  5. Const WIDTH As Integer = 300
  6. Const HEIGHT As Integer = 300
  7. Const DOT_SIZE As Integer = 10
  8. Const ALL_DOTS As Integer = 900
  9. Const RAND_POS As Integer = 30
  10. Const DELAY As Integer = 140
  11. Dim x(ALL_DOTS) As Integer
  12. Dim y(ALL_DOTS) As Integer
  13. Dim dots As Integer
  14. Dim apple_x As Integer
  15. Dim apple_y As Integer
  16. Dim left As Boolean = False
  17. Dim right As Boolean = True
  18. Dim up As Boolean = False
  19. Dim down As Boolean = False
  20. Dim inGame As Boolean = True
  21. Dim timer As QBasicTimer
  22. Dim ball As QImage
  23. Dim apple As QImage
  24. Dim head As QImage
  25. Public Sub New()
  26. Me.SetStyleSheet("QWidget { background-color: black }")
  27. Me.FocusPolicy = Qt.FocusPolicy.StrongFocus
  28. Try
  29. ball = New QImage("dot.png")
  30. apple = New QImage("apple.png")
  31. head = New QImage("head.png")
  32. Catch e As Exception
  33. Console.WriteLine("Cannot read images")
  34. Console.WriteLine(e.Message)
  35. Environment.Exit(1)
  36. End Try
  37. Me.InitGame()
  38. End Sub
  39. Private Sub InitGame()
  40. dots = 3
  41. For z As Integer = 0 To dots-1
  42. x(z) = 50 - z*10
  43. y(z) = 50
  44. Next
  45. Me.LocateApple()
  46. timer = New QBasicTimer()
  47. timer.Start(DELAY, Me)
  48. End Sub
  49. Protected Overrides Sub PaintEvent(ByVal e As QPaintEvent)
  50. Dim painter As New QPainter()
  51. painter.Begin(Me)
  52. If inGame
  53. Me.DrawObjects(painter)
  54. Else
  55. Me.GameOver(painter)
  56. End If
  57. painter.End()
  58. End Sub
  59. Private Sub DrawObjects(ByVal painter As QPainter)
  60. painter.DrawImage(apple_x, apple_y, apple)
  61. For z As Integer = 0 to dots - 1
  62. If z = 0
  63. painter.DrawImage(x(z), y(z), head)
  64. Else
  65. painter.DrawImage(x(z), y(z), ball)
  66. End If
  67. Next
  68. End Sub
  69. Private Sub GameOver(ByVal painter As QPainter)
  70. Dim msg As String = "Game Over"
  71. Dim small As New QFont("Helvetica", 12)
  72. small.SetBold(True)
  73. Dim metr As New QFontMetrics(small)
  74. Dim textWidth As Integer = metr.Width(msg)
  75. Dim h As Integer = Me.Height
  76. Dim w As Integer = Me.Width
  77. painter.SetPen(New QPen(New QBrush(Qt.GlobalColor.white), 1))
  78. painter.SetFont(small)
  79. painter.Translate(New QPoint(w/2, h/2))
  80. Dim text_x As Integer = -textWidth / 2
  81. Dim text_y As Integer = 0
  82. painter.DrawText(text_x, text_y, msg)
  83. End Sub
  84. Private Sub CheckApple()
  85. If x(0) = apple_x And y(0) = apple_y
  86. dots += 1
  87. Me.LocateApple()
  88. End If
  89. End Sub
  90. Private Sub Move()
  91. For z As Integer = dots To 1 Step -1
  92. x(z) = x(z - 1)
  93. y(z) = y(z - 1)
  94. Next
  95. If left
  96. x(0) -= DOT_SIZE
  97. End If
  98. If right
  99. x(0) += DOT_SIZE
  100. End If
  101. If up
  102. y(0) -= DOT_SIZE
  103. End If
  104. If down
  105. y(0) += DOT_SIZE
  106. End If
  107. End Sub
  108. Private Sub CheckCollision()
  109. For z As Integer = dots To 1 Step -1
  110. If z > 4 And x(0) = x(z) And y(0) = y(z)
  111. inGame = False
  112. End If
  113. Next
  114. If y(0) > HEIGHT
  115. inGame = False
  116. End If
  117. If y(0) < 0
  118. inGame = False
  119. End If
  120. If x(0) > WIDTH
  121. inGame = False
  122. End If
  123. If x(0) < 0
  124. inGame = False
  125. End If
  126. End Sub
  127. Private Sub LocateApple()
  128. Dim rand As New Random()
  129. Dim r As Integer = rand.Next(RAND_POS)
  130. apple_x = r * DOT_SIZE
  131. r = rand.Next(RAND_POS)
  132. apple_y = r * DOT_SIZE
  133. End Sub
  134. Protected Overrides Sub TimerEvent(ByVal e As QTimerEvent)
  135. If inGame
  136. Me.CheckApple()
  137. Me.CheckCollision()
  138. Me.Move()
  139. Else
  140. timer.Stop()
  141. End If
  142. Me.Repaint()
  143. End Sub
  144. Protected Overrides Sub KeyPressEvent(ByVal e As QKeyEvent)
  145. Dim key As Integer = e.Key()
  146. If key = Qt.Key.Key_Left And Not right
  147. left = True
  148. up = False
  149. down = False
  150. End If
  151. If key = Qt.Key.Key_Right And Not left
  152. right = True
  153. up = False
  154. down = False
  155. End If
  156. If key = Qt.Key.Key_Up And Not down
  157. up = True
  158. right = False
  159. left = False
  160. End If
  161. If key = Qt.Key.Key_Down And Not up
  162. down = True
  163. right = False
  164. left = False
  165. End If
  166. End Sub
  167. End Class
  168. 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(painter)
  3. Else
  4. Me.GameOver(painter)
  5. End If

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

  1. Private Sub DrawObjects(ByVal painter As QPainter)
  2. painter.DrawImage(apple_x, apple_y, apple)
  3. For z As Integer = 0 to dots - 1
  4. If z = 0
  5. painter.DrawImage(x(z), y(z), head)
  6. Else
  7. painter.DrawImage(x(z), y(z), ball)
  8. End If
  9. Next
  10. 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. Else
  6. timer.Stop()
  7. End If

每 140 毫秒,将调用TimerEvent()方法。 如果我们参与了游戏,我们将调用三种构建游戏逻辑的方法。 否则,我们将停止计时器。

Board类的KeyPressEvent()方法中,我们确定按下的键。

  1. If key = Qt.Key.Key_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 Qt 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 Qyoto
  10. Imports BoardSpace
  11. Public Class VBQApp
  12. Inherits QMainWindow
  13. Dim WIDTH As Integer = 250
  14. Dim HEIGHT As Integer = 150
  15. Public Sub New()
  16. Me.SetWindowTitle("Nibbles")
  17. Dim board As New BoardSpace.Board()
  18. Me.SetCentralWidget(board)
  19. Me.Resize(310, 310)
  20. Me.Move(300, 300)
  21. Me.Show()
  22. End Sub
  23. Public Shared Sub Main(ByVal args() As String)
  24. Dim qapp As New QApplication(args)
  25. Dim app As New VBQApp
  26. QApplication.Exec()
  27. End Sub
  28. End Class

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

贪食蛇 - 图1

图:贪食蛇

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