久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python如何寫個(gè)俄羅斯方塊

瀏覽:7日期:2022-07-06 10:05:05

俄羅斯方塊是俄羅斯人發(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()

看一下效果:

python如何寫個(gè)俄羅斯方塊

分?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

python如何寫個(gè)俄羅斯方塊

我們知道方塊是不斷自動(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)效果:

python如何寫個(gè)俄羅斯方塊

是不是有內(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)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 盐山县| 龙胜| 鄂尔多斯市| 彭州市| 阿拉善右旗| 屯留县| 古田县| 海阳市| 清新县| 五河县| 安塞县| 滕州市| 德格县| 平凉市| 临沧市| 汾阳市| 永福县| 韶关市| 繁峙县| 普格县| 聊城市| 灌云县| 肥城市| 伽师县| 体育| 黄大仙区| 潞城市| 岗巴县| 和硕县| 连山| 洞头县| 高青县| 怀柔区| 广饶县| 来凤县| 赞皇县| 钟山县| 玉门市| 鹿泉市| 汉阴县| 文登市|