用py+qt制作找茬

(1) 点击开始,左右同时出现两幅路有差别 (差别了-5处内)的图像
(2) 点击图像上存在差别的位置,会框选出矩形框.
(3) 点击完成,会提示是否通关 (总体设计3关即可)

该回答内容部分引用GPT,GPT_Pro更好的解决问题
使用 python + Qt 制作找茬游戏,需要做的事情有:

  1. 两张图片的初始化:使用 Qt 中的 QPixmap 对象来读取图片文件,然后将其设置到窗口中的两个 QLabel 上,即可完成图片的显示。

  2. 设置鼠标点击事件:使用 Qt 中的 QLabel 的 mousePressEvent() 来监听鼠标点击,并且在鼠标点击时,在QLabel上画出一个矩形框框选出差异处。

  3. 实现游戏通关功能:当玩家完成游戏后,应该提醒玩家是否已经通关。这里可以使用一个判断函数来判断是否已经找到所有的差异处。如果找到了所有的差异处,就可以提醒玩家已经通关。

# 初始化图片
self.image1 = QLabel(self)
self.image2 = QLabel(self)
pixmap1 = QPixmap("1.png") # 读取图片文件
pixmap2 = QPixmap("2.png")
self.image1.setPixmap(pixmap1) # 设置图片到label上
self.image2.setPixmap(pixmap2)

# 监听鼠标点击事件
def mousePressEvent(self, event):
    painter = QPainter(self) # 创建画家对象
    painter.setPen(QPen(Qt.black, 5, Qt.SolidLine)) # 设置画笔颜色、宽度及样式
    painter.drawRect(event.x(), event.y(), 10, 10) # 画出框选框
    
# 判断是否通关函数
def isPassed():
    pass_num = 0  # 通关数量计数器
    for i in range(5): # 检查5处差异位置是否被框选出来
        if (difference[i][0], difference[i][1]) in rect_list: # 若差异位置被框选出来,则 pass_num + 1 
            pass_num += 1 
    if pass_num == 5: # 5处差异都被正确框选出来,则提醒通关成功 
        print('Congratulations, you have passed the game!') 

如果回答有帮助,望采纳。

参考GPT和自己的思路,以下是一个用Python和PyQt实现的找茬游戏的示例代码:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton
from PyQt5.QtGui import QPixmap, QPainter, QColor, QPen
from PyQt5.QtCore import Qt, QRect, QTimer

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("找茬游戏")
        self.setGeometry(100, 100, 800, 600)

        self.left_img = "left.png"
        self.right_img = "right.png"

        self.left_label = QLabel(self)
        self.left_label.setGeometry(0, 0, 400, 600)
        self.left_label.setPixmap(QPixmap(self.left_img))

        self.right_label = QLabel(self)
        self.right_label.setGeometry(400, 0, 400, 600)
        self.right_label.setPixmap(QPixmap(self.right_img))

        self.score = 0
        self.score_label = QLabel(self)
        self.score_label.setGeometry(650, 50, 100, 50)
        self.score_label.setAlignment(Qt.AlignCenter)
        self.score_label.setText("得分:0")

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.check_diff)
        self.timer.start(1000)

        self.rect = None
        self.rect_button = QPushButton(self)
        self.rect_button.setGeometry(0, 0, 400, 600)
        self.rect_button.setFlat(True)
        self.rect_button.clicked.connect(self.check_win)

        self.win_label = QLabel(self)
        self.win_label.setGeometry(200, 200, 400, 200)
        self.win_label.setAlignment(Qt.AlignCenter)
        self.win_label.setStyleSheet("font-size: 24pt; color: red;")

        self.level = 1
        self.max_score = 10

    def check_diff(self):
        diff_img = QPixmap(self.right_img)
        painter = QPainter(diff_img)

        pen = QPen()
        pen.setWidth(5)
        pen.setColor(QColor(255, 0, 0))
        painter.setPen(pen)

        if self.level == 1:
            x1, y1, x2, y2 = 50, 250, 200, 350
        elif self.level == 2:
            x1, y1, x2, y2 = 250, 150, 350, 250
        elif self.level == 3:
            x1, y1, x2, y2 = 100, 350, 250, 450

        painter.drawRect(QRect(x1+400, y1, x2-x1, y2-y1))
        painter.end()

        self.right_label.setPixmap(diff_img)

    def check_win(self):
        if self.rect is None:
            return

        if self.level == 1:
            x1, y1, x2, y2 = 50, 250, 200, 350
        elif self.level == 2:
            x1, y1, x2, y2 = 250, 150, 350, 250
        elif self.level == 3:
            x1, y1, x2, y2 = 100, 350, 250, 450

        if self.rect.x() == x1+400 and self.rect.y() == y1 and \
           self.rect.width() == x2-x1 and self.rect.height() == y2-y1:
            self.score += 1
            self.score_label.setText(f"得分:{self.score}")
            if self.score == self.max_score:
                self.game_over("通关啦!")
            else:
                self.next_level()

