主要知识点:QWidget,QPushButton,QTimer 计时器
说明:学 Qt 时的第一个小练习,只做了最基础简单的功能。
完整代码:
import sys
import os
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import random
class myMainWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("打地鼠小游戏")
self.mousenum = 0#记录游戏开始后出现的地鼠只数
global score
score = 0
self.timer = QTimer()#全局计时器
self.timer.timeout.connect(self.gameend) #结束时调用
self.mousetimer = QTimer()#地鼠出现时间计时器
self.mousetimer.timeout.connect(self.mouseactive)
self.remainingTimetimer = QTimer()#每秒刷新剩余时间
self.remainingTimetimer.timeout.connect(self.remainingTimeShow)
self.mouseSign = None
#图像
self.imagelayout = QGridLayout()
x = 0
for i in range(25):
exec ("self.mouse%s=mouse()"%i)
for i in range(5):
for j in range(5):
exec ("self.imagelayout.addWidget(self.mouse%s,%s,%s)"%(x,i,j))
x+=1
self.setLayout(self.imagelayout)
self.imagelayout.addWidget(self.mouse10,2,0)
#右侧固定窗口内控件
self.settingslayout = QGridLayout()
self.settingsWidget = QWidget()
self.settingsWidget.setFixedSize(80,350)
self.imagelayout.addWidget(self.settingsWidget,0,5,1,5)
self.settingsWidget.setLayout(self.settingslayout)
self.startPushButton = QPushButton("start")
self.startPushButton.setFixedSize(50,30)
self.textBrowser = QTextBrowser()
self.textBrowser.setText("游戏未启动")
self.textBrowser.setFixedSize(70,30)
self.scoreTextBrowser = QTextBrowser()
self.scoreTextBrowser.setText("击中数:0")
self.scoreTextBrowser.setFixedSize(70,30)
self.runTextBrowser = QTextBrowser()
self.runTextBrowser.setText("逃离数:0")
self.runTextBrowser.setFixedSize(70,30)
self.remainingTimeTextBrowser = QTextBrowser()
self.remainingTimeTextBrowser.setText("剩余时间:\n30秒")
self.remainingTimeTextBrowser.setFixedSize(70,40)
self.startPushButton.clicked.connect(self.gamestart)
self.settingslayout.addWidget(self.startPushButton,0,0)
self.settingslayout.addWidget(self.textBrowser,1,0)
self.settingslayout.addWidget(self.runTextBrowser,2,0)
self.settingslayout.addWidget(self.scoreTextBrowser,3,0)
self.settingslayout.addWidget(self.remainingTimeTextBrowser,4,0)
def gamestart(self):
score = 0
self.textBrowser.setText("游戏中")
self.timer.start(30000)#单位毫秒 1000为1秒
self.mousetimer.start(1250)
self.remainingTimetimer.start(1000)
def mouseactive(self):
if self.mouseSign != None:
exec ("self.mouse%s.flag = 0"%self.mouseSign)
exec ("self.mouse%s.setStyleSheet('QPushButton{border-image: url(E:/打地鼠游戏/0.jpg)}')"%self.mouseSign)
self.mouseSign = int(random.random()*25)#随机
exec ("self.mouse%s.flag = 1"%self.mouseSign)
exec ("self.mouse%s.setStyleSheet('QPushButton{border-image: url(E:/打地鼠游戏/3.jpg)}')"%self.mouseSign)#地鼠冒出
self.mousetimer.start(1250)#地鼠出现时间
self.mousenum += 1
self.scoreTextBrowser.setText("击中数:"+str(score))
self.runTextBrowser.setText("逃离数:"+str(self.mousenum-score-1))
def gameend(self):
self.timer.stop()
self.mousetimer.stop()
self.textBrowser.setText("游戏结束")
self.mousenum = 0
for i in range(25):
exec ("self.mouse%s.flag = 0"%i)#地鼠flag还原
def remainingTimeShow(self):
remaintime = int(round(self.timer.remainingTime()/1000,0))
self.remainingTimeTextBrowser.setText("剩余时间:\n"+str(remaintime)+"秒")
self.remainingTimetimer.start(1000)
class mouse(QPushButton):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(80,70)#self.resize(40, 30)
self.setStyleSheet("QPushButton{border-image: url(E:/打地鼠游戏/0.jpg)}")#地洞
self.flag = 0
self.upTime = QTimer()#恢复锤子状态
self.upTime.timeout.connect(self.mouseUp)
def hit(self):
if self.flag:
self.setStyleSheet("QPushButton{border-image: url(E:/打地鼠游戏/xing.jpg)}")#地鼠被砸
global score
score += 1
self.flag = 0
def mousePressEvent(self,event):
self.setCursor(QCursor(QPixmap(r"E:\打地鼠游戏\down.png")))#鼠标形状变成落下的锤子
self.upTime.start(100)
self.hit()
def mouseUp(self):#抬起锤子
self.setCursor(QCursor(QPixmap(r"E:\打地鼠游戏\up.png")))
app = QApplication(sys.argv)
form = myMainWindow()
form.setFixedSize(480, 350)
form.show()
app.exec_()