当前位置:   article > 正文

免费分享一套Python俄罗斯方块源码 PyQt5俄罗斯方块源码,太好玩了~

免费分享一套Python俄罗斯方块源码 PyQt5俄罗斯方块源码,太好玩了~

大家好,我是java1234_小锋老师,看到一个不错的Python俄罗斯方块源码 PyQt5俄罗斯方块源码,分享下哈。

项目视频演示

【免费】Python俄罗斯方块源码 PyQt5俄罗斯方块源码 Python小游戏源码_哔哩哔哩_bilibili【免费】Python俄罗斯方块源码 PyQt5俄罗斯方块源码 Python小游戏源码项目来自互联网,免费开源分享,严禁商业。更多Python源码:http://www.java1234.com/a/bysj/python/, 视频播放量 238、弹幕量 0、点赞数 4、投硬币枚数 3、收藏人数 7、转发人数 2, 视频作者 java1234官方, 作者简介 公众号:java1234 微信:java9266,相关视频:美国13岁少年通关原版俄罗斯方块,此前只有人工智能能做到,2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~,探访上海猛哥总部:阿帮点点两两美丽的震撼一刻!,3D动画:解压彩色版-软体俄罗斯方块,【附PyCharm激活码】最新Python+PyCharm安装激活教程,提供安装包+激活码,一键激活,永久使用,小白也能学得会!python安装包,【免费】PyQt5 学生信息管理系统 Python管理系统 Python源码 Python毕业设计,PyQt6图书管理系统视频教程 Python桌面开发 Python入门级项目实战 (无废话版) 火爆连载更新中~,意义怪出没!新闻主播抨击首位通关俄罗斯方块13岁男孩 遭网友怒喷,【Python脚本】怎么破解WiFi手机版,一分钟教你如何用Python脚本破解WiFi手机版,俄罗斯方块icon-default.png?t=N7T8https://www.bilibili.com/video/BV1Pe411n7w8/

项目介绍

《俄罗斯方块》原本是前苏联科学家阿列克谢·帕基特诺夫所开发的教育用软件,之后开始提供授权给各个游戏公司,造成各平台上软件大量发行的现象。

Game Boy版的俄罗斯方块在日本卖出424万套,是Game Boy史上卖最好的游戏。海湾战争时,也是前线美军最常拿消磨时间的游戏之一。

由于俄罗斯方块具有的数学性、动态性与知名度,也经常拿来作为游戏程序设计的练习题材。

系统展示

