python programming作业11 Qt designer (打地鼠,不是很完美)

 

python programming作业11 Qt designer (打地鼠,不是很完美)_python

 不导包的代码

 

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class Ui_widget(object):
    def setupUi(self, widget):
        widget.setObjectName("widget")
        widget.resize(548, 461)
        
        self.pushButton_1 = QtWidgets.QPushButton(widget)
        self.pushButton_1.setGeometry(QtCore.QRect(60, 130, 111, 81))
        self.pushButton_1.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(widget)
        self.pushButton_2.setGeometry(QtCore.QRect(220, 130, 111, 81))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(widget)
        self.pushButton_3.setGeometry(QtCore.QRect(390, 130, 111, 81))
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(widget)
        self.pushButton_4.setGeometry(QtCore.QRect(60, 240, 111, 81))
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_5 = QtWidgets.QPushButton(widget)
        self.pushButton_5.setGeometry(QtCore.QRect(60, 340, 111, 81))
        self.pushButton_5.setObjectName("pushButton_5")
        self.pushButton_6 = QtWidgets.QPushButton(widget)
        self.pushButton_6.setGeometry(QtCore.QRect(220, 240, 111, 81))
        self.pushButton_6.setObjectName("pushButton_6")
        self.pushButton_7 = QtWidgets.QPushButton(widget)
        self.pushButton_7.setGeometry(QtCore.QRect(390, 240, 111, 81))
        self.pushButton_7.setObjectName("pushButton_7")
        self.pushButton_8 = QtWidgets.QPushButton(widget)
        self.pushButton_8.setGeometry(QtCore.QRect(220, 340, 111, 81))
        self.pushButton_8.setObjectName("pushButton_8")
        self.pushButton_9 = QtWidgets.QPushButton(widget)
        self.pushButton_9.setGeometry(QtCore.QRect(390, 340, 111, 81))
        self.pushButton_9.setObjectName("pushButton_9")
        self.pushButton_10 = QtWidgets.QPushButton(widget)
        self.pushButton_10.setGeometry(QtCore.QRect(30, 20, 91, 41))
        self.pushButton_10.setObjectName("pushButton_10")
        self.pushButton_11 = QtWidgets.QPushButton(widget)
        self.pushButton_11.setGeometry(QtCore.QRect(30, 80, 91, 41))
        self.pushButton_11.setObjectName("pushButton_11")
        self.label = QtWidgets.QLabel(widget)
        self.label.setGeometry(QtCore.QRect(220, 30, 151, 51))
        self.label.setObjectName("label")

        self.retranslateUi(widget)
        QtCore.QMetaObject.connectSlotsByName(widget)
        widget.setTabOrder(self.pushButton_1, self.pushButton_2)
        widget.setTabOrder(self.pushButton_2, self.pushButton_4)
        widget.setTabOrder(self.pushButton_4, self.pushButton_3)
        widget.setTabOrder(self.pushButton_3, self.pushButton_5)
        widget.setTabOrder(self.pushButton_5, self.pushButton_6)
        widget.setTabOrder(self.pushButton_6, self.pushButton_7)
        widget.setTabOrder(self.pushButton_7, self.pushButton_8)
        widget.setTabOrder(self.pushButton_8, self.pushButton_9)
        widget.setTabOrder(self.pushButton_9, self.pushButton_10)
        widget.setTabOrder(self.pushButton_10, self.pushButton_11)

    def retranslateUi(self, widget):
        _translate = QtCore.QCoreApplication.translate
        widget.setWindowTitle(_translate("widget", "帅帅的打地鼠游戏"))
        #self.pushButton.setText(_translate("widget", "PushButton"))
        self.pushButton_1.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_2.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_3.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_4.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_5.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_6.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_7.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_8.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_9.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_10.setText(_translate("widget", "开始游戏"))
        self.pushButton_11.setText(_translate("widget", "停止"))
        self.label.setText(_translate("widget", "TextLabel"))


