俄罗斯方块游戏

本章我们要制作一个俄罗斯方块游戏。

Tetris

译注:称呼:方块是由四个小方格组成的

俄罗斯方块游戏是世界上最流行的游戏之一。是由一名叫Alexey Pajitnov的俄罗斯程序员在1985年制作的,从那时起,这个游戏就风靡了各个游戏平台。

俄罗斯方块归类为下落块迷宫游戏。游戏有7个基本形状:S、Z、T、L、反向L、直线、方块,每个形状都由4个方块组成,方块最终都会落到屏幕底部。所以玩家通过控制形状的左右位置和旋转,让每个形状都以合适的位置落下,如果有一行全部被方块填充,这行就会消失,并且得分。游戏结束的条件是有形状接触到了屏幕顶部。

方块展示:

tetrominoes

PyQt5是专门为创建图形界面产生的,里面一些专门为制作游戏而开发的组件,所以PyQt5是能制作小游戏的。

制作电脑游戏也是提高自己编程能力的一种很好的方式。

开发

没有图片,所以就自己用绘画画出来几个图形。每个游戏里都有数学模型的,这个也是。

开工之前:

  • QtCore.QBasicTimer()创建一个游戏循环
  • 模型是一直下落的
  • 模型的运动是以小块为基础单位的,不是按像素
  • 从数学意义上来说,模型就是就是一串数字而已