在这个代码中,我加了一个max_score变量,表示每个关卡最高得分,当score达到max_score时就通关了。next_level函数用于进入下一个关卡,game_over函数用于结束游戏。你需要根据自己的游戏逻辑修改这些函数的实现。

该回答引用GPTᴼᴾᴱᴺᴬᴵ
实现找茬游戏的代码比较复杂,需要涉及到图像处理和交互设计等方面的知识。下面给您提供一些实现该游戏的思路和代码片段,供参考:
1.加载图片并显示在界面上

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtGui import QPixmap

app = QApplication(sys.argv)

# 创建主窗口
window = QWidget()
window.setWindowTitle('找茬游戏')

# 加载图片
img1_path = 'image1.jpg'
img2_path = 'image2.jpg'
img1 = QPixmap(img1_path)
img2 = QPixmap(img2_path)

# 创建标签并显示图片
label1 = QLabel()
label1.setPixmap(img1)

label2 = QLabel()
label2.setPixmap(img2)

# 布局
layout = QVBoxLayout()
layout.addWidget(label1)
layout.addWidget(label2)

window.setLayout(layout)
window.show()
sys.exit(app.exec_())

2.在图片上绘制矩形框

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QPen

class ImageLabel(QLabel):
    def __init__(self, pixmap):
        super().__init__()
        self.setPixmap(pixmap)
        self.point = None

    def mousePressEvent(self, event):
        self.point = event.pos()

    def mouseReleaseEvent(self, event):
        rect = self.getRect(self.point, event.pos())
        self.paintRect(rect)

    def getRect(self, start, end):
        x = min(start.x(), end.x())
        y = min(start.y(), end.y())
        w = abs(start.x() - end.x())
        h = abs(start.y() - end.y())
        return QRect(x, y, w, h)

    def paintRect(self, rect):
        painter = QPainter(self.pixmap())
        painter.setPen(QPen(Qt.red, 3, Qt.SolidLine))
        painter.drawRect(rect)
        self.update()

3.检测矩形框位置是否正确并给出通关提示

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("找茬游戏")
        self.setGeometry(200, 200, 800, 600)
        self.central_widget = QtWidgets.QWidget(self)
        self.setCentralWidget(self.central_widget)

        # 创建按钮
        self.finish_button = QtWidgets.QPushButton("完成", self)
        self.finish_button.setGeometry(350, 530, 100, 50)
        self.finish_button.clicked.connect(self.check_diffs)

        # 创建提示框
        self.message_box = QtWidgets.QMessageBox()

    def check_diffs(self):
        # 判断是否找完所有茬
        if len(self.diff_positions) == 0:
            self.message_box.setWindowTitle("通关啦")
            self.message_box.setText("恭喜您已通过所有关卡!")
            self.message_box.exec_()
        else:
            self.message_box.setWindowTitle("未找完")
            self.message_box.setText("您还有未找到的茬,请继续努力!")
            self.message_box.exec_()

这里我们定义了一个 MainWindow 类,其中的 check_diffs 方法用于判断是否找完了所有茬,如果找完了,则弹出“通关”提示框,否则弹出“未找完”提示框。在 MainWindow 类的初始化方法中,我们创建了一个“完成”按钮和一个 QMessageBox 对象,用于显示提示框。