部分代码

  1. import sys, random
  2. from PyQt5.QtWidgets import QMainWindow, QFrame, QDesktopWidget, QApplication
  3. from PyQt5.QtCore import Qt, QBasicTimer, pyqtSignal
  4. from PyQt5.QtGui import QPainter, QColor
  5. class Tetris(QMainWindow):
  6. def __init__(self):
  7. super().__init__()
  8. self.initUI()
  9. class Board(QFrame):
  10. msg2Statusbar = pyqtSignal(str)
  11. BoardWidth = 10
  12. BoardHeight = 22
  13. Speed = 300
  14. def __init__(self, parent):
  15. super().__init__(parent)
  16. self.initBoard()
  17. def initBoard(self):
  18. self.timer = QBasicTimer()
  19. self.isWaitingAfterLine = False
  20. self.curX = 0
  21. self.curY = 0
  22. self.numLinesRemoved = 0
  23. self.board = []
  24. self.setFocusPolicy(Qt.StrongFocus)
  25. self.isStarted = False
  26. self.isPaused = False
  27. self.clearBoard()
  28. def shapeAt(self, x, y):
  29. return self.board[(y * Board.BoardWidth) + x]
  30. def setShapeAt(self, x, y, shape):
  31. self.board[(y * Board.BoardWidth) + x] = shape
  32. def squareWidth(self):
  33. return self.contentsRect().width() // Board.BoardWidth
  34. def squareHeight(self):
  35. return self.contentsRect().height() // Board.BoardHeight
  36. def start(self):
  37. if self.isPaused:
  38. return
  39. self.isStarted = True
  40. self.isWaitingAfterLine = False
  41. self.numLinesRemoved = 0
  42. self.clearBoard()
  43. self.msg2Statusbar.emit(str(self.numLinesRemoved))
  44. self.newPiece()
  45. self.timer.start(Board.Speed, self)
  46. def pause(self):
  47. if not self.isStarted:
  48. return
  49. self.isPaused = not self.isPaused
  50. if self.isPaused:
  51. self.timer.stop()
  52. self.msg2Statusbar.emit("paused")
  53. else:
  54. self.timer.start(Board.Speed, self)
  55. self.msg2Statusbar.emit(str(self.numLinesRemoved))
  56. self.update()
  57. def paintEvent(self, event):
  58. painter = QPainter(self)
  59. rect = self.contentsRect()
  60. boardTop = rect.bottom() - Board.BoardHeight * self.squareHeight()
  61. for i in range(Board.BoardHeight):
  62. for j in range(Board.BoardWidth):
  63. shape = self.shapeAt(j, Board.BoardHeight - i - 1)
  64. if shape != Tetrominoe.NoShape:
  65. self.drawSquare(painter,
  66. rect.left() + j * self.squareWidth(),
  67. boardTop + i * self.squareHeight(), shape)
  68. if self.curPiece.shape() != Tetrominoe.NoShape:
  69. for i in range(4):
  70. x = self.curX + self.curPiece.x(i)
  71. y = self.curY - self.curPiece.y(i)
  72. self.drawSquare(painter, rect.left() + x * self.squareWidth(),
  73. boardTop + (Board.BoardHeight - y - 1) * self.squareHeight(),
  74. self.curPiece.shape())
  75. def keyPressEvent(self, event):
  76. if not self.isStarted or self.curPiece.shape() == Tetrominoe.NoShape:
  77. super(Board, self).keyPressEvent(event)
  78. return
  79. key = event.key()
  80. if key == Qt.Key_P:
  81. self.pause()
  82. return
  83. if self.isPaused:
  84. return
  85. elif key == Qt.Key_Left:
  86. self.tryMove(self.curPiece, self.curX - 1, self.curY)
  87. elif key == Qt.Key_Right:
  88. self.tryMove(self.curPiece, self.curX + 1, self.curY)
  89. elif key == Qt.Key_Down:
  90. self.tryMove(self.curPiece.rotateRight(), self.curX, self.curY)
  91. elif key == Qt.Key_Up:
  92. self.tryMove(self.curPiece.rotateLeft(), self.curX, self.curY)
  93. elif key == Qt.Key_Space:
  94. self.dropDown()
  95. elif key == Qt.Key_D:
  96. self.oneLineDown()
  97. else:
  98. super(Board, self).keyPressEvent(event)
  99. def timerEvent(self, event):
  100. if event.timerId() == self.timer.timerId():
  101. if self.isWaitingAfterLine:
  102. self.isWaitingAfterLine = False
  103. self.newPiece()
  104. else:
  105. self.oneLineDown()
  106. else:
  107. super(Board, self).timerEvent(event)
  108. def clearBoard(self):
  109. for i in range(Board.BoardHeight * Board.BoardWidth):
  110. self.board.append(Tetrominoe.NoShape)
  111. def dropDown(self):
  112. newY = self.curY
  113. while newY > 0:
  114. if not self.tryMove(self.curPiece, self.curX, newY - 1):
  115. break
  116. newY -= 1
  117. self.pieceDropped()
  118. def oneLineDown(self):
  119. if not self.tryMove(self.curPiece, self.curX, self.curY - 1):
  120. self.pieceDropped()
  121. def pieceDropped(self):
  122. for i in range(4):
  123. x = self.curX + self.curPiece.x(i)
  124. y = self.curY - self.curPiece.y(i)
  125. self.setShapeAt(x, y, self.curPiece.shape())
  126. self.removeFullLines()
  127. if not self.isWaitingAfterLine:
  128. self.newPiece()
  129. def removeFullLines(self):
  130. numFullLines = 0
  131. rowsToRemove = []
  132. for i in range(Board.BoardHeight):
  133. n = 0
  134. for j in range(Board.BoardWidth):
  135. if not self.shapeAt(j, i) == Tetrominoe.NoShape:
  136. n = n + 1
  137. if n == 10:
  138. rowsToRemove.append(i)
  139. rowsToRemove.reverse()
  140. for m in rowsToRemove:
  141. for k in range(m, Board.BoardHeight):
  142. for l in range(Board.BoardWidth):
  143. self.setShapeAt(l, k, self.shapeAt(l, k + 1))
  144. numFullLines = numFullLines + len(rowsToRemove)
  145. if numFullLines > 0:
  146. self.numLinesRemoved = self.numLinesRemoved + numFullLines
  147. self.msg2Statusbar.emit(str(self.numLinesRemoved))
  148. self.isWaitingAfterLine = True
  149. self.curPiece.setShape(Tetrominoe.NoShape)
  150. self.update()
  151. def newPiece(self):
  152. self.curPiece = Shape()
  153. self.curPiece.setRandomShape()
  154. self.curX = Board.BoardWidth // 2 + 1
  155. self.curY = Board.BoardHeight - 1 + self.curPiece.minY()
  156. if not self.tryMove(self.curPiece, self.curX, self.curY):
  157. self.curPiece.setShape(Tetrominoe.NoShape)
  158. self.timer.stop()
  159. self.isStarted = False
  160. self.msg2Statusbar.emit("Game over")
  161. def tryMove(self, newPiece, newX, newY):
  162. for i in range(4):
  163. x = newX + newPiece.x(i)
  164. y = newY - newPiece.y(i)
  165. if x < 0 or x >= Board.BoardWidth or y < 0 or y >= Board.BoardHeight:
  166. return False
  167. if self.shapeAt(x, y) != Tetrominoe.NoShape:
  168. return False
  169. self.curPiece = newPiece
  170. self.curX = newX
  171. self.curY = newY
  172. self.update()
  173. return True
  174. def drawSquare(self, painter, x, y, shape):
  175. colorTable = [0x000000, 0xCC6666, 0x66CC66, 0x6666CC,
  176. 0xCCCC66, 0xCC66CC, 0x66CCCC, 0xDAAA00]
  177. color = QColor(colorTable[shape])
  178. painter.fillRect(x + 1, y + 1, self.squareWidth() - 2,
  179. self.squareHeight() - 2, color)
  180. painter.setPen(color.lighter())
  181. painter.drawLine(x, y + self.squareHeight() - 1, x, y)
  182. painter.drawLine(x, y, x + self.squareWidth() - 1, y)
  183. painter.setPen(color.darker())
  184. painter.drawLine(x + 1, y + self.squareHeight() - 1,
  185. x + self.squareWidth() - 1, y + self.squareHeight() - 1)
  186. painter.drawLine(x + self.squareWidth() - 1,
  187. y + self.squareHeight() - 1, x + self.squareWidth() - 1, y + 1)
  188. class Tetrominoe(object):
  189. NoShape = 0
  190. ZShape = 1
  191. SShape = 2
  192. LineShape = 3
  193. TShape = 4
  194. SquareShape = 5
  195. LShape = 6
  196. MirroredLShape = 7
  197. class Shape(object):
  198. coordsTable = (
  199. ((0, 0), (0, 0), (0, 0), (0, 0)),
  200. ((0, -1), (0, 0), (-1, 0), (-1, 1)),
  201. ((0, -1), (0, 0), (1, 0), (1, 1)),
  202. ((0, -1), (0, 0), (0, 1), (0, 2)),
  203. ((-1, 0), (0, 0), (1, 0), (0, 1)),
  204. ((0, 0), (1, 0), (0, 1), (1, 1)),
  205. ((-1, -1), (0, -1), (0, 0), (0, 1)),
  206. ((1, -1), (0, -1), (0, 0), (0, 1))
  207. )
  208. def __init__(self):
  209. self.coords = [[0, 0] for i in range(4)]
  210. self.pieceShape = Tetrominoe.NoShape
  211. self.setShape(Tetrominoe.NoShape)
  212. def shape(self):
  213. return self.pieceShape
  214. def setShape(self, shape):
  215. table = Shape.coordsTable[shape]
  216. for i in range(4):
  217. for j in range(2):
  218. self.coords[i][j] = table[i][j]
  219. self.pieceShape = shape
  220. def setRandomShape(self):
  221. self.setShape(random.randint(1, 7))
  222. def x(self, index):
  223. return self.coords[index][0]
  224. def y(self, index):
  225. return self.coords[index][1]
  226. def setX(self, index, x):
  227. self.coords[index][0] = x
  228. def setY(self, index, y):
  229. self.coords[index][1] = y
  230. def minX(self):
  231. m = self.coords[0][0]
  232. for i in range(4):
  233. m = min(m, self.coords[i][0])
  234. return m
  235. def maxX(self):
  236. m = self.coords[0][0]
  237. for i in range(4):
  238. m = max(m, self.coords[i][0])
  239. return m
  240. def minY(self):
  241. m = self.coords[0][1]
  242. for i in range(4):
  243. m = min(m, self.coords[i][1])
  244. return m
  245. def maxY(self):
  246. m = self.coords[0][1]
  247. for i in range(4):
  248. m = max(m, self.coords[i][1])
  249. return m
  250. def rotateLeft(self):
  251. if self.pieceShape == Tetrominoe.SquareShape:
  252. return self
  253. result = Shape()
  254. result.pieceShape = self.pieceShape
  255. for i in range(4):
  256. result.setX(i, self.y(i))
  257. result.setY(i, -self.x(i))
  258. return result
  259. def rotateRight(self):
  260. if self.pieceShape == Tetrominoe.SquareShape:
  261. return self
  262. result = Shape()
  263. result.pieceShape = self.pieceShape
  264. for i in range(4):
  265. result.setX(i, -self.y(i))
  266. result.setY(i, self.x(i))
  267. return result
  268. if __name__ == '__main__':
  269. app = QApplication([])
  270. tetris = Tetris()
  271. sys.exit(app.exec_())

源码下载

CSDN 1积分下载:https://download.csdn.net/download/caofeng891102/88765569

或者免费领取加小锋老师wx:java9266

热门推荐

免费分享一套PyQt6学生信息管理系统 Python管理系统 Python源码,挺漂亮的-CSDN博客文章浏览阅读2.4k次,点赞82次,收藏36次。期末作业要求用Qt做一个学生管理系统,笔者这段时间在自学Python,故在征求老师同意后不用C++而选择了Python的PyQt5,本项目主要实现了PyQt5的增删改查。因为学习PyQt5的时间短,写代码时遇到了很多困难,笔者在CSDN与Github上我查阅了大量的资料,最终做了一个半成品出来。但笔者认为,以这个小项目作为一个入门练手项目十分合适,现将其分享,希望能够帮助到新手入门的人。本项目主要用到了PyQt5、pymysql、xlwt模块、Mysql数据库、QtDesigner和pyuic。https://blog.csdn.net/caoli201314/article/details/135313681

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/49996
推荐阅读
相关标签
  

闽ICP备14008679号