python qt制作2048代码
时间: 2023-08-28 15:06:53 浏览: 91
以下是使用Python和PyQt5制作2048游戏的示例代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QGridLayout, QFrame, QLabel
from PyQt5.QtGui import QColor, QFont
class Block(QFrame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setFixedSize(80, 80)
self.setStyleSheet("background-color: #ccc")
self.label = QLabel("", self)
self.label.setGeometry(0, 0, 80, 80)
self.label.setAlignment(Qt.AlignCenter)
self.label.setFont(QFont("Arial", 36))
def setNumber(self, number):
self.number = number
if self.number == 0:
self.label.setText("")
self.setStyleSheet("background-color: #ccc")
else:
self.label.setText(str(self.number))
if self.number == 2:
self.setStyleSheet("background-color: #eee")
self.label.setStyleSheet("color: #555")
elif self.number == 4:
self.setStyleSheet("background-color: #eee")
self.label.setStyleSheet("color: #555")
elif self.number == 8:
self.setStyleSheet("background-color: #f5a45d")
self.label.setStyleSheet("color: #fff")
elif self.number == 16:
self.setStyleSheet("background-color: #f68a5c")
self.label.setStyleSheet("color: #fff")
elif self.number == 32:
self.setStyleSheet("background-color: #f66e3d")
self.label.setStyleSheet("color: #fff")
elif self.number == 64:
self.setStyleSheet("background-color: #f64c2e")
self.label.setStyleSheet("color: #fff")
elif self.number == 128:
self.setStyleSheet("background-color: #edcf72")
self.label.setStyleSheet("color: #fff")
elif self.number == 256:
self.setStyleSheet("background-color: #edcc61")
self.label.setStyleSheet("color: #fff")
elif self.number == 512:
self.setStyleSheet("background-color: #edc850")
self.label.setStyleSheet("color: #fff")
elif self.number == 1024:
self.setStyleSheet("background-color: #edc53f")
self.label.setStyleSheet("color: #fff")
elif self.number == 2048:
self.setStyleSheet("background-color: #edc22e")
self.label.setStyleSheet("color: #fff")
class Game(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
self.initBoard()
self.show()
def initUI(self):
self.setWindowTitle("2048")
self.setFixedSize(420, 520)
self.grid = QGridLayout()
self.grid.setSpacing(10)
centralWidget = QFrame()
centralWidget.setLayout(self.grid)
self.setCentralWidget(centralWidget)
def initBoard(self):
self.board = [[0 for j in range(4)] for i in range(4)]
self.addBlock()
self.addBlock()
self.updateBoard()
def addBlock(self):
empty_blocks = [(i, j) for i in range(4) for j in range(4) if self.board[i][j] == 0]
if empty_blocks:
i, j = empty_blocks[randint(0, len(empty_blocks) - 1)]
self.board[i][j] = 2
def updateBoard(self):
for i in range(4):
for j in range(4):
block = Block()
block.setNumber(self.board[i][j])
self.grid.addWidget(block, i, j)
def moveLeft(self):
for i in range(4):
for j in range(1, 4):
if self.board[i][j] != 0:
k = j
while k > 0 and self.board[i][k - 1] == 0:
k -= 1
if k != j:
self.board[i][k] = self.board[i][j]
self.board[i][j] = 0
self.addBlock()
self.updateBoard()
def moveRight(self):
for i in range(4):
for j in range(2, -1, -1):
if self.board[i][j] != 0:
k = j
while k < 3 and self.board[i][k + 1] == 0:
k += 1
if k != j:
self.board[i][k] = self.board[i][j]
self.board[i][j] = 0
self.addBlock()
self.updateBoard()
def moveUp(self):
for j in range(4):
for i in range(1, 4):
if self.board[i][j] != 0:
k = i
while k > 0 and self.board[k - 1][j] == 0:
k -= 1
if k != i:
self.board[k][j] = self.board[i][j]
self.board[i][j] = 0
self.addBlock()
self.updateBoard()
def moveDown(self):
for j in range(4):
for i in range(2, -1, -1):
if self.board[i][j] != 0:
k = i
while k < 3 and self.board[k + 1][j] == 0:
k += 1
if k != i:
self.board[k][j] = self.board[i][j]
self.board[i][j] = 0
self.addBlock()
self.updateBoard()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Left:
self.moveLeft()
elif event.key() == Qt.Key_Right:
self.moveRight()
elif event.key() == Qt.Key_Up:
self.moveUp()
elif event.key() == Qt.Key_Down:
self.moveDown()
if __name__ == '__main__':
app = QApplication(sys.argv)
game = Game()
sys.exit(app.exec_())
```
这段代码实现了一个基本的2048游戏,包括游戏界面、数字块、游戏逻辑和用户输入事件。您可以根据需要进行修改和优化。
阅读全文