首先创建一个PyQt5应用程序框架:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        # 设置窗口标题和尺寸
        self.setWindowTitle('找茬游戏')
        self.setGeometry(100, 100, 800, 600)

        # TODO: 添加游戏界面的组件

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

这里创建了一个名为MainWindow的主窗口,并使用initUI方法来初始化界面。可以在此方法中添加游戏界面的组件,如两幅路有差别的图片和“开始”按钮。接下来,需要使用PyQt5中的QPixmap和QLabel类来显示图片:

from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QLabel

class MainWindow(QMainWindow):
    def initUI(self):
        # 添加左侧图像
        self.left_image = QLabel(self)
        self.left_image.setPixmap(QPixmap('left_image.png'))
        self.left_image.move(50, 50)

        # 添加右侧图像
        self.right_image = QLabel(self)
        self.right_image.setPixmap(QPixmap('right_image.png'))
        self.right_image.move(450, 50)

在上面的代码中,我们使用QLabel类来创建标签,然后使用setPixmap方法将QPixmap对象设置为标签的图像,需要调整标签的位置和大小来适应您的游戏。接下来,您需要为图像添加单击事件,以便在单击差异时进行处理:

from PyQt5.QtCore import Qt

class MainWindow(QMainWindow):
    def initUI(self):
        # ...

        # 添加左侧图像
        self.left_image = QLabel(self)
        self.left_image.setPixmap(QPixmap('left_image.png'))
        self.left_image.move(50, 50)
        self.left_image.mousePressEvent = self.onLeftImageClicked

        # 添加右侧图像
        self.right_image = QLabel(self)
        self.right_image.setPixmap(QPixmap('right_image.png'))
        self.right_image.move(450, 50)
        self.right_image.mousePressEvent = self.onRightImageClicked

    def onLeftImageClicked(self, event):
        x = event.pos().x()
        y = event.pos().y()
        # TODO: 处理左侧图像的单击事件

    def onRightImageClicked(self, event):
        x = event.pos().x()
        y = event.pos().y()
        # TODO: 处理右侧图像的单击事件

若对你有所帮助,望采纳。

  1. 首先导入Qt库: import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * 2. 创建窗口类: class FindDifference(QWidget): def init(self): super().init() self.initUI() def initUI(self): self.setWindowTitle('Find Difference') self.setGeometry(150, 150, 500, 500) 3. 创建控件: self.startBtn = QPushButton('开始', self) self.startBtn.setGeometry(200, 400, 100, 50) self.startBtn.clicked.connect(self.startGame) self.completeBtn = QPushButton('完成', self) self.completeBtn.setGeometry(200, 450, 100, 50) self.completeBtn.clicked.connect(self.completeGame) self.diffList = [] 4. 加载图片: self.leftImageLab = QLabel(self) self.leftImageLab.setGeometry(30, 30, 200, 300) self.rightImageLab = QLabel(self) self.rightImageLab.setGeometry(260, 30, 200, 300) self.leftImageLab.setPixmap(QPixmap('test1.jpg')) self.rightImageLab.setPixmap(QPixmap('test2.jpg')) 5. 定义点击事件: def mousePressEvent(self, event): if self.startBtn.isEnabled(): return pos = event.pos() # 判断点击位置是否已经框选 if pos not in self.diffList: # 框选点击位置 self.diffList.append(pos) self.update() # 重绘窗口 def startGame(self): self.startBtn.setEnabled(False) def completeGame(self): # 判断是否全部框选成功 if len(self.diffList) == 5: QMessageBox.information(self, '提示', '恭喜你,通关成功!') else: QMessageBox.information(self, '提示', '很遗憾,通关失败!') 6. 重绘窗口: def paintEvent(self, event): qp = QPainter(self) pen = QPen(Qt.red, 3, Qt.SolidLine) qp.setPen(pen) for pos in self.diffList: qp.drawRect(pos.x()-5, pos.y()-5, 10, 10) 7. 显示窗口: if name == 'main': app = QApplication(sys.argv) window = FindDifference() window.show() sys.exit(app.exec_())