转载自ZetCode PyQt5 tutorial,修改为PySide2版方便在maya2017中运行
"""
ZetCode PyQt5 tutorialThis is a Tetris game clone.author: Jan Bodnar
website: zetcode.com
last edited: January 2015
"""import sys, random
from PySide2.QtWidgets import QMainWindow, QFrame, QDesktopWidget, QApplication
from PySide2.QtCore import Qt, QBasicTimer, Signal
from PySide2.QtGui import QPainter, QColorclass Tetris(QMainWindow):def __init__(self):super(Tetris, self).__init__()self.initUI()def initUI(self):self.tboard = Board(self)self.setCentralWidget(self.tboard)self.statusbar = self.statusBar()self.tboard.msg2Statusbar[str].connect(self.statusbar.showMessage)self.tboard.start()self.resize(180, 380)self.center()self.setWindowTitle('Tetris')self.show()def center(self):screen = QDesktopWidget().screenGeometry()size = self.geometry()self.move((screen.width() - size.width()) / 2,(screen.height() - size.height()) / 2)class Board(QFrame):msg2Statusbar = Signal(str)BoardWidth = 10BoardHeight = 22Speed = 300def __init__(self, parent):super(Board, self).__init__(parent)self.initBoard()def initBoard(self):self.timer = QBasicTimer()self.isWaitingAfterLine = Falseself.curX = 0self.curY = 0self.numLinesRemoved = 0self.board = []self.setFocusPolicy(Qt.StrongFocus)self.isStarted = Falseself.isPaused = Falseself.clearBoard()def shapeAt(self, x, y):return self.board[(y * Board.BoardWidth) + x]def setShapeAt(self, x, y, shape):self.board[(y * Board.BoardWidth) + x] = shapedef squareWidth(self):return self.contentsRect().width() // Board.BoardWidthdef squareHeight(self):return self.contentsRect().height() // Board.BoardHeightdef start(self):if self.isPaused:returnself.isStarted = Trueself.isWaitingAfterLine = Falseself.numLinesRemoved = 0self.clearBoard()self.msg2Statusbar.emit(str(self.numLinesRemoved))self.newPiece()self.timer.start(Board.Speed, self)def pause(self):if not self.isStarted:returnself.isPaused = not self.isPausedif self.isPaused:self.timer.stop()self.msg2Statusbar.emit("paused")else:self.timer.start(Board.Speed, self)self.msg2Statusbar.emit(str(self.numLinesRemoved))self.update()def paintEvent(self, event):painter = QPainter(self)rect = self.contentsRect()boardTop = rect.bottom() - Board.BoardHeight * self.squareHeight()for i in range(Board.BoardHeight):for j in range(Board.BoardWidth):shape = self.shapeAt(j, Board.BoardHeight - i - 1)if shape != Tetrominoe.NoShape:self.drawSquare(painter,rect.left() + j * self.squareWidth(),boardTop + i * self.squareHeight(), shape)if self.curPiece.shape() != Tetrominoe.NoShape:for i in range(4):x = self.curX + self.curPiece.x(i)y = self.curY - self.curPiece.y(i)self.drawSquare(painter, rect.left() + x * self.squareWidth(),boardTop + (Board.BoardHeight - y - 1) * self.squareHeight(),self.curPiece.shape())def keyPressEvent(self, event):if not self.isStarted or self.curPiece.shape() == Tetrominoe.NoShape:super(Board, self).keyPressEvent(event)returnkey = event.key()if key == Qt.Key_P:self.pause()returnif self.isPaused:returnelif key == Qt.Key_Left:self.tryMove(self.curPiece, self.curX - 1, self.curY)elif key == Qt.Key_Right:self.tryMove(self.curPiece, self.curX + 1, self.curY)elif key == Qt.Key_Down:self.tryMove(self.curPiece.rotateRight(), self.curX, self.curY)elif key == Qt.Key_Up:self.tryMove(self.curPiece.rotateLeft(), self.curX, self.curY)elif key == Qt.Key_Space:self.dropDown()elif key == Qt.Key_D:self.oneLineDown()else:super(Board, self).keyPressEvent(event)def timerEvent(self, event):if event.timerId() == self.timer.timerId():if self.isWaitingAfterLine:self.isWaitingAfterLine = Falseself.newPiece()else:self.oneLineDown()else:super(Board, self).timerEvent(event)def clearBoard(self):for i in range(Board.BoardHeight * Board.BoardWidth):self.board.append(Tetrominoe.NoShape)def dropDown(self):newY = self.curYwhile newY > 0:if not self.tryMove(self.curPiece, self.curX, newY - 1):breaknewY -= 1self.pieceDropped()def oneLineDown(self):if not self.tryMove(self.curPiece, self.curX, self.curY - 1):self.pieceDropped()def pieceDropped(self):for i in range(4):x = self.curX + self.curPiece.x(i)y = self.curY - self.curPiece.y(i)self.setShapeAt(x, y, self.curPiece.shape())self.removeFullLines()if not self.isWaitingAfterLine:self.newPiece()def removeFullLines(self):numFullLines = 0rowsToRemove = []for i in range(Board.BoardHeight):n = 0for j in range(Board.BoardWidth):if not self.shapeAt(j, i) == Tetrominoe.NoShape:n = n + 1if n == 10:rowsToRemove.append(i)rowsToRemove.reverse()for m in rowsToRemove:for k in range(m, Board.BoardHeight):for l in range(Board.BoardWidth):self.setShapeAt(l, k, self.shapeAt(l, k + 1))numFullLines = numFullLines + len(rowsToRemove)if numFullLines > 0:self.numLinesRemoved = self.numLinesRemoved + numFullLinesself.msg2Statusbar.emit(str(self.numLinesRemoved))self.isWaitingAfterLine = Trueself.curPiece.setShape(Tetrominoe.NoShape)self.update()def newPiece(self):self.curPiece = Shape()self.curPiece.setRandomShape()self.curX = Board.BoardWidth // 2 + 1self.curY = Board.BoardHeight - 1 + self.curPiece.minY()if not self.tryMove(self.curPiece, self.curX, self.curY):self.curPiece.setShape(Tetrominoe.NoShape)self.timer.stop()self.isStarted = Falseself.msg2Statusbar.emit("Game over")def tryMove(self, newPiece, newX, newY):for i in range(4):x = newX + newPiece.x(i)y = newY - newPiece.y(i)if x < 0 or x >= Board.BoardWidth or y < 0 or y >= Board.BoardHeight:return Falseif self.shapeAt(x, y) != Tetrominoe.NoShape:return Falseself.curPiece = newPieceself.curX = newXself.curY = newYself.update()return Truedef drawSquare(self, painter, x, y, shape):colorTable = [0x000000, 0xCC6666, 0x66CC66, 0x6666CC,0xCCCC66, 0xCC66CC, 0x66CCCC, 0xDAAA00]color = QColor(colorTable[shape])painter.fillRect(x + 1, y + 1, self.squareWidth() - 2,self.squareHeight() - 2, color)painter.setPen(color.lighter())painter.drawLine(x, y + self.squareHeight() - 1, x, y)painter.drawLine(x, y, x + self.squareWidth() - 1, y)painter.setPen(color.darker())painter.drawLine(x + 1, y + self.squareHeight() - 1,x + self.squareWidth() - 1, y + self.squareHeight() - 1)painter.drawLine(x + self.squareWidth() - 1,y + self.squareHeight() - 1, x + self.squareWidth() - 1, y + 1)class Tetrominoe(object):NoShape = 0ZShape = 1SShape = 2LineShape = 3TShape = 4SquareShape = 5LShape = 6MirroredLShape = 7class Shape(object):coordsTable = (((0, 0), (0, 0), (0, 0), (0, 0)),((0, -1), (0, 0), (-1, 0), (-1, 1)),((0, -1), (0, 0), (1, 0), (1, 1)),((0, -1), (0, 0), (0, 1), (0, 2)),((-1, 0), (0, 0), (1, 0), (0, 1)),((0, 0), (1, 0), (0, 1), (1, 1)),((-1, -1), (0, -1), (0, 0), (0, 1)),((1, -1), (0, -1), (0, 0), (0, 1)))def __init__(self):self.coords = [[0, 0] for i in range(4)]self.pieceShape = Tetrominoe.NoShapeself.setShape(Tetrominoe.NoShape)def shape(self):return self.pieceShapedef setShape(self, shape):table = Shape.coordsTable[shape]for i in range(4):for j in range(2):self.coords[i][j] = table[i][j]self.pieceShape = shapedef setRandomShape(self):self.setShape(random.randint(1, 7))def x(self, index):return self.coords[index][0]def y(self, index):return self.coords[index][1]def setX(self, index, x):self.coords[index][0] = xdef setY(self, index, y):self.coords[index][1] = ydef minX(self):m = self.coords[0][0]for i in range(4):m = min(m, self.coords[i][0])return mdef maxX(self):m = self.coords[0][0]for i in range(4):m = max(m, self.coords[i][0])return mdef minY(self):m = self.coords[0][1]for i in range(4):m = min(m, self.coords[i][1])return mdef maxY(self):m = self.coords[0][1]for i in range(4):m = max(m, self.coords[i][1])return mdef rotateLeft(self):if self.pieceShape == Tetrominoe.SquareShape:return selfresult = Shape()result.pieceShape = self.pieceShapefor i in range(4):result.setX(i, self.y(i))result.setY(i, -self.x(i))return resultdef rotateRight(self):if self.pieceShape == Tetrominoe.SquareShape:return selfresult = Shape()result.pieceShape = self.pieceShapefor i in range(4):result.setX(i, -self.y(i))result.setY(i, self.x(i))return resultif __name__ == '__main__':#app = QApplication([])tetris = Tetris()#sys.exit(app.exec_())



												

