python如何寫個(gè)俄羅斯方塊
俄羅斯方塊是俄羅斯人發(fā)明的一款休閑類的小游戲,這款小游戲可以說(shuō)是很多人童年的主打電子游戲了,本文我們使用 Python 來(lái)實(shí)現(xiàn)這款小游戲。
游戲的基本規(guī)則是:移動(dòng)、旋轉(zhuǎn)和擺放游戲自動(dòng)輸出的各種方塊,使之排列成完整的一行或多行并且消除得分。
實(shí)現(xiàn)
我們實(shí)現(xiàn)俄羅斯方塊,主要用到的是 PyQt5 庫(kù),安裝使用 pip install PyQt5 即可,游戲的組成比較簡(jiǎn)單,主要包括:主界面、各種方塊和計(jì)分板,下面我們來(lái)看一下具體實(shí)現(xiàn)。
首先,我們來(lái)畫一個(gè)主界面,主要實(shí)現(xiàn)代碼如下:
class MainBoard(QFrame): msg = pyqtSignal(str) BoardWidth = 10 BoardHeight = 20 Speed = 300 def __init__(self, parent): super().__init__(parent) self.initBoard() def initBoard(self): self.timer = QBasicTimer() self.isWaitingAfterLine = False self.curX = 0 self.curY = 0 self.numLinesRemoved = 0 self.board = [] self.setFocusPolicy(Qt.StrongFocus) self.isStarted = False self.isPaused = False self.clearBoard()
看一下效果:
分?jǐn)?shù)的顯示就是利用上面 msg 的 emit() 方法實(shí)現(xiàn)的。
我們接著畫各種方塊,方塊的形狀主要包括:T、Z、L、I、O 等,主要實(shí)現(xiàn)代碼如下:
class ShapeForm(object): NoShape = 0 ZShape = 1 SShape = 2 LineShape = 3 TShape = 4 SquareShape = 5 LShape = 6 MirroredLShape = 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 = ShapeForm.NoShape self.setShape(ShapeForm.NoShape) def shape(self): return self.pieceShape def 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 = shape
我們知道方塊是不斷自動(dòng)下落的,因此需要一個(gè)計(jì)時(shí)器來(lái)控制,主要實(shí)現(xiàn)代碼如下:
def timerEvent(self, event):if event.timerId() == self.timer.timerId():if self.isWaitingAfterLine:self.isWaitingAfterLine = Falseself.newPiece()else:self.oneLineDown()else:super(MainBoard, self).timerEvent(event)
在方塊下落的過程中,我們需要通過鍵盤來(lái)控制方塊的形狀以及左右移動(dòng),因此,我們需要一個(gè)按鍵事件來(lái)控制它,主要實(shí)現(xiàn)代碼如下:
def keyPressEvent(self, event):if not self.isStarted or self.curPiece.shape() == ShapeForm.NoShape:super(MainBoard, 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(MainBoard, self).keyPressEvent(event)
當(dāng)方塊落到底部后,需要來(lái)檢測(cè)是否有構(gòu)成一條直線的,因此我們需要有一個(gè)方法來(lái)找到所有能消除的行并且消除它們,主要實(shí)現(xiàn)代碼如下:
def removeFullLines(self):numFullLines = 0rowsToRemove = []for i in range(MainBoard.BoardHeight):n = 0for j in range(MainBoard.BoardWidth):if not self.shapeAt(j, i) == ShapeForm.NoShape:n = n + 1if n == 10:rowsToRemove.append(i)rowsToRemove.reverse()for m in rowsToRemove:for k in range(m, MainBoard.BoardHeight):for l in range(MainBoard.BoardWidth):self.setShapeAt(l, k, self.shapeAt(l, k + 1))numFullLines = numFullLines + len(rowsToRemove)if numFullLines > 0:self.numLinesRemoved = self.numLinesRemoved + numFullLinesself.msg.emit(str(self.numLinesRemoved))self.isWaitingAfterLine = Trueself.curPiece.setShape(ShapeForm.NoShape)self.update()
我們來(lái)看一下最終實(shí)現(xiàn)效果:
是不是有內(nèi)味了。
總結(jié)
本文我們使用 PyQt5 庫(kù)寫了一個(gè)俄羅斯方塊小游戲,如果你對(duì) PyQt5 庫(kù)感興趣的話,可以嘗試使用一下。
示例代碼:py-tetris
以上就是python寫個(gè)俄羅斯方塊的詳細(xì)內(nèi)容,更多關(guān)于python 俄羅斯方塊的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. XML入門精解之結(jié)構(gòu)與語(yǔ)法3. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享4. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁(yè)的方法5. PHP循環(huán)與分支知識(shí)點(diǎn)梳理6. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享7. UDDI FAQs8. 使用css實(shí)現(xiàn)全兼容tooltip提示框9. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效10. 匹配模式 - XSL教程 - 4
