其他分享
首页 > 其他分享> > 数字华容道

数字华容道

作者:互联网

所需的库(若有未安装的,程序无法运行):

1.sys

2.random

3.time

4.enum

5.PyQt5

import sys
import random
from time import *
from enum import IntEnum
from PyQt5.QtWidgets import QLabel, QWidget, QApplication, QGridLayout, QMessageBox
from PyQt5.QtGui import QFont, QPalette
from PyQt5.QtCore import Qt
#*_lm1399-sqw-0956.windows.mender-r_*#
print("-不喜勿喷-")
sleep(1)
print("-窗口在下边-")
sleep(1)
print("...上下左右键移动...")

class Direction(IntEnum):
    UP = 0
    DOWN = 1
    LEFT = 2
    RIGHT = 3
class NumberHuaRong(QWidget):
    def __init__(self):
        super().__init__()
        self.blocks = []
        self.zero_row = 0
        self.zero_column = 0
        self.gltMain = QGridLayout()
        self.initUI()
    def initUI(self):      
        self.gltMain.setSpacing(0)
        self.onInit()
        self.setLayout(self.gltMain)
        self.setFixedSize(550, 550)
        self.setWindowTitle('数字华容道2')
        self.setStyleSheet("background-color:purple;")
        self.show()
    def onInit(self):
        self.numbers = list(range(1, 16))
        self.numbers.append(0)
        for row in range(4):
            self.blocks.append([])
            for column in range(4):
                temp = self.numbers[row * 4 + column]
                if temp == 0:
                    self.zero_row = row
                    self.zero_column = column
                self.blocks[row].append(temp)
        for i in range(1000):
            random_num = random.randint(0, 3)
            self.move(Direction(random_num))
        self.updatePanel()
    def keyPressEvent(self, event):
        key = event.key()
        if(key == Qt.Key_Up or key == Qt.Key_W):
            self.move(Direction.UP)
        if(key == Qt.Key_Down or key == Qt.Key_S):
            self.move(Direction.DOWN)
        if(key == Qt.Key_Left or key == Qt.Key_A):
            self.move(Direction.LEFT)
        if(key == Qt.Key_Right or key == Qt.Key_D):
            self.move(Direction.RIGHT)
        self.updatePanel()
        if self.checkResult():
            if QMessageBox.Ok == QMessageBox.information(self, '挑战结果', '恭喜您完成挑战!'):
                self.onInit()
    def move(self, direction):
        if(direction == Direction.UP):
            if self.zero_row != 3:
                self.blocks[self.zero_row][self.zero_column] = self.blocks[self.zero_row + 1][self.zero_column]
                self.blocks[self.zero_row + 1][self.zero_column] = 0
                self.zero_row += 1
        if(direction == Direction.DOWN):
            if self.zero_row != 0:
                self.blocks[self.zero_row][self.zero_column] = self.blocks[self.zero_row - 1][self.zero_column]
                self.blocks[self.zero_row - 1][self.zero_column] = 0
                self.zero_row -= 1
        if(direction == Direction.LEFT):
            if self.zero_column != 3:
                self.blocks[self.zero_row][self.zero_column] = self.blocks[self.zero_row][self.zero_column + 1]
                self.blocks[self.zero_row][self.zero_column + 1] = 0
                self.zero_column += 1
        if(direction == Direction.RIGHT):
            if self.zero_column != 0:
                self.blocks[self.zero_row][self.zero_column] = self.blocks[self.zero_row][self.zero_column - 1]
                self.blocks[self.zero_row][self.zero_column - 1] = 0
                self.zero_column -= 1
    def updatePanel(self):
        for row in range(4):
            for column in range(4):
                self.gltMain.addWidget(Block(self.blocks[row][column]), row, column)
        self.setLayout(self.gltMain)
    def checkResult(self):
        if self.blocks[3][3] != 0:
            return False
        for row in range(4):
            for column in range(4):
                if row == 3 and column == 3:
                    pass
                elif self.blocks[row][column] != row * 5 + column + 1:
                    return False
        return True
class Block(QLabel):
    def __init__(self, number):
        super().__init__()
        self.number = number
        self.setFixedSize(100, 100)
        font = QFont()
        font.setPointSize(50)
        font.setBold(True)
        self.setFont(font)
        pa = QPalette()
        pa.setColor(QPalette.WindowText, Qt.green)
        self.setPalette(pa)
        self.setAlignment(Qt.AlignCenter)
        if self.number == 0:
            self.setStyleSheet("background-color:yellow;border-radius: 20%;")
        else:
            self.setStyleSheet("background-color:red;border-radius: 20%;")
            self.setText(str(self.number))
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = NumberHuaRong()
    sys.exit(app.exec_())
    PyQt5.lc

标签:blocks,数字,column,华容道,self,zero,Qt,row
来源: https://blog.csdn.net/jxzz_55/article/details/121043868