Advertisement
Guest User

Untitled

a guest
Nov 9th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.08 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -- coding: utf-8 --
  3.  
  4. import sys, random
  5. from PyQt5.QtWidgets import QMainWindow, QFrame, QDesktopWidget, QApplication
  6. from PyQt5.QtCore import Qt, QBasicTimer, pyqtSignal
  7. from PyQt5.QtGui import QPainter, QColor
  8.  
  9.  
  10. class Tetris(QMainWindow):
  11.  
  12. def _init_(self):
  13. super()._init_()
  14.  
  15. self.initUI()
  16.  
  17.  
  18. def initUI(self):
  19.  
  20. self.tboard = Board(self)
  21. self.setCentralWidget(self.tboard)
  22.  
  23. self.statusbar = self.statusBar()
  24. self.tboard.msg2Statusbar[str].connect(self.statusbar.showMessage)
  25.  
  26. self.tboard.start()
  27.  
  28. self.resize(180, 380)
  29. self.center()
  30. self.setWindowTitle('Tetris')
  31. self.show()
  32.  
  33.  
  34. def center(self):
  35.  
  36. screen = QDesktopWidget().screenGeometry()
  37. size = self.geometry()
  38. self.move((screen.width()-size.width())/2,
  39. (screen.height()-size.height())/2)
  40.  
  41.  
  42. class Board(QFrame):
  43.  
  44. msg2Statusbar = pyqtSignal(str)
  45.  
  46. BoardWidth = 10
  47. BoardHeight = 22
  48. Speed = 300
  49.  
  50. def _init_(self, parent):
  51. super()._init_(parent)
  52.  
  53. self.initBoard()
  54.  
  55.  
  56. def initBoard(self):
  57.  
  58. self.timer = QBasicTimer()
  59. self.isWaitingAfterLine = False
  60.  
  61. self.curX = 0
  62. self.curY = 0
  63. self.numLinesRemoved = 0
  64. self.board = []
  65.  
  66. self.setFocusPolicy(Qt.StrongFocus)
  67. self.isStarted = False
  68. self.isPaused = False
  69. self.clearBoard()
  70.  
  71.  
  72. def shapeAt(self, x, y):
  73. return self.board[(y * Board.BoardWidth) + x]
  74.  
  75.  
  76. def setShapeAt(self, x, y, shape):
  77. self.board[(y * Board.BoardWidth) + x] = shape
  78.  
  79.  
  80. def squareWidth(self):
  81. return self.contentsRect().width() // Board.BoardWidth
  82.  
  83.  
  84. def squareHeight(self):
  85. return self.contentsRect().height() // Board.BoardHeight
  86.  
  87.  
  88. def start(self):
  89.  
  90. if self.isPaused:
  91. return
  92.  
  93. self.isStarted = True
  94. self.isWaitingAfterLine = False
  95. self.numLinesRemoved = 0
  96. self.clearBoard()
  97.  
  98. self.msg2Statusbar.emit(str(self.numLinesRemoved))
  99.  
  100. self.newPiece()
  101. self.timer.start(Board.Speed, self)
  102.  
  103.  
  104. def pause(self):
  105.  
  106. if not self.isStarted:
  107. return
  108.  
  109. self.isPaused = not self.isPaused
  110.  
  111. if self.isPaused:
  112. self.timer.stop()
  113. self.msg2Statusbar.emit("paused")
  114.  
  115. else:
  116. self.timer.start(Board.Speed, self)
  117. self.msg2Statusbar.emit(str(self.numLinesRemoved))
  118.  
  119. self.update()
  120.  
  121.  
  122. def paintEvent(self, event):
  123.  
  124. painter = QPainter(self)
  125. rect = self.contentsRect()
  126.  
  127. boardTop = rect.bottom() - Board.BoardHeight * self.squareHeight()
  128.  
  129. for i in range(Board.BoardHeight):
  130. for j in range(Board.BoardWidth):
  131. shape = self.shapeAt(j, Board.BoardHeight - i - 1)
  132.  
  133. if shape != Tetrominoe.NoShape:
  134. self.drawSquare(painter,
  135. rect.left() + j * self.squareWidth(),
  136. boardTop + i * self.squareHeight(), shape)
  137.  
  138. if self.curPiece.shape() != Tetrominoe.NoShape:
  139.  
  140. for i in range(4):
  141.  
  142. x = self.curX + self.curPiece.x(i)
  143. y = self.curY - self.curPiece.y(i)
  144. self.drawSquare(painter, rect.left() + x * self.squareWidth(),
  145. boardTop + (Board.BoardHeight - y - 1) * self.squareHeight(),
  146. self.curPiece.shape())
  147.  
  148.  
  149. def keyPressEvent(self, event):
  150.  
  151. if not self.isStarted or self.curPiece.shape() == Tetrominoe.NoShape:
  152. super(Board, self).keyPressEvent(event)
  153. return
  154.  
  155. key = event.key()
  156.  
  157. if key == Qt.Key_P:
  158. self.pause()
  159. return
  160.  
  161. if self.isPaused:
  162. return
  163.  
  164. elif key == Qt.Key_Left:
  165. self.tryMove(self.curPiece, self.curX - 1, self.curY)
  166.  
  167. elif key == Qt.Key_Right:
  168. self.tryMove(self.curPiece, self.curX + 1, self.curY)
  169.  
  170. elif key == Qt.Key_Down:
  171. self.tryMove(self.curPiece.rotateRight(), self.curX, self.curY)
  172.  
  173. elif key == Qt.Key_Up:
  174. self.tryMove(self.curPiece.rotateLeft(), self.curX, self.curY)
  175.  
  176. elif key == Qt.Key_Space:
  177. self.dropDown()
  178.  
  179. elif key == Qt.Key_D:
  180. self.oneLineDown()
  181.  
  182. else:
  183. super(Board, self).keyPressEvent(event)
  184.  
  185.  
  186. def timerEvent(self, event):
  187.  
  188. if event.timerId() == self.timer.timerId():
  189.  
  190. if self.isWaitingAfterLine:
  191. self.isWaitingAfterLine = False
  192. self.newPiece()
  193. else:
  194. self.oneLineDown()
  195.  
  196. else:
  197. super(Board, self).timerEvent(event)
  198.  
  199.  
  200. def clearBoard(self):
  201.  
  202. for i in range(Board.BoardHeight * Board.BoardWidth):
  203. self.board.append(Tetrominoe.NoShape)
  204.  
  205.  
  206. def dropDown(self):
  207.  
  208. newY = self.curY
  209.  
  210. while newY > 0:
  211.  
  212. if not self.tryMove(self.curPiece, self.curX, newY - 1):
  213. break
  214.  
  215. newY -= 1
  216.  
  217. self.pieceDropped()
  218.  
  219.  
  220. def oneLineDown(self):
  221.  
  222. if not self.tryMove(self.curPiece, self.curX, self.curY - 1):
  223. self.pieceDropped()
  224.  
  225.  
  226. def pieceDropped(self):
  227.  
  228. for i in range(4):
  229.  
  230. x = self.curX + self.curPiece.x(i)
  231. y = self.curY - self.curPiece.y(i)
  232. self.setShapeAt(x, y, self.curPiece.shape())
  233.  
  234. self.removeFullLines()
  235.  
  236. if not self.isWaitingAfterLine:
  237. self.newPiece()
  238.  
  239.  
  240. def removeFullLines(self):
  241.  
  242. numFullLines = 0
  243. rowsToRemove = []
  244.  
  245. for i in range(Board.BoardHeight):
  246.  
  247. n = 0
  248. for j in range(Board.BoardWidth):
  249. if not self.shapeAt(j, i) == Tetrominoe.NoShape:
  250. n = n + 1
  251.  
  252. if n == 10:
  253. rowsToRemove.append(i)
  254.  
  255. rowsToRemove.reverse()
  256.  
  257.  
  258. for m in rowsToRemove:
  259.  
  260. for k in range(m, Board.BoardHeight):
  261. for l in range(Board.BoardWidth):
  262. self.setShapeAt(l, k, self.shapeAt(l, k + 1))
  263.  
  264. numFullLines = numFullLines + len(rowsToRemove)
  265.  
  266. if numFullLines > 0:
  267.  
  268. self.numLinesRemoved = self.numLinesRemoved + numFullLines
  269. self.msg2Statusbar.emit(str(self.numLinesRemoved))
  270.  
  271. self.isWaitingAfterLine = True
  272. self.curPiece.setShape(Tetrominoe.NoShape)
  273. self.update()
  274.  
  275.  
  276. def newPiece(self):
  277.  
  278. self.curPiece = Shape()
  279. self.curPiece.setRandomShape()
  280. self.curX = Board.BoardWidth // 2 + 1
  281. self.curY = Board.BoardHeight - 1 + self.curPiece.minY()
  282.  
  283. if not self.tryMove(self.curPiece, self.curX, self.curY):
  284.  
  285. self.curPiece.setShape(Tetrominoe.NoShape)
  286. self.timer.stop()
  287. self.isStarted = False
  288. self.msg2Statusbar.emit("Game over")
  289.  
  290.  
  291.  
  292. def tryMove(self, newPiece, newX, newY):
  293.  
  294. for i in range(4):
  295.  
  296. x = newX + newPiece.x(i)
  297. y = newY - newPiece.y(i)
  298.  
  299. if x < 0 or x >= Board.BoardWidth or y < 0 or y >= Board.BoardHeight:
  300. return False
  301.  
  302. if self.shapeAt(x, y) != Tetrominoe.NoShape:
  303. return False
  304.  
  305. self.curPiece = newPiece
  306. self.curX = newX
  307. self.curY = newY
  308. self.update()
  309.  
  310. return True
  311.  
  312.  
  313. def drawSquare(self, painter, x, y, shape):
  314.  
  315. colorTable = [0x000000, 0xCC6666, 0x66CC66, 0x6666CC,
  316. 0xCCCC66, 0xCC66CC, 0x66CCCC, 0xDAAA00]
  317.  
  318. color = QColor(colorTable[shape])
  319. painter.fillRect(x + 1, y + 1, self.squareWidth() - 2,
  320. self.squareHeight() - 2, color)
  321.  
  322. painter.setPen(color.lighter())
  323. painter.drawLine(x, y + self.squareHeight() - 1, x, y)
  324. painter.drawLine(x, y, x + self.squareWidth() - 1, y)
  325.  
  326. painter.setPen(color.darker())
  327. painter.drawLine(x + 1, y + self.squareHeight() - 1,
  328. x + self.squareWidth() - 1, y + self.squareHeight() - 1)
  329. painter.drawLine(x + self.squareWidth() - 1,
  330. y + self.squareHeight() - 1, x + self.squareWidth() - 1, y + 1)
  331.  
  332.  
  333. class Tetrominoe(object):
  334.  
  335. NoShape = 0
  336. ZShape = 1
  337. SShape = 2
  338. LineShape = 3
  339. TShape = 4
  340. SquareShape = 5
  341. LShape = 6
  342. MirroredLShape = 7
  343.  
  344.  
  345. class Shape(object):
  346.  
  347. coordsTable = (
  348. ((0, 0), (0, 0), (0, 0), (0, 0)),
  349. ((0, -1), (0, 0), (-1, 0), (-1, 1)),
  350. ((0, -1), (0, 0), (1, 0), (1, 1)),
  351. ((0, -1), (0, 0), (0, 1), (0, 2)),
  352. ((-1, 0), (0, 0), (1, 0), (0, 1)),
  353. ((0, 0), (1, 0), (0, 1), (1, 1)),
  354. ((-1, -1), (0, -1), (0, 0), (0, 1)),
  355. ((1, -1), (0, -1), (0, 0), (0, 1))
  356. )
  357.  
  358. def _init_(self):
  359.  
  360. self.coords = [[0,0] for i in range(4)]
  361. self.pieceShape = Tetrominoe.NoShape
  362.  
  363. self.setShape(Tetrominoe.NoShape)
  364.  
  365.  
  366. def shape(self):
  367. return self.pieceShape
  368.  
  369.  
  370. def setShape(self, shape):
  371.  
  372. table = Shape.coordsTable[shape]
  373.  
  374. for i in range(4):
  375. for j in range(2):
  376. self.coords[i][j] = table[i][j]
  377.  
  378. self.pieceShape = shape
  379.  
  380.  
  381. def setRandomShape(self):
  382. self.setShape(random.randint(1, 7))
  383.  
  384.  
  385. def x(self, index):
  386. return self.coords[index][0]
  387.  
  388.  
  389. def y(self, index):
  390. return self.coords[index][1]
  391.  
  392.  
  393. def setX(self, index, x):
  394. self.coords[index][0] = x
  395.  
  396.  
  397. def setY(self, index, y):
  398. self.coords[index][1] = y
  399.  
  400.  
  401. def minX(self):
  402.  
  403. m = self.coords[0][0]
  404. for i in range(4):
  405. m = min(m, self.coords[i][0])
  406.  
  407. return m
  408.  
  409.  
  410. def maxX(self):
  411.  
  412. m = self.coords[0][0]
  413. for i in range(4):
  414. m = max(m, self.coords[i][0])
  415.  
  416. return m
  417.  
  418.  
  419. def minY(self):
  420.  
  421. m = self.coords[0][1]
  422. for i in range(4):
  423. m = min(m, self.coords[i][1])
  424.  
  425. return m
  426.  
  427.  
  428. def maxY(self):
  429.  
  430. m = self.coords[0][1]
  431. for i in range(4):
  432. m = max(m, self.coords[i][1])
  433.  
  434. return m
  435.  
  436.  
  437. def rotateLeft(self):
  438.  
  439. if self.pieceShape == Tetrominoe.SquareShape:
  440. return self
  441.  
  442. result = Shape()
  443. result.pieceShape = self.pieceShape
  444.  
  445. for i in range(4):
  446.  
  447. result.setX(i, self.y(i))
  448. result.setY(i, -self.x(i))
  449.  
  450. return result
  451.  
  452.  
  453. def rotateRight(self):
  454.  
  455. if self.pieceShape == Tetrominoe.SquareShape:
  456. return self
  457.  
  458. result = Shape()
  459. result.pieceShape = self.pieceShape
  460.  
  461. for i in range(4):
  462.  
  463. result.setX(i, -self.y(i))
  464. result.setY(i, self.x(i))
  465.  
  466. return result
  467.  
  468.  
  469. if _name_ == '_main_':
  470.  
  471. app = QApplication([])
  472. tetris = Tetris()
  473. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement