在 Ruby Qt 编程教程的这一部分中,我们将创建一个贪食蛇游戏克隆。
贪食蛇是较旧的经典视频游戏。 它最初是在 70 年代后期创建的。 后来它被带到 PC 上。 在这个游戏中,玩家控制蛇。 目的是尽可能多地吃苹果。 蛇每次吃一个苹果,它的身体就会长大。 蛇必须避开墙壁和自己的身体。
开发
蛇的每个关节的大小为 10px。 蛇由光标键控制。 最初,蛇具有三个关节。 游戏立即开始。 游戏结束后,我们在窗口中心显示"Game Over"消息。
Board.rb
WIDTH = 300HEIGHT = 300DOT_SIZE = 10ALL_DOTS = WIDTH * HEIGHT / (DOT_SIZE * DOT_SIZE)RAND_POS = 29DELAY = 140$x = [0] * ALL_DOTS$y = [0] * ALL_DOTSclass Board < Qt::Widgetdef initialize(parent)super(parent)setFocusPolicy Qt::StrongFocusinitGameenddef initGame@left = false@right = true@up = false@down = false@inGame = true@dots = 3begin@ball = Qt::Image.new "dot.png"@apple = Qt::Image.new "apple.png"@head = Qt::Image.new "head.png"rescueputs "cannot load images"endfor i in (0..@dots)$x[i] = 50 - i * 10$y[i] = 50endlocateApplesetStyleSheet "QWidget { background-color: #000000 }"@timer = Qt::BasicTimer.new@timer.start(140, self)enddef paintEvent eventpainter = Qt::Painter.newpainter.begin selfif @inGamedrawObjects painterelsegameOver painterendpainter.endenddef drawObjects painterpainter.drawImage @apple_x, @apple_y, @applefor z in (0..@dots)if z == 0painter.drawImage $x[z], $y[z], @headelsepainter.drawImage $x[z], $y[z], @ballendendenddef gameOver paintermsg = "Game Over"small = Qt::Font.new "Helvetica", 12,Qt::Font::Bold.valuemetr = Qt::FontMetrics.new smalltextWidth = metr.width msgh = heightw = widthpainter.setPen Qt::Color.new Qt::whitepainter.setFont smallpainter.translate Qt::Point.new w/2, h/2painter.drawText -textWidth/2, 0, msgenddef checkAppleif $x[0] == @apple_x and $y[0] == @apple_y@dots = @dots + 1locateAppleendenddef movez = @dotswhile z > 0$x[z] = $x[(z - 1)]$y[z] = $y[(z - 1)]z = z - 1endif @left$x[0] -= DOT_SIZEendif @right$x[0] += DOT_SIZEendif @up$y[0] -= DOT_SIZEendif @down$y[0] += DOT_SIZEendenddef checkCollisionz = @dotswhile z > 0if z > 4 and $x[0] == $x[z] and $y[0] == $y[z]@inGame = falseendz = z - 1endif $y[0] > HEIGHT@inGame = falseendif $y[0] < 0@inGame = falseendif $x[0] > WIDTH@inGame = falseendif $x[0] < 0@inGame = falseendenddef locateAppler = rand RAND_POS@apple_x = r * DOT_SIZEr = rand RAND_POS@apple_y = r * DOT_SIZEenddef timerEvent eventif @inGamecheckApplecheckCollisionmoveelse@timer.stopendrepaintenddef keyPressEvent eventkey = event.keyif key == Qt::Key_Left.value and not @right@left = true@up = false@down = falseendif key == Qt::Key_Right.value and not @left@right = true@up = false@down = falseendif key == Qt::Key_Up.value and not @down@up = true@right = false@left = falseendif key == Qt::Key_Down.value and not @up@down = true@right = false@left = falseendendend
首先,我们将定义一些在游戏中使用的常量。
WIDTH和HEIGHT常数确定电路板的大小。 DOT_SIZE是苹果的大小和蛇的点。 ALL_DOTS常数定义了板上可能的最大点数。 RAND_POS常数用于计算苹果的随机位置。 DELAY常数确定游戏的速度。
$x = [0] * ALL_DOTS$y = [0] * ALL_DOTS
这两个数组存储蛇的所有可能关节的 x,y 坐标。
initGame方法初始化变量,加载图像并启动超时功能。
if @inGamedrawObjects painterelsegameOver painterend
在paintEvent方法内部,我们检查@inGame变量。 如果为真,则绘制对象。 苹果和蛇的关节。 否则,我们显示"Game Over"文本。
def drawObjects painterpainter.drawImage @apple_x, @apple_y, @applefor z in (0..@dots)if z == 0painter.drawImage $x[z], $y[z], @headelsepainter.drawImage $x[z], $y[z], @ballendendend
drawObjects方法绘制苹果和蛇的关节。 蛇的第一个关节是其头部,用红色圆圈表示。
def checkAppleif $x[0] == @apple_x and $y[0] == @apple_y@dots = @dots + 1locateAppleendend
checkApple方法检查蛇是否击中了苹果对象。 如果是这样,我们添加另一个蛇形关节并调用locateApple方法,该方法将随机放置一个新的Apple对象。
在move方法中,我们有游戏的关键算法。 要了解它,请看一下蛇是如何运动的。 您控制蛇的头。 您可以使用光标键更改其方向。 其余关节在链上向上移动一个位置。 第二关节移动到第一个关节的位置,第三关节移动到第二个关节的位置,依此类推。
while z > 0$x[z] = $x[(z - 1)]$y[z] = $y[(z - 1)]z = z - 1end
该代码将关节向上移动。
if @left$x[0] -= DOT_SIZEend
将头向左移动。
在checkCollision方法中,我们确定蛇是否击中了自己或撞墙之一。
while z > 0if z > 4 and $x[0] == $x[z] and $y[0] == $y[z]@inGame = falseendz = z - 1end
如果蛇用头撞到关节之一,我们就结束游戏。
if $y[0] > HEIGHT@inGame = falseend
如果蛇击中了棋盘的底部,我们就结束了游戏。
locateApple方法在板上随机放置一个苹果。
r = rand RAND_POS
我们得到一个从 0 到RAND_POS-1的随机数。
@apple_x = r * DOT_SIZE...@apple_y = r * DOT_SIZE
这些行设置了apple对象的 x 和 y 坐标。
if @inGamecheckApplecheckCollisionmoveelse@timer.stopend
每 140 毫秒,将调用timerEvent方法。 如果我们参与了游戏,我们将调用三种构建游戏逻辑的方法。 否则,我们将停止计时器。
在Board类的keyPressEvent方法中,我们确定按下的键。
if key == Qt::Key_Left.value and not @right@left = true@up = false@down = falseend
如果单击左光标键,则将@left变量设置为true。 在move方法中使用此变量来更改蛇对象的坐标。 还要注意,当蛇向右行驶时,我们不能立即向左转。
Nibbles.rb
#!/usr/bin/ruby# ZetCode Ruby Qt tutorial## In this program, we create# a Nibbles game clone.## author: Jan Bodnar# website: www.zetcode.com# last modified: September 2012require 'Qt'require 'Board'class QtApp < Qt::MainWindowdef initializesupersetWindowTitle "Nibbles"setCentralWidget Board.new(self)resize 310, 310move 300, 300showendendapp = Qt::Application.new ARGVQtApp.newapp.exec
在Nibbles.rb文件中,我们设置了贪食蛇游戏。

图:贪食蛇
这是使用 Qt 库和 Ruby 编程语言编程的贪食蛇电脑游戏。
