在 Visual Basic Qyoto 编程教程的这一部分中,我们将创建贪食蛇游戏克隆。
贪食蛇是较旧的经典视频游戏。 它最初是在 70 年代后期创建的。 后来它被带到 PC 上。 在这个游戏中,玩家控制蛇。 目的是尽可能多地吃苹果。 蛇每次吃一个苹果,它的身体就会长大。 蛇必须避开墙壁和自己的身体。
开发
蛇的每个关节的大小为 10px。 蛇由光标键控制。 最初,蛇具有三个关节。 游戏立即开始。 游戏结束后,我们在窗口中心显示"Game Over"消息。
board.vb
Imports QyotoNameSpace BoardSpacepublic class BoardInherits QFrameConst WIDTH As Integer = 300Const HEIGHT As Integer = 300Const DOT_SIZE As Integer = 10Const ALL_DOTS As Integer = 900Const RAND_POS As Integer = 30Const DELAY As Integer = 140Dim x(ALL_DOTS) As IntegerDim y(ALL_DOTS) As IntegerDim dots As IntegerDim apple_x As IntegerDim apple_y As IntegerDim left As Boolean = FalseDim right As Boolean = TrueDim up As Boolean = FalseDim down As Boolean = FalseDim inGame As Boolean = TrueDim timer As QBasicTimerDim ball As QImageDim apple As QImageDim head As QImagePublic Sub New()Me.SetStyleSheet("QWidget { background-color: black }")Me.FocusPolicy = Qt.FocusPolicy.StrongFocusTryball = New QImage("dot.png")apple = New QImage("apple.png")head = New QImage("head.png")Catch e As ExceptionConsole.WriteLine("Cannot read images")Console.WriteLine(e.Message)Environment.Exit(1)End TryMe.InitGame()End SubPrivate Sub InitGame()dots = 3For z As Integer = 0 To dots-1x(z) = 50 - z*10y(z) = 50NextMe.LocateApple()timer = New QBasicTimer()timer.Start(DELAY, Me)End SubProtected Overrides Sub PaintEvent(ByVal e As QPaintEvent)Dim painter As New QPainter()painter.Begin(Me)If inGameMe.DrawObjects(painter)ElseMe.GameOver(painter)End Ifpainter.End()End SubPrivate Sub DrawObjects(ByVal painter As QPainter)painter.DrawImage(apple_x, apple_y, apple)For z As Integer = 0 to dots - 1If z = 0painter.DrawImage(x(z), y(z), head)Elsepainter.DrawImage(x(z), y(z), ball)End IfNextEnd SubPrivate Sub GameOver(ByVal painter As QPainter)Dim msg As String = "Game Over"Dim small As New QFont("Helvetica", 12)small.SetBold(True)Dim metr As New QFontMetrics(small)Dim textWidth As Integer = metr.Width(msg)Dim h As Integer = Me.HeightDim w As Integer = Me.Widthpainter.SetPen(New QPen(New QBrush(Qt.GlobalColor.white), 1))painter.SetFont(small)painter.Translate(New QPoint(w/2, h/2))Dim text_x As Integer = -textWidth / 2Dim text_y As Integer = 0painter.DrawText(text_x, text_y, msg)End SubPrivate Sub CheckApple()If x(0) = apple_x And y(0) = apple_ydots += 1Me.LocateApple()End IfEnd SubPrivate Sub Move()For z As Integer = dots To 1 Step -1x(z) = x(z - 1)y(z) = y(z - 1)NextIf leftx(0) -= DOT_SIZEEnd IfIf rightx(0) += DOT_SIZEEnd IfIf upy(0) -= DOT_SIZEEnd IfIf downy(0) += DOT_SIZEEnd IfEnd SubPrivate Sub CheckCollision()For z As Integer = dots To 1 Step -1If z > 4 And x(0) = x(z) And y(0) = y(z)inGame = FalseEnd IfNextIf y(0) > HEIGHTinGame = FalseEnd IfIf y(0) < 0inGame = FalseEnd IfIf x(0) > WIDTHinGame = FalseEnd IfIf x(0) < 0inGame = FalseEnd IfEnd SubPrivate Sub LocateApple()Dim rand As New Random()Dim r As Integer = rand.Next(RAND_POS)apple_x = r * DOT_SIZEr = rand.Next(RAND_POS)apple_y = r * DOT_SIZEEnd SubProtected Overrides Sub TimerEvent(ByVal e As QTimerEvent)If inGameMe.CheckApple()Me.CheckCollision()Me.Move()Elsetimer.Stop()End IfMe.Repaint()End SubProtected Overrides Sub KeyPressEvent(ByVal e As QKeyEvent)Dim key As Integer = e.Key()If key = Qt.Key.Key_Left And Not rightleft = Trueup = Falsedown = FalseEnd IfIf key = Qt.Key.Key_Right And Not leftright = Trueup = Falsedown = FalseEnd IfIf key = Qt.Key.Key_Up And Not downup = Trueright = Falseleft = FalseEnd IfIf key = Qt.Key.Key_Down And Not updown = Trueright = Falseleft = FalseEnd IfEnd SubEnd ClassEnd Namespace
首先,我们将定义一些在游戏中使用的全局变量。
WIDTH和HEIGHT常数确定电路板的大小。 DOT_SIZE是苹果的大小和蛇的点。 ALL_DOTS常数定义了板上可能的最大点数。 RAND_POS常数用于计算苹果的随机位置。 DELAY常数确定游戏的速度。
Dim x(ALL_DOTS) As IntegerDim y(ALL_DOTS) As Integer
这两个数组存储蛇的所有可能关节的 x,y 坐标。
InitGame()方法初始化变量,加载图像并启动超时功能。
If inGameMe.DrawObjects(painter)ElseMe.GameOver(painter)End If
在PaintEvent()方法内部,我们检查inGame变量。 如果为真,则绘制对象。 苹果和蛇的关节。 否则,我们显示"Game Over"文本。
Private Sub DrawObjects(ByVal painter As QPainter)painter.DrawImage(apple_x, apple_y, apple)For z As Integer = 0 to dots - 1If z = 0painter.DrawImage(x(z), y(z), head)Elsepainter.DrawImage(x(z), y(z), ball)End IfNextEnd Sub
DrawObjects()方法绘制苹果和蛇的关节。 蛇的第一个关节是其头部,用红色圆圈表示。
Private Sub CheckApple()If x(0) = apple_x And y(0) = apple_ydots += 1Me.LocateApple()End IfEnd Sub
CheckApple()方法检查蛇是否击中了苹果对象。 如果是这样,我们添加另一个蛇形关节并调用LocateApple()方法,该方法将随机放置一个新的Apple对象。
在Move()方法中,我们有游戏的关键算法。 要了解它,请看一下蛇是如何运动的。 您控制蛇的头。 您可以使用光标键更改其方向。 其余关节在链上向上移动一个位置。 第二关节移动到第一个关节的位置,第三关节移动到第二个关节的位置,依此类推。
For z As Integer = dots To 1 Step -1x(z) = x(z - 1)y(z) = y(z - 1)Next
该代码将关节向上移动。
If leftx(0) -= DOT_SIZEEnd If
将头向左移动。
在CheckCollision()方法中,我们确定蛇是否击中了自己或撞墙之一。
For z As Integer = dots To 1 Step -1If z > 4 And x(0) = x(z) And y(0) = y(z)inGame = FalseEnd IfNext
如果蛇用头撞到关节之一,我们就结束游戏。
If y(0) > HEIGHTinGame = FalseEnd If
如果蛇击中了棋盘的底部,我们就结束了游戏。
LocateApple()方法在板上随机放置一个苹果。
Dim rand As New Random()Dim r As Integer = rand.Next(RAND_POS)
我们得到一个从 0 到RAND_POS-1的随机数。
apple_x = r * DOT_SIZE...apple_y = r * DOT_SIZE
这些行设置了apple对象的 x,y 坐标。
If inGameMe.CheckApple()Me.CheckCollision()Me.Move()Elsetimer.Stop()End If
每 140 毫秒,将调用TimerEvent()方法。 如果我们参与了游戏,我们将调用三种构建游戏逻辑的方法。 否则,我们将停止计时器。
在Board类的KeyPressEvent()方法中,我们确定按下的键。
If key = Qt.Key.Key_Left And Not rightleft = Trueup = Falsedown = FalseEnd If
如果单击左光标键,则将left变量设置为true。 在Move()方法中使用此变量来更改蛇对象的坐标。 还要注意,当蛇向右行驶时,我们不能立即向左转。
nibbles.vb
' ZetCode Mono Visual Basic Qt tutorial'' In this program, we create' a Nibbles game clone'' author jan bodnar' last modified May 2009' website www.zetcode.comImports QyotoImports BoardSpacePublic Class VBQAppInherits QMainWindowDim WIDTH As Integer = 250Dim HEIGHT As Integer = 150Public Sub New()Me.SetWindowTitle("Nibbles")Dim board As New BoardSpace.Board()Me.SetCentralWidget(board)Me.Resize(310, 310)Me.Move(300, 300)Me.Show()End SubPublic Shared Sub Main(ByVal args() As String)Dim qapp As New QApplication(args)Dim app As New VBQAppQApplication.Exec()End SubEnd Class
在这个类中,我们设置了贪食蛇游戏。

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