Finish the game with py+qt

youthsong 注册会员
2023-02-27 22:24

Here is sample code for a simple game of finding fault based on the PyQt implementation:

from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton
from PyQt5.QtGui import QPixmap, QPainter, QPen, QColor
from PyQt5.QtCore import Qt, QPoint
import random

class FindDiffGame(QMainWindow):
    def __init__(self):

        self.setWindowTitle("Find Diff Game")
        self.setGeometry(100, 100, 800, 600)

        self.central_widget = QLabel(self)
        self.central_widget.setGeometry(0, 0, 800, 600)

        self.start_button = QPushButton('Start', self)
        self.start_button.setGeometry(350, 500, 100, 50)

        self.quit_button = QPushButton('Quit', self)
        self.quit_button.setGeometry(450, 500, 100, 50)

        self.diff_areas = []
        self.diff_count = 5
        self.current_diff_index = 0

        self.image1 = QPixmap("image1.png")
        self.image2 = QPixmap("image2.png")
        self.image_width = self.image1.width()
        self.image_height = self.image1.height()

        self.painter = QPainter()
        self.diff_painter = QPainter()

    def start_game(self):
        self.current_diff_index = 0



        self.central_widget.mousePressEvent = self.check_diff

    def generate_diff_areas(self):
        self.diff_areas = []
        for i in range(self.diff_count):
            x = random.randint(0, self.image_width - 50)
            y = random.randint(0, self.image_height - 50)
            self.diff_areas.append((x, y))

    def check_diff(self, event):
        x = event.pos().x()
        y = event.pos().y()
        for diff_area in self.diff_areas:
            if abs(x - diff_area[0]) < 50 and abs(y - diff_area[1]) < 50:
                self.current_diff_index += 1
                if self.current_diff_index == self.diff_count:
        self.draw_wrong_area(x, y)

    def draw_diff_area(self, diff_area):
        pen = QPen(QColor(255, 0, 0), 3)
        self.diff_painter.drawRect(diff_area[0], diff_area[1], 50, 50)

    def draw_wrong_area(self, x, y):
        pen = QPen(QColor(0, 0, 255), 3)
        self.painter.drawPoint(QPoint(x, y))

    def win_game(self):

if __name__ == '__main__':
    app = QApplication([])
    game = FindDiffGame()

If it helps you, please accept it.

cyf198711091 注册会员
2023-02-27 22:24

UI or your own design, here at most to make a game package, after all, 15 yuan is not worth so much operation

anhui_my 注册会员
2023-02-27 22:24

Part of the answer references GPT, GPT_Pro better problem solving
Using Python+Qt to complete the game, mainly using Python language to write the game, And Qt as a graphical interface design, the realization of the game interface and functions.

First, we need to use Qt Designer to design the interface, considering the game to achieve the function of finding faults, we need to add two Label controls in the interface, one is to place the left picture of the label, the other is to place the right picture of the label; Then add a Button control. The function of this control is to click it to start the game. Finally, add a Button control, the Button function is to click it to complete the game.

Second, use the pyqt5 library to write Python code. First, you need to import the QApplication and QWidget functions. Then define an app variable to hold the instance created by QApplication; Create another interface variable to place the UI design window. We then define a function start_game() to start the game, which displays two different images randomly on the left and right labels. Finally, a function finish_game() is defined to complete the game. The function of this function is to detect whether there is a difference between the pictures on the left and right label. If there is a difference in location, a rectangular box is displayed at that location to indicate it. If there is no difference, a dialog box will pop up to tell you whether to clear the level.

Finally, pack the code, compress it, and send it.
If the answer is helpful, please accept it.

About the Author

Question Info

Publish Time
2023-02-27 22:24
Update Time
2023-02-27 22:24