PyQt5图形界面小游戏--剪刀石头布

python 复制代码
import random
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QCheckBox, QFrame
from PyQt5.QtGui import QIcon, QFont


class Game(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('剪刀石头布')
        self.setWindowIcon(QIcon('./bitbug_favicon.ico'))  # 窗口图标
        self.resize(600, 300)  # 窗口大小
        self.move(600, 300)
        self.label = QLabel(self)  # 标签
        self.label1 = QLabel(self)
        self.label1.setText('电脑出招!')  # 标签名称
        self.label1.setFont(QFont('microsoft Yahei', 20))  # 标签字体
        self.label1.move(50, 250)
        # 空格用来占位, 代表后续只能输入这些数量的单词
        self.label.setText('play!                  ')
        self.label.setFont(QFont('microsoft Yahei', 20))
        self.label.move(260, 150)

        btn1 = QPushButton('剪刀', self)  # 按钮
        btn1.setIcon(QIcon('./game_1/jiandao.png'))  # 按钮图标
        # btn1.setStyleSheet('QPushButton{background-color:blue}')  # 按钮颜色
        btn1.move(50, 50)
        btn1.resize(100, 60)
        btn2 = QPushButton('石头', self)
        btn2.setIcon(QIcon('./game_1/shitou.png'))
        btn2.move(250, 50)
        btn2.resize(100, 60)
        btn3 = QPushButton('布', self)
        btn3.setIcon(QIcon('./game_1/bu.png'))
        btn3.move(450, 50)
        btn3.resize(100, 60)
        
        # 按钮连接信号函数buttonClicked
        btn1.clicked.connect(self.buttonClicked)
        btn2.clicked.connect(self.buttonClicked)
        btn3.clicked.connect(self.buttonClicked)
        self.show()

    def buttonClicked(self):
        ls = ['剪刀', '石头', '布']
        a = random.choice(ls)  # 随即出招
        self.label1.setText(f'{a}')
        # 判断输赢条件
        if a == self.sender().text():
            self.label.setText('平局')
            self.label.move(260, 150)
        elif self.sender().text() == '布' and a == '石头':
            self.label.move(200, 150)
            self.label.setText('恭喜你, 你赢啦')
        elif self.sender().text() == '布' and a == '剪刀':
            self.label.move(200, 150)
            self.label.setText('很遗憾, 你输了')
        elif self.sender().text() == '剪刀' and a == '布':
            self.label.move(200, 150)
            self.label.setText('恭喜你, 你赢啦')
        elif self.sender().text() == '剪刀' and a == '石头':
            self.label.move(200, 150)
            self.label.setText('很遗憾, 你输了')
        elif self.sender().text() == '石头' and a == '剪刀':
            self.label.move(200, 150)
            self.label.setText('恭喜你, 你赢啦')
        else:
            self.label.move(200, 150)
            self.label.setText('很遗憾, 你输了')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Game()
    sys.exit(app.exec_())
相关推荐
一世琉璃白_Y3 分钟前
pg配置国内数据源安装
linux·python·postgresql·centos
liwulin05069 分钟前
【PYTHON】COCO数据集中的物品ID
开发语言·python
小鸡吃米…9 分钟前
Python - XML 处理
xml·开发语言·python·开源
我赵帅的飞起16 分钟前
python国密SM4加解密
python·sm4加解密·国密sm4加解密
yaoh.wang35 分钟前
力扣(LeetCode) 1: 两数之和 - 解法思路
python·程序人生·算法·leetcode·面试·跳槽·哈希算法
APIshop42 分钟前
Java爬虫1688详情api接口实战解析
java·开发语言·爬虫
Mr.Jessy1 小时前
JavaScript高级:深浅拷贝、异常处理、防抖及节流
开发语言·前端·javascript·学习
bing.shao1 小时前
Golang 高并发秒杀系统踩坑
开发语言·后端·golang
liwulin05061 小时前
【PYTHON-YOLOV8N】关于YOLO的推理训练图片的尺寸
开发语言·python·yolo
我送炭你添花2 小时前
Pelco KBD300A 模拟器:04+1.Python 打包详解:历史、发展与多种方式对比
python·测试工具·运维开发