代码由四个类组成:Tetris, Board, Tetrominoe和Shape。Tetris类创建游戏,Board是游戏主要逻辑。Tetrominoe包含了所有的砖块,Shape是所有砖块的代码。

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. This is a Tetris game clone.
  6. Author: Jan Bodnar
  7. Website: zetcode.com
  8. Last edited: August 2017
  9. """
  10. from PyQt5.QtWidgets import QMainWindow, QFrame, QDesktopWidget, QApplication
  11. from PyQt5.QtCore import Qt, QBasicTimer, pyqtSignal
  12. from PyQt5.QtGui import QPainter, QColor
  13. import sys, random
  14. class Tetris(QMainWindow):
  15. def __init__(self):
  16. super().__init__()
  17. self.initUI()
  18. def initUI(self):
  19. '''initiates application UI'''
  20. self.tboard = Board(self)
  21. self.setCentralWidget(self.tboard)
  22. self.statusbar = self.statusBar()
  23. self.tboard.msg2Statusbar[str].connect(self.statusbar.showMessage)
  24. self.tboard.start()
  25. self.resize(180, 380)
  26. self.center()
  27. self.setWindowTitle('Tetris')
  28. self.show()
  29. def center(self):
  30. '''centers the window on the screen'''
  31. screen = QDesktopWidget().screenGeometry()
  32. size = self.geometry()
  33. self.move((screen.width()-size.width())/2,
  34. (screen.height()-size.height())/2)
  35. class Board(QFrame):
  36. msg2Statusbar = pyqtSignal(str)
  37. BoardWidth = 10
  38. BoardHeight = 22
  39. Speed = 300
  40. def __init__(self, parent):
  41. super().__init__(parent)
  42. self.initBoard()
  43. def initBoard(self):
  44. '''initiates board'''
  45. self.timer = QBasicTimer()
  46. self.isWaitingAfterLine = False
  47. self.curX = 0
  48. self.curY = 0
  49. self.numLinesRemoved = 0
  50. self.board = []
  51. self.setFocusPolicy(Qt.StrongFocus)
  52. self.isStarted = False
  53. self.isPaused = False
  54. self.clearBoard()
  55. def shapeAt(self, x, y):
  56. '''determines shape at the board position'''
  57. return self.board[(y * Board.BoardWidth) + x]
  58. def setShapeAt(self, x, y, shape):
  59. '''sets a shape at the board'''
  60. self.board[(y * Board.BoardWidth) + x] = shape
  61. def squareWidth(self):
  62. '''returns the width of one square'''
  63. return self.contentsRect().width() // Board.BoardWidth
  64. def squareHeight(self):
  65. '''returns the height of one square'''
  66. return self.contentsRect().height() // Board.BoardHeight
  67. def start(self):
  68. '''starts game'''
  69. if self.isPaused:
  70. return
  71. self.isStarted = True
  72. self.isWaitingAfterLine = False
  73. self.numLinesRemoved = 0
  74. self.clearBoard()
  75. self.msg2Statusbar.emit(str(self.numLinesRemoved))
  76. self.newPiece()
  77. self.timer.start(Board.Speed, self)
  78. def pause(self):
  79. '''pauses game'''
  80. if not self.isStarted:
  81. return
  82. self.isPaused = not self.isPaused
  83. if self.isPaused:
  84. self.timer.stop()
  85. self.msg2Statusbar.emit("paused")
  86. else:
  87. self.timer.start(Board.Speed, self)
  88. self.msg2Statusbar.emit(str(self.numLinesRemoved))
  89. self.update()
  90. def paintEvent(self, event):
  91. '''paints all shapes of the game'''
  92. painter = QPainter(self)
  93. rect = self.contentsRect()
  94. boardTop = rect.bottom() - Board.BoardHeight * self.squareHeight()
  95. for i in range(Board.BoardHeight):
  96. for j in range(Board.BoardWidth):
  97. shape = self.shapeAt(j, Board.BoardHeight - i - 1)
  98. if shape != Tetrominoe.NoShape:
  99. self.drawSquare(painter,
  100. rect.left() + j * self.squareWidth(),
  101. boardTop + i * self.squareHeight(), shape)
  102. if self.curPiece.shape() != Tetrominoe.NoShape:
  103. for i in range(4):
  104. x = self.curX + self.curPiece.x(i)
  105. y = self.curY - self.curPiece.y(i)
  106. self.drawSquare(painter, rect.left() + x * self.squareWidth(),
  107. boardTop + (Board.BoardHeight - y - 1) * self.squareHeight(),
  108. self.curPiece.shape())
  109. def keyPressEvent(self, event):
  110. '''processes key press events'''
  111. if not self.isStarted or self.curPiece.shape() == Tetrominoe.NoShape:
  112. super(Board, self).keyPressEvent(event)
  113. return
  114. key = event.key()
  115. if key == Qt.Key_P:
  116. self.pause()
  117. return
  118. if self.isPaused:
  119. return
  120. elif key == Qt.Key_Left:
  121. self.tryMove(self.curPiece, self.curX - 1, self.curY)
  122. elif key == Qt.Key_Right:
  123. self.tryMove(self.curPiece, self.curX + 1, self.curY)
  124. elif key == Qt.Key_Down:
  125. self.tryMove(self.curPiece.rotateRight(), self.curX, self.curY)
  126. elif key == Qt.Key_Up:
  127. self.tryMove(self.curPiece.rotateLeft(), self.curX, self.curY)
  128. elif key == Qt.Key_Space:
  129. self.dropDown()
  130. elif key == Qt.Key_D:
  131. self.oneLineDown()
  132. else:
  133. super(Board, self).keyPressEvent(event)
  134. def timerEvent(self, event):
  135. '''handles timer event'''
  136. if event.timerId() == self.timer.timerId():
  137. if self.isWaitingAfterLine:
  138. self.isWaitingAfterLine = False
  139. self.newPiece()
  140. else:
  141. self.oneLineDown()
  142. else:
  143. super(Board, self).timerEvent(event)
  144. def clearBoard(self):
  145. '''clears shapes from the board'''
  146. for i in range(Board.BoardHeight * Board.BoardWidth):
  147. self.board.append(Tetrominoe.NoShape)
  148. def dropDown(self):
  149. '''drops down a shape'''
  150. newY = self.curY
  151. while newY > 0:
  152. if not self.tryMove(self.curPiece, self.curX, newY - 1):
  153. break
  154. newY -= 1
  155. self.pieceDropped()
  156. def oneLineDown(self):
  157. '''goes one line down with a shape'''
  158. if not self.tryMove(self.curPiece, self.curX, self.curY - 1):
  159. self.pieceDropped()
  160. def pieceDropped(self):
  161. '''after dropping shape, remove full lines and create new shape'''
  162. for i in range(4):
  163. x = self.curX + self.curPiece.x(i)
  164. y = self.curY - self.curPiece.y(i)
  165. self.setShapeAt(x, y, self.curPiece.shape())
  166. self.removeFullLines()
  167. if not self.isWaitingAfterLine:
  168. self.newPiece()
  169. def removeFullLines(self):
  170. '''removes all full lines from the board'''
  171. numFullLines = 0
  172. rowsToRemove = []
  173. for i in range(Board.BoardHeight):
  174. n = 0
  175. for j in range(Board.BoardWidth):
  176. if not self.shapeAt(j, i) == Tetrominoe.NoShape:
  177. n = n + 1
  178. if n == 10:
  179. rowsToRemove.append(i)
  180. rowsToRemove.reverse()
  181. for m in rowsToRemove:
  182. for k in range(m, Board.BoardHeight):
  183. for l in range(Board.BoardWidth):
  184. self.setShapeAt(l, k, self.shapeAt(l, k + 1))
  185. numFullLines = numFullLines + len(rowsToRemove)
  186. if numFullLines > 0:
  187. self.numLinesRemoved = self.numLinesRemoved + numFullLines
  188. self.msg2Statusbar.emit(str(self.numLinesRemoved))
  189. self.isWaitingAfterLine = True
  190. self.curPiece.setShape(Tetrominoe.NoShape)
  191. self.update()
  192. def newPiece(self):
  193. '''creates a new shape'''
  194. self.curPiece = Shape()
  195. self.curPiece.setRandomShape()
  196. self.curX = Board.BoardWidth // 2 + 1
  197. self.curY = Board.BoardHeight - 1 + self.curPiece.minY()
  198. if not self.tryMove(self.curPiece, self.curX, self.curY):
  199. self.curPiece.setShape(Tetrominoe.NoShape)
  200. self.timer.stop()
  201. self.isStarted = False
  202. self.msg2Statusbar.emit("Game over")
  203. def tryMove(self, newPiece, newX, newY):
  204. '''tries to move a shape'''
  205. for i in range(4):
  206. x = newX + newPiece.x(i)
  207. y = newY - newPiece.y(i)
  208. if x < 0 or x >= Board.BoardWidth or y < 0 or y >= Board.BoardHeight:
  209. return False
  210. if self.shapeAt(x, y) != Tetrominoe.NoShape:
  211. return False
  212. self.curPiece = newPiece
  213. self.curX = newX
  214. self.curY = newY
  215. self.update()
  216. return True
  217. def drawSquare(self, painter, x, y, shape):
  218. '''draws a square of a shape'''
  219. colorTable = [0x000000, 0xCC6666, 0x66CC66, 0x6666CC,
  220. 0xCCCC66, 0xCC66CC, 0x66CCCC, 0xDAAA00]
  221. color = QColor(colorTable[shape])
  222. painter.fillRect(x + 1, y + 1, self.squareWidth() - 2,
  223. self.squareHeight() - 2, color)
  224. painter.setPen(color.lighter())
  225. painter.drawLine(x, y + self.squareHeight() - 1, x, y)
  226. painter.drawLine(x, y, x + self.squareWidth() - 1, y)
  227. painter.setPen(color.darker())
  228. painter.drawLine(x + 1, y + self.squareHeight() - 1,
  229. x + self.squareWidth() - 1, y + self.squareHeight() - 1)
  230. painter.drawLine(x + self.squareWidth() - 1,
  231. y + self.squareHeight() - 1, x + self.squareWidth() - 1, y + 1)
  232. class Tetrominoe(object):
  233. NoShape = 0
  234. ZShape = 1
  235. SShape = 2
  236. LineShape = 3
  237. TShape = 4
  238. SquareShape = 5
  239. LShape = 6
  240. MirroredLShape = 7
  241. class Shape(object):
  242. coordsTable = (
  243. ((0, 0), (0, 0), (0, 0), (0, 0)),
  244. ((0, -1), (0, 0), (-1, 0), (-1, 1)),
  245. ((0, -1), (0, 0), (1, 0), (1, 1)),
  246. ((0, -1), (0, 0), (0, 1), (0, 2)),
  247. ((-1, 0), (0, 0), (1, 0), (0, 1)),
  248. ((0, 0), (1, 0), (0, 1), (1, 1)),
  249. ((-1, -1), (0, -1), (0, 0), (0, 1)),
  250. ((1, -1), (0, -1), (0, 0), (0, 1))
  251. )
  252. def __init__(self):
  253. self.coords = [[0,0] for i in range(4)]
  254. self.pieceShape = Tetrominoe.NoShape
  255. self.setShape(Tetrominoe.NoShape)
  256. def shape(self):
  257. '''returns shape'''
  258. return self.pieceShape
  259. def setShape(self, shape):
  260. '''sets a shape'''
  261. table = Shape.coordsTable[shape]
  262. for i in range(4):
  263. for j in range(2):
  264. self.coords[i][j] = table[i][j]
  265. self.pieceShape = shape
  266. def setRandomShape(self):
  267. '''chooses a random shape'''
  268. self.setShape(random.randint(1, 7))
  269. def x(self, index):
  270. '''returns x coordinate'''
  271. return self.coords[index][0]
  272. def y(self, index):
  273. '''returns y coordinate'''
  274. return self.coords[index][1]
  275. def setX(self, index, x):
  276. '''sets x coordinate'''
  277. self.coords[index][0] = x
  278. def setY(self, index, y):
  279. '''sets y coordinate'''
  280. self.coords[index][1] = y
  281. def minX(self):
  282. '''returns min x value'''
  283. m = self.coords[0][0]
  284. for i in range(4):
  285. m = min(m, self.coords[i][0])
  286. return m
  287. def maxX(self):
  288. '''returns max x value'''
  289. m = self.coords[0][0]
  290. for i in range(4):
  291. m = max(m, self.coords[i][0])
  292. return m
  293. def minY(self):
  294. '''returns min y value'''
  295. m = self.coords[0][1]
  296. for i in range(4):
  297. m = min(m, self.coords[i][1])
  298. return m
  299. def maxY(self):
  300. '''returns max y value'''
  301. m = self.coords[0][1]
  302. for i in range(4):
  303. m = max(m, self.coords[i][1])
  304. return m
  305. def rotateLeft(self):
  306. '''rotates shape to the left'''
  307. if self.pieceShape == Tetrominoe.SquareShape:
  308. return self
  309. result = Shape()
  310. result.pieceShape = self.pieceShape
  311. for i in range(4):
  312. result.setX(i, self.y(i))
  313. result.setY(i, -self.x(i))
  314. return result
  315. def rotateRight(self):
  316. '''rotates shape to the right'''
  317. if self.pieceShape == Tetrominoe.SquareShape:
  318. return self
  319. result = Shape()
  320. result.pieceShape = self.pieceShape
  321. for i in range(4):
  322. result.setX(i, -self.y(i))
  323. result.setY(i, self.x(i))
  324. return result
  325. if __name__ == '__main__':
  326. app = QApplication([])
  327. tetris = Tetris()
  328. sys.exit(app.exec_())

游戏很简单,所以也就很好理解。程序加载之后游戏也就直接开始了,可以用P键暂停游戏,空格键让方块直接落到最下面。游戏的速度是固定的,并没有实现加速的功能。分数就是游戏中消除的行数。

  1. self.tboard = Board(self)
  2. self.setCentralWidget(self.tboard)

创建了一个Board类的实例,并设置为应用的中心组件。

  1. self.statusbar = self.statusBar()
  2. self.tboard.msg2Statusbar[str].connect(self.statusbar.showMessage)

创建一个statusbar来显示三种信息:消除的行数,游戏暂停状态或者游戏结束状态。msg2Statusbar是一个自定义的信号,用在(和)Board类(交互),showMessage()方法是一个内建的,用来在statusbar上显示信息的方法。

  1. self.tboard.start()

初始化游戏:

  1. class Board(QFrame):
  2. msg2Statusbar = pyqtSignal(str)
  3. ...

创建了一个自定义信号msg2Statusbar,当我们想往statusbar里显示信息的时候,发出这个信号就行了。

  1. BoardWidth = 10
  2. BoardHeight = 22
  3. Speed = 300

这些是Board类的变量。BoardWidthBoardHeight分别是board的宽度和高度。Speed是游戏的速度,每300ms出现一个新的方块。

  1. ...
  2. self.curX = 0
  3. self.curY = 0
  4. self.numLinesRemoved = 0
  5. self.board = []
  6. ...

initBoard()里初始化了一些重要的变量。self.board定义了方块的形状和位置,取值范围是0-7。

  1. def shapeAt(self, x, y):
  2. return self.board[(y * Board.BoardWidth) + x]

shapeAt()决定了board里方块的的种类。

  1. def squareWidth(self):
  2. return self.contentsRect().width() // Board.BoardWidth

board的大小可以动态的改变。所以方格的大小也应该随之变化。squareWidth()计算并返回每个块应该占用多少像素—也即Board.BoardWidth

  1. def pause(self):
  2. '''pauses game'''
  3. if not self.isStarted:
  4. return
  5. self.isPaused = not self.isPaused
  6. if self.isPaused:
  7. self.timer.stop()
  8. self.msg2Statusbar.emit("paused")
  9. else:
  10. self.timer.start(Board.Speed, self)
  11. self.msg2Statusbar.emit(str(self.numLinesRemoved))
  12. self.update()

pause()方法用来暂停游戏,停止计时并在statusbar上显示一条信息。

  1. def paintEvent(self, event):
  2. '''paints all shapes of the game'''
  3. painter = QPainter(self)
  4. rect = self.contentsRect()
  5. ...

渲染是在paintEvent()方法里发生的QPainter负责PyQt5里所有低级绘画操作。

  1. for i in range(Board.BoardHeight):
  2. for j in range(Board.BoardWidth):
  3. shape = self.shapeAt(j, Board.BoardHeight - i - 1)
  4. if shape != Tetrominoe.NoShape:
  5. self.drawSquare(painter,
  6. rect.left() + j * self.squareWidth(),
  7. boardTop + i * self.squareHeight(), shape)

渲染游戏分为两步。第一步是先画出所有已经落在最下面的的图,这些保存在self.board里。可以使用shapeAt()查看这个这个变量。

  1. if self.curPiece.shape() != Tetrominoe.NoShape:
  2. for i in range(4):
  3. x = self.curX + self.curPiece.x(i)
  4. y = self.curY - self.curPiece.y(i)
  5. self.drawSquare(painter, rect.left() + x * self.squareWidth(),
  6. boardTop + (Board.BoardHeight - y - 1) * self.squareHeight(),
  7. self.curPiece.shape())

第二步是画出更在下落的方块。

  1. elif key == Qt.Key_Right:
  2. self.tryMove(self.curPiece, self.curX + 1, self.curY)

keyPressEvent()方法获得用户按下的按键。如果按下的是右方向键,就尝试把方块向右移动,说尝试是因为有可能到边界不能移动了。

  1. elif key == Qt.Key_Up:
  2. self.tryMove(self.curPiece.rotateLeft(), self.curX, self.curY)

上方向键是把方块向左旋转一下

  1. elif key == Qt.Key_Space:
  2. self.dropDown()

空格键会直接把方块放到底部

  1. elif key == Qt.Key_D:
  2. self.oneLineDown()

D键是加速一次下落速度。

  1. def tryMove(self, newPiece, newX, newY):
  2. for i in range(4):
  3. x = newX + newPiece.x(i)
  4. y = newY - newPiece.y(i)
  5. if x < 0 or x >= Board.BoardWidth or y < 0 or y >= Board.BoardHeight:
  6. return False
  7. if self.shapeAt(x, y) != Tetrominoe.NoShape:
  8. return False
  9. self.curPiece = newPiece
  10. self.curX = newX
  11. self.curY = newY
  12. self.update()
  13. return True

tryMove()是尝试移动方块的方法。如果方块已经到达board的边缘或者遇到了其他方块,就返回False。否则就把方块下落到想要

  1. def timerEvent(self, event):
  2. if event.timerId() == self.timer.timerId():
  3. if self.isWaitingAfterLine:
  4. self.isWaitingAfterLine = False
  5. self.newPiece()
  6. else:
  7. self.oneLineDown()
  8. else:
  9. super(Board, self).timerEvent(event)

在计时器事件里,要么是等一个方块下落完之后创建一个新的方块,要么是让一个方块直接落到底(move a falling piece one line down)。

  1. def clearBoard(self):
  2. for i in range(Board.BoardHeight * Board.BoardWidth):
  3. self.board.append(Tetrominoe.NoShape)

clearBoard()方法通过Tetrominoe.NoShape清空broad

  1. def removeFullLines(self):
  2. numFullLines = 0
  3. rowsToRemove = []
  4. for i in range(Board.BoardHeight):
  5. n = 0
  6. for j in range(Board.BoardWidth):
  7. if not self.shapeAt(j, i) == Tetrominoe.NoShape:
  8. n = n + 1
  9. if n == 10:
  10. rowsToRemove.append(i)
  11. rowsToRemove.reverse()
  12. for m in rowsToRemove:
  13. for k in range(m, Board.BoardHeight):
  14. for l in range(Board.BoardWidth):
  15. self.setShapeAt(l, k, self.shapeAt(l, k + 1))
  16. numFullLines = numFullLines + len(rowsToRemove)
  17. ...

如果方块碰到了底部,就调用removeFullLines()方法,找到所有能消除的行消除它们。消除的具体动作就是把符合条件的行消除掉之后,再把它上面的行下降一行。注意移除满行的动作是倒着来的,因为我们是按照重力来表现游戏的,如果不这样就有可能出现有些方块浮在空中的现象。

  1. def newPiece(self):
  2. self.curPiece = Shape()
  3. self.curPiece.setRandomShape()
  4. self.curX = Board.BoardWidth // 2 + 1
  5. self.curY = Board.BoardHeight - 1 + self.curPiece.minY()
  6. if not self.tryMove(self.curPiece, self.curX, self.curY):
  7. self.curPiece.setShape(Tetrominoe.NoShape)
  8. self.timer.stop()
  9. self.isStarted = False
  10. self.msg2Statusbar.emit("Game over")

newPiece()方法是用来创建形状随机的方块。如果随机的方块不能正确的出现在预设的位置,游戏结束。

  1. class Tetrominoe(object):
  2. NoShape = 0
  3. ZShape = 1
  4. SShape = 2
  5. LineShape = 3
  6. TShape = 4
  7. SquareShape = 5
  8. LShape = 6
  9. MirroredLShape = 7

Tetrominoe类保存了所有方块的形状。我们还定义了一个NoShape的空形状。

Shape类保存类方块内部的信息。

  1. class Shape(object):
  2. coordsTable = (
  3. ((0, 0), (0, 0), (0, 0), (0, 0)),
  4. ((0, -1), (0, 0), (-1, 0), (-1, 1)),
  5. ...
  6. )
  7. ...

coordsTable元组保存了所有的方块形状的组成。是一个构成方块的坐标模版。

  1. self.coords = [[0,0] for i in range(4)]

上面创建了一个新的空坐标数组,这个数组将用来保存方块的坐标。

坐标系示意图:

coordinates

上面的图片可以帮助我们更好的理解坐标值的意义。比如元组(0, -1), (0, 0), (-1, 0), (-1, -1)代表了一个Z形状的方块。这个图表就描绘了这个形状。

  1. def rotateLeft(self):
  2. if self.pieceShape == Tetrominoe.SquareShape:
  3. return self
  4. result = Shape()
  5. result.pieceShape = self.pieceShape
  6. for i in range(4):
  7. result.setX(i, self.y(i))
  8. result.setY(i, -self.x(i))
  9. return result

rotateLeft()方法向右旋转一个方块。正方形的方块就没必要旋转,就直接返回了。其他的是返回一个新的,能表示这个形状旋转了的坐标。

程序展示:

Tetris