在Maya2017中玩俄罗斯方块相关推荐

  1. 如何让AI教机器自己玩俄罗斯方块?

    作者 | Ahab 转载自公众号Ahab杂货铺(ID:PythonLearningCamp) 人工智能大火的今天,如果还是自己玩俄罗斯方块未免显得太 LOW,为什么不对游戏升级,让机器自己去玩俄罗斯方 ...

  2. 让 AI 教机器自己玩俄罗斯方块

    作者 | Ahab 责编 | 仲培艺 人工智能大火的今天,如果还是自己玩俄罗斯方块未免显得太 LOW,为什么不对游戏升级,让机器自己去玩俄罗斯方块呢?有了这个想法之后,我用了两天时间去搜集了大量资料, ...

  3. 划水神器--在浏览器地址栏和标签页title里面玩俄罗斯方块

    划水神器–在浏览器地址栏和标签页title里面玩俄罗斯方块 效果如图: 在线体验 1. 原理 URL地址栏中玩:使用window.location.hash动态修改页面的hash值 标签页中玩:使用d ...

  4. 俄罗斯方块linux服务器,安装Tint以在Linux终端下玩俄罗斯方块

    如果你想在Linux终端(Linux命令行)下玩俄罗斯方块游戏,那就安装Tint,它可以满足你的需求.Tint尝试尽可能地转向原始游戏,但有一些显着的差异,例如下一个区块没有预览,并且暂停游戏没有选项 ...

  5. AI玩俄罗斯方块(Python实现)

    目录 1.环境 2.实现机制(Pierre Dellacherie算法) 3.代码实现 人工智能大火的今天,如果还是自己玩俄罗斯方块未免显得太LOW,为什么不对游戏升级,让机器自己去玩俄罗斯方块呢?有 ...

  6. 管理 zabbix_Zabbix 2019 峰会丨看睿象云如何在 Zabbix 中玩转告警

    2019年11月29日-30日,为期两天的 Zabbix 大会中国站在北京盛大召开,本届 Zabbix 大会以"新视界,新技术,共建未来新监控!"为主题,为与会人员提供前沿的监控技 ...

  7. 无冬连接不上账号服务器,电脑中玩无冬online掉线怎么回事_电脑中玩无冬online游戏总是掉线如何修复...

    无冬online是一款大型网络游戏,很多玩家都很喜欢在电脑中安装玩耍,在游戏过程中,想必很多用户经常会遇到这样一个问题,就是在电脑中玩无冬online游戏的时候,经常会出现掉线的情况,导致每次游戏都会 ...

  8. c语言启动程序句柄无效,电脑中玩英雄联盟提示“句柄无效”是怎么回事

    电脑中玩英雄联盟提示"句柄无效"是怎么回事?一位用户反馈自己在玩英雄联盟游戏时,突然遇到警告音提示,将游戏进行最小化后,看到桌面上不停的出现提示"句柄无效",感 ...

  9. 如何在 Spring 生态中玩转 RocketMQ?

    作者 | 通融.洛夜 来源 | 阿里巴巴云原生公众号 RocketMQ 作为业务消息的首选,在消息和流处理领域被广泛应用.而微服务生态 Spring 框架也是业务开发中最受欢迎的框架,两者的完美契合使 ...

