当前位置: 首页 > 后端技术 > Python

用Python实现童年游戏《俄罗斯方块》

时间:2023-03-26 01:22:19 Python

在电子产品相对匮乏的年代,小型游戏机仍然是为数不多的游戏类电子产品之一,对孩子们来说是无法抗拒的。那个时候,要是哪个小朋??友买了我就买了个小游戏机,大家肯定赶紧围上去。。。俄罗斯方块作为小游戏之一,虽然规则简单,只有黑白两色,但是它对游戏玩家的影响在那时候不亚于LOL,打农药,吃鸡。现在游戏玩家的影响,我们来看看如何用Python实现俄罗斯方块这个小游戏。规则:由小方块组成的不同形状的盘子从屏幕上方依次落下。玩家调整板块的位置和方向,使它们在屏幕底部形成一条或几条完整的线。然后,这些完整的条块会消失,为新掉落的方块腾出空间,同时玩家会获得积分奖励。没有消除的块继续堆积。一旦堆到达屏幕顶部,玩家就输了,游戏结束。---百度百科环境操作系统:WindowsPython版本:3.6涉及模块:sys、random、PyQt5实现首先安装第三方模块PyQt5,使用pipinstallPyQt5即可。?游戏主界面实现??代码fromPyQt5.QtWidgetsimport*fromPyQt5.QtCoreimport*importsysclassMainBoard(QFrame):msg=pyqtSignal(str)BoardWidth=10BoardHeight=22Speed=300def__init__(self,parent):super().__init__(parent)self.initBoard()definitBoard(self):self.timer=QBasicTimer()self.isWaitingAfterLine=Falseself.curX=0self.curY=0self.numLinesRemoved=0self.board=[]self.setFocusPolicy(Qt.StrongFocus)self.isStarted=Falseself.isPaused=False效果图如下?小板定义小板形状classShapeForm(object):NoShape=0ZShape=1SShape=2LineShape=3TShape=4SquareShape=5LShape=6MirroredLShape=7classShape(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]foriinrange(4)]self.pieceShape=ShapeForm.NoShapeself.setShape(ShapeForm.NoShape)画出图形defdrawSquare(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,颜色)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)效果图如下?点击事件defkeyPressEvent(self,event):ifnotself.isStartedorself.curPiece.shape()==ShapeForm.NoShape:super(MainBoard,self).keyPressEvent(event)returnkey=event.key()ifkey==Qt.Key_P:self.pause()returnifself.isPaused:returnelifkey==Qt.Key_Left:self.tryMove(self.curPiece,self.curX-1,self.curY)elifkey==Qt.Key_Right:self.tryMove(self.curPiece,self.curX+1,self.curY)elifkey==Qt.Key_Down:self.tryMove(self.curPiece.rotateRight(),self.curX,self.curY)elifkey==Qt.Key_Up:self.tryMove(self.curPiece.rotateLeft(),self.curX,self.curY)elifkey==Qt.Key_Space:self.dropDown()el如果key==Qt.Key_D:self.oneLineDown()else:super(MainBoard,self).keyPressEvent(event)deftryMove(self,newPiece,newX,newY):foriinrange(4):x=newX+newPiece.x(i)y=newY-newPiece.y(i)如果x<0或x>=MainBoard.BoardWidth或y<0或y>=MainBoard.BoardHeight:如果self.shapeAt(x,y)则返回False!=ShapeForm.NoShape:returnFalseself.curPiece=newPieceself.curX=newXself.curY=newYself.update()returnTrue?计时器事件deftimerEvent(self,event):ifevent.timerId()==self.timer.timerId():ifself.isWaitingAfterLine:self.isWaitingAfterLine=Falseself.newPiece()else:self.oneLineDown()else:super(MainBoard,self).timerEvent(event)?开始和暂停defstart(self):如果self.isPaused:返回self.isStarted=Trueself.isWaitingAfterLine=Falseself.numLinesRemoved=0self.clearBoard()self.msg.emit(str(self.numLinesRemoved))self.newPiece()self.timer.start(MainBoard.Speed,self)def暂停(自我):如果不是self.isStarted:返回self.isPaused=notself.isPaused如果self.isPaused:self.timer.stop()self.msg.emit(“暂停”)否则:self.timer.start(MainBoard.Speed,self)self.msg.emit(str(self.numLinesRemoved))self.update()?游戏类及初始化classTetris(QMainWindow):def__init__(self):super().__init__()self.initUI()definitUI(self):self.tboard=MainBoard(self)self.setCentralWidget(self.tboard)self.statusbar=self.statusBar()self.tboard.msg[str].connect(self.statusbar.showMessage)self.tboard.start()self.resize(300,500)self.center()self.setWindowTitle('Tetris')self.show()defcenter(self):screen=QDesktopWidget().screenGeometry()size=self.geometry()self.move((screen.width()-size.width())/2,(screen.height()-size.height())/2)startif__name__=='__main__':app=QApplication([])tetris=Tetris()sys.exit(app.exec_())最后的效果就打包好了。为了方便操作,我们将Python文件打成exe文件,使用的插件是pyinstaller。首先,安装pyinstaller,使用pipinstallpyinstaller。安装完成后,在文件目录下打开命令窗口,执行命令pyinstaller--onefile--nowindowed--icon="C:\Users\LE\Desktop\tetris\tetris.ico"tetris.pyinthe命令窗口。执行完成后,我们可以在dist目录下找到生成的exe文件。可以在公众号Python小二后台回复g1获取源码。