from PyQt5.QtCore import QTimer
import time
import numpy as np

class MyWindow(QDialog, Ui_widget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self. setupUi(self)
        self.begin = False
        self.end = True
        self.score = 0

        #标记这9格子有没有出现小老鼠
        self.flag = np.zeros(10)

        self.timer = QTimer(self) #初始化一个定时器
        self.timer.timeout.connect(self.operate) #计时结束调用operate()方法

        self.push_dic={}
        self.push_dic[1]=self.pushButton_1
        self.push_dic[2]=self.pushButton_2
        self.push_dic[3]=self.pushButton_3
        self.push_dic[4]=self.pushButton_4
        self.push_dic[5]=self.pushButton_5
        self.push_dic[6]=self.pushButton_6
        self.push_dic[7]=self.pushButton_7
        self.push_dic[8]=self.pushButton_8
        self.push_dic[9]=self.pushButton_9
        
        self.pushButton_10.clicked.connect(self.game)
        self.pushButton_11.clicked.connect(self.gameover)
        self.label.setText(str(self.score))

        self.push_dic[1].clicked.connect(lambda:self.getscore(1))
        self.push_dic[2].clicked.connect(lambda:self.getscore(2))
        self.push_dic[3].clicked.connect(lambda:self.getscore(3))
        self.push_dic[4].clicked.connect(lambda:self.getscore(4))
        self.push_dic[5].clicked.connect(lambda:self.getscore(5))
        self.push_dic[6].clicked.connect(lambda:self.getscore(6))
        self.push_dic[7].clicked.connect(lambda:self.getscore(7))
        self.push_dic[8].clicked.connect(lambda:self.getscore(8))
        self.push_dic[9].clicked.connect(lambda:self.getscore(9))

        self.label.setStyleSheet("font:20pt '楷体';border-width: 1px;border-style: solid;border-color: rgb(255, 0, 0);")


    # 每过一秒发生的事情   
    def operate(self):
        #具体操作
        #print("计时结束")

        #地鼠全部藏起来
        for i in range(1,10):
            self.push_dic[i].setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.flag = np.zeros(10)
        

        a=np.random.randint(1,10)
        self.push_dic[a].setStyleSheet('QPushButton{background-image:url(mouse.png)}')
        self.flag[a]=1
        a=np.random.randint(1,10)
        self.push_dic[a].setStyleSheet('QPushButton{background-image:url(mouse.png)}')
        self.flag[a]=1
        a=np.random.randint(1,10)
        self.push_dic[a].setStyleSheet('QPushButton{background-image:url(mouse.png)}')
        self.flag[a]=1
        
        
    def game(self):
        if self.begin == False:
            self.end = False
            self.begin = True
            print("启动计时器")
            self.timer.start(800) #设置计时间隔并启动
            self.pushButton_10.setText("暂停")

        elif self.begin == True:
            self.begin = False
            self.timer.stop()
            self.pushButton_10.setText("继续")
            
            
    def gameover(self):
        if self.end == False:
            self.begin = False
            self.end == True
            self.pushButton_10.setText("开始游戏")
            self.flag = np.zeros(10)
            self.timer.stop()
            #地鼠全部藏起来
            for i in range(1,10):
                self.push_dic[i].setStyleSheet('QPushButton{background-image:url(tu.png)}')
            self.score = 0
            self.label.setText(str(self.score))


    def getscore(self,a):
        if self.flag[a] == 1 and self.begin == True:
            self.score += 1
            self.label.setText(str(self.score))
            self.flag[a] = 0
            self.push_dic[a].setStyleSheet('QPushButton{background-image:url(catch.png)}')
        
        

app = QApplication(sys.argv)
form = MyWindow()
form.show()
app.exec_()

 

导包的代码,先用Qt布局再

 

python programming作业11 Qt designer (打地鼠,不是很完美)_初始化_02

qt_designer.ui转为qt_designer.py以后

 

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '11.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_widget(object):
    def setupUi(self, widget):
        widget.setObjectName("widget")
        widget.resize(548, 461)
        
        self.pushButton_1 = QtWidgets.QPushButton(widget)
        self.pushButton_1.setGeometry(QtCore.QRect(60, 130, 111, 81))
        self.pushButton_1.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(widget)
        self.pushButton_2.setGeometry(QtCore.QRect(220, 130, 111, 81))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(widget)
        self.pushButton_3.setGeometry(QtCore.QRect(390, 130, 111, 81))
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(widget)
        self.pushButton_4.setGeometry(QtCore.QRect(60, 240, 111, 81))
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_5 = QtWidgets.QPushButton(widget)
        self.pushButton_5.setGeometry(QtCore.QRect(60, 340, 111, 81))
        self.pushButton_5.setObjectName("pushButton_5")
        self.pushButton_6 = QtWidgets.QPushButton(widget)
        self.pushButton_6.setGeometry(QtCore.QRect(220, 240, 111, 81))
        self.pushButton_6.setObjectName("pushButton_6")
        self.pushButton_7 = QtWidgets.QPushButton(widget)
        self.pushButton_7.setGeometry(QtCore.QRect(390, 240, 111, 81))
        self.pushButton_7.setObjectName("pushButton_7")
        self.pushButton_8 = QtWidgets.QPushButton(widget)
        self.pushButton_8.setGeometry(QtCore.QRect(220, 340, 111, 81))
        self.pushButton_8.setObjectName("pushButton_8")
        self.pushButton_9 = QtWidgets.QPushButton(widget)
        self.pushButton_9.setGeometry(QtCore.QRect(390, 340, 111, 81))
        self.pushButton_9.setObjectName("pushButton_9")
        self.pushButton_10 = QtWidgets.QPushButton(widget)
        self.pushButton_10.setGeometry(QtCore.QRect(30, 20, 91, 41))
        self.pushButton_10.setObjectName("pushButton_10")
        self.pushButton_11 = QtWidgets.QPushButton(widget)
        self.pushButton_11.setGeometry(QtCore.QRect(30, 80, 91, 41))
        self.pushButton_11.setObjectName("pushButton_11")
        self.label = QtWidgets.QLabel(widget)
        self.label.setGeometry(QtCore.QRect(220, 30, 151, 51))
        self.label.setObjectName("label")

        self.retranslateUi(widget)
        QtCore.QMetaObject.connectSlotsByName(widget)
        widget.setTabOrder(self.pushButton_1, self.pushButton_2)
        widget.setTabOrder(self.pushButton_2, self.pushButton_4)
        widget.setTabOrder(self.pushButton_4, self.pushButton_3)
        widget.setTabOrder(self.pushButton_3, self.pushButton_5)
        widget.setTabOrder(self.pushButton_5, self.pushButton_6)
        widget.setTabOrder(self.pushButton_6, self.pushButton_7)
        widget.setTabOrder(self.pushButton_7, self.pushButton_8)
        widget.setTabOrder(self.pushButton_8, self.pushButton_9)
        widget.setTabOrder(self.pushButton_9, self.pushButton_10)
        widget.setTabOrder(self.pushButton_10, self.pushButton_11)

    def retranslateUi(self, widget):
        _translate = QtCore.QCoreApplication.translate
        widget.setWindowTitle(_translate("widget", "帅帅的打地鼠游戏"))
        #self.pushButton.setText(_translate("widget", "PushButton"))
        self.pushButton_1.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_2.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_3.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_4.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_5.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_6.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_7.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_8.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_9.setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.pushButton_10.setText(_translate("widget", "开始游戏"))
        self.pushButton_11.setText(_translate("widget", "停止"))
        self.label.setText(_translate("widget", "TextLabel"))

再导入

 

from qt_desiginer import Ui_widget
from PyQt5.QtCore import QTimer
import time
import numpy as np
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys

class MyWindow(QDialog, Ui_widget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self. setupUi(self)
        self.begin = False
        self.end = True
        self.score = 0

        #标记这9格子有没有出现小老鼠
        self.flag = np.zeros(10)

        self.timer = QTimer(self) #初始化一个定时器
        self.timer.timeout.connect(self.operate) #计时结束调用operate()方法

        self.push_dic={}
        self.push_dic[1]=self.pushButton_1
        self.push_dic[2]=self.pushButton_2
        self.push_dic[3]=self.pushButton_3
        self.push_dic[4]=self.pushButton_4
        self.push_dic[5]=self.pushButton_5
        self.push_dic[6]=self.pushButton_6
        self.push_dic[7]=self.pushButton_7
        self.push_dic[8]=self.pushButton_8
        self.push_dic[9]=self.pushButton_9
        
        self.pushButton_10.clicked.connect(self.game)
        self.pushButton_11.clicked.connect(self.gameover)
        self.label.setText(str(self.score))

        self.push_dic[1].clicked.connect(lambda:self.getscore(1))
        self.push_dic[2].clicked.connect(lambda:self.getscore(2))
        self.push_dic[3].clicked.connect(lambda:self.getscore(3))
        self.push_dic[4].clicked.connect(lambda:self.getscore(4))
        self.push_dic[5].clicked.connect(lambda:self.getscore(5))
        self.push_dic[6].clicked.connect(lambda:self.getscore(6))
        self.push_dic[7].clicked.connect(lambda:self.getscore(7))
        self.push_dic[8].clicked.connect(lambda:self.getscore(8))
        self.push_dic[9].clicked.connect(lambda:self.getscore(9))

        self.label.setStyleSheet("font:20pt '楷体';border-width: 1px;border-style: solid;border-color: rgb(255, 0, 0);")


    # 每过一秒发生的事情   
    def operate(self):
        #具体操作
        #print("计时结束")

        #地鼠全部藏起来
        for i in range(1,10):
            self.push_dic[i].setStyleSheet('QPushButton{background-image:url(tu.png)}')
        self.flag = np.zeros(10)
        

        a=np.random.randint(1,10)
        self.push_dic[a].setStyleSheet('QPushButton{background-image:url(mouse.png)}')
        self.flag[a]=1
        a=np.random.randint(1,10)
        self.push_dic[a].setStyleSheet('QPushButton{background-image:url(mouse.png)}')
        self.flag[a]=1
        a=np.random.randint(1,10)
        self.push_dic[a].setStyleSheet('QPushButton{background-image:url(mouse.png)}')
        self.flag[a]=1
        
        
    def game(self):
        if self.begin == False:
            self.end = False
            self.begin = True
            print("启动计时器")
            self.timer.start(800) #设置计时间隔并启动
            self.pushButton_10.setText("暂停")

        elif self.begin == True:
            self.begin = False
            self.timer.stop()
            self.pushButton_10.setText("继续")
            
            
    def gameover(self):
        if self.end == False:
            self.begin = False
            self.end == True
            self.pushButton_10.setText("开始游戏")
            self.flag = np.zeros(10)
            self.timer.stop()
            #地鼠全部藏起来
            for i in range(1,10):
                self.push_dic[i].setStyleSheet('QPushButton{background-image:url(tu.png)}')
            self.score = 0
            self.label.setText(str(self.score))


    def getscore(self,a):
        if self.flag[a] == 1 and self.begin == True:
            self.score += 1
            self.label.setText(str(self.score))
            self.flag[a] = 0
            self.push_dic[a].setStyleSheet('QPushButton{background-image:url(catch.png)}')
        
        

app = QApplication(sys.argv)
form = MyWindow()
form.show()
app.exec_()

 

python programming作业11 Qt designer (打地鼠,不是很完美)_python_03