最新文章

  1. ubuntu——python
  2. android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序
  3. 算法分析结课总结--回溯算法
  4. 小米纵向拓展接力赛 接棒新国货“热水器”
  5. SpringBoot系列: RestTemplate 快速入门
  6. POJ 2230 Watchcow 欧拉回路的DFS解法(模板题)
  7. 比其他行业晚了十年的工业软件,转型的核心和动力是什么?
  8. 蔚来与雷蛇联合推出NIO ES6限量版车型 售价46.78万元
  9. [RK3399][Android7.1] 调试笔记 --- 虚拟声卡驱动添加
  10. AI机器人AI源码营销机器人电销机器人智能电话机器人拨号机器人语音机器人空号识别FreeSWITCH呼叫中心中间ipbxIPBX科大识别阿里识别语音识别语音翻译
  11. 从程序员到项目经理转自西门吹雪
  12. Adobe reader update 无法将数值disableexceptionchainvaliddation写入键/sofeware...请验证您对该有足够
  13. Mac如何读写NTFS硬盘,NTFSTool让Mac也可以轻松读写NTFS硬盘
  14. iOS开发证书、bundle ID、App ID、描述文件、p12文件,及企业证书打包发布详述
  15. SQL查询语句的使用
  16. [VB.NET]设置TextBox的提示文字
  17. html 颜色 excel,Excel~常用颜色对照表
  18. php printer_open 用法_使用printer_write()函数直接从PHP打印
  19. jxnu-linux 实验九
  20. Apache poi文件读取

热门文章

  1. 「macOS」我的终端我做主——iTerm2+Homebrew+oh-my-zsh+Powerline安装及简单配置
  2. 芯片、模组、开发板的区别与联系-结合ESP32浅谈
  3. SAP公司间STO里发货单过账后触发的IDoc报错 – Could not find code page for receiving system –
  4. Windows非开机状态下尝试进入注册表解决Fixing(E:) Stage问题
  5. 肝2022世界杯,怒写企业级镜像私仓Docker+Harbor实践
  6. kepserver写入mysql_Kepserver连接Mysql教程(三)Kepserver 数据写入mysql数据库
  7. 联想笔记本桌面计算机图标,联想笔记本如何找回丢失的“显示桌面”图标
  8. Chrome插件离线安装 + Chrono下载管理器
  9. windows图形编程基础
  10. 计算机一级 photoshow,PerfectPhotoShow