PyQt5 – 乱码字游戏

PyQt5 – 乱码字游戏

在这篇文章中,我们将看到如何使用PyQt5创建一个乱码字游戏。 乱码字游戏:乱码 字是给玩家的,玩家必须重新排列该字的字符以组成一个正确的有意义的字。以下是游戏的外观

PyQt5 - 乱码字游戏

GUI实施步骤:

1.创建一个标题标签,显示游戏名称

2.创建标签,显示拼写的单词

3.一个获取文本的行编辑部件

4.一个检查输入文本的按钮,就在行编辑的旁边

5.创建一个结果标签,告知答案是否正确并改变其颜色

6.两个按钮用于开始和重置游戏

后端实施步骤:

1.创建一个单词列表

2.为检查按钮添加动作

3.在检查按钮的动作中获取输入的文本并与输入的单词进行比较

4.如果单词匹配,显示结果为正确,并将颜色设为绿色,否则将颜色设为红色,并说错误

5.为启动按钮添加动作

6.在开始按钮的动作中,使用随机函数从列表中获取当前单词

7.从当前单词创建一个拼写单词,并将其设置为拼写标签

8.删除结果标签的文字,并将颜色设为黄色

9.为复位按钮添加动作

10.在重置按钮的动作中,将当前单词设置为空白

11.删除所有标签上的文字,并将其颜色设为原来的颜色。

以下是执行情况

# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import random
import sys
 
 
class Window(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        # setting title
        self.setWindowTitle("Python ")
 
        # setting geometry
        self.setGeometry(100, 100, 320, 350)
 
        # calling method
        self.UiComponents()
 
        # showing all the widgets
        self.show()
 
        # words
        self.words = ['red', 'cold', 'hot', 'geeks', 'rain',
                           'black', 'snow', 'hills', 'code']
 
        # current word
        self.current_text = ""
 
    # method for components
    def UiComponents(self):
 
        # creating head label
        head = QLabel("Jumbled Word Game", self)
 
        # setting geometry to the head
        head.setGeometry(20, 10, 280, 60)
 
        # font
        font = QFont('Times', 15)
        font.setBold(True)
        font.setItalic(True)
        font.setUnderline(True)
 
        # setting font to the head
        head.setFont(font)
 
        # setting alignment of the head
        head.setAlignment(Qt.AlignCenter)
 
        # setting color effect to the head
        color = QGraphicsColorizeEffect(self)
        color.setColor(Qt.darkCyan)
        head.setGraphicsEffect(color)
 
        # creating label to show the jumbled word
        self.j_word = QLabel(self)
 
        # setting geometry
        self.j_word.setGeometry(30, 80, 260, 50)
 
        # setting style sheet
        self.j_word.setStyleSheet("border : 2px solid black; background : white;")
 
        # setting font
        self.j_word.setFont(QFont('Times', 12))
 
        # setting alignment
        self.j_word.setAlignment(Qt.AlignCenter)
 
 
        # creating a line edit widget to het the text
        self.input = QLineEdit(self)
 
        # setting geometry
        self.input.setGeometry(20, 150, 200, 40)
 
        # setting alignment
        self.input.setAlignment(Qt.AlignCenter)
 
        # creating push button to check the input
        self.check = QPushButton("Check", self)
 
        # setting geometry
        self.check.setGeometry(230, 155, 80, 30)
 
        # adding action to the check button
        self.check.clicked.connect(self.check_action)
 
        # result label
        self.result = QLabel(self)
 
        # setting geometry
        self.result.setGeometry(40, 210, 240, 50)
 
        # setting font
        self.result.setFont(QFont('Times', 13))
 
        # setting alignment
        self.result.setAlignment(Qt.AlignCenter)
 
        # setting style sheet
        self.result.setStyleSheet("border : 2px solid black; background : yellow;")
 
        # creating push buttons to start and reset the game
        start = QPushButton("Start", self)
        reset = QPushButton("Reset", self)
 
        # setting geometry to both the button
        start.setGeometry(15, 290, 140, 40)
        reset.setGeometry(165, 290, 140, 40)
 
        # adding action to both the buttons
        start.clicked.connect(self.start_action)
        reset.clicked.connect(self.reset_action)
 
    def check_action(self):
 
        # getting text from the line edit
        text = self.input.text()
 
        # checking if text is similar to the current text
        if text == self.current_text:
            self.result.setText("Correct Answer")
 
            # making result color green
            self.result.setStyleSheet("background : lightgreen;")
 
        else:
            self.result.setText("Wrong Answer")
 
            # making result color red
            self.result.setStyleSheet("background : red;")
 
 
 
    def start_action(self):
 
        # selecting one word
        self.current_text = random.choice(self.words)
 
        # sample() method shuffling the characters of the word
        random_word = random.sample(self.current_text, len(self.current_text))
 
        # join() method join the elements
        # of the iterator(e.g. list) with particular character .
        jumbled = ''.join(random_word)
 
        # setting text to the jumbled word
        self.j_word.setText(jumbled)
 
        # setting result text to blank
        self.result.setText("")
 
        # making result label color yellow
        self.result.setStyleSheet("border : 2px solid black; background : yellow;")
 
        # setting text of input to blank
        self.input.setText("")
 
    def reset_action(self):
 
        # setting current text blank
        self.current_text = ""
 
        # setting text of input to blank
        self.input.setText("")
 
        # clear the text of all the labels
        self.j_word.setText("")
        self.result.setText("")
 
        # making result label color yellow
        self.result.setStyleSheet("border : 2px solid black; background : yellow;")
 
 
 
# create pyqt5 app
App = QApplication(sys.argv)
 
# create the instance of our Window
window = Window()
 
# start the app
sys.exit(App.exec())

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程