# pyinstaller -D -w ./zhuakunkun.py 打包命令
import random
import sys
from PyQt5.QtCore import QBasicTimer, Qt, QTimer ,QUrl
from PyQt5.QtGui import QColor, QImage, QPixmap, QFont, QIcon, QCursor
from PyQt5.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, QMainWindow, QWidget, QPushButton
from PyQt5.QtMultimedia import QMediaPlayer ,QMediaContent
class WhackAMole(QMainWindow):
def __init__(self):
super().__init__()
self.i = 0
# 设置窗口属性
self.setWindowTitle("击毙坤坤")
self.setGeometry(300, 300, 650, 650)
self.setWindowIcon(QIcon('enemy.png'))
# 创建一个图形场景
self.scene = QGraphicsScene()
self.background=self.scene.addPixmap(QPixmap("background.png").scaled(645,645))
self.background.setZValue(0)
# self.background.setZValue(1)
self.view = QGraphicsView(self.scene)
# 创建一个计时器
self.timer = QBasicTimer()
# self.interval = 1000 # 每一秒发生一次事件
self.counter = 0 # 计数器
# 创建开始按钮
self.btn = QPushButton("开始游戏", self.view)
self.btn.setGeometry(280, 500, 100, 30)
self.btn.clicked.connect(self.choosebtn)
# 创建一个地鼠
self.mole = self.scene.addPixmap(QPixmap("enemy.png"))
self.mole.setZValue(1)
self.mole.hide()
# 创建计数器
self.btn_click = self.scene.addSimpleText("计数:" + str(self.counter))
self.btn_click.setZValue(2)
self.font = QFont()
self.font.setPointSize(20)
self.btn_click.setFont(self.font)
self.btn_click.setPos(0,0)
self.btn_click.hide()
# 创建结算画面
font = QFont() # 字体设置
font.setPointSize(50)
font2 = QFont()
font2.setPointSize(20)
self.gameover = self.scene.addSimpleText("你干嘛哎嗨哟")
self.gameover.setZValue(2)
self.gameover.setFont(font)
self.gameover.setPos(140,300)
self.gameover.hide()
self.score = self.scene.addSimpleText("你用"+ str(self.counter)+"枪击毙了坤坤")
self.score.setZValue(2)
self.score.setFont(font2)
self.score.setPos(220,100)
self.score.hide()
# 将鼠标跟踪设置为True,以便获取鼠标位置
self.setMouseTracking(True)
# 存储图像项的引用
self.pixmap_items = []
# 设置自定义鼠标图片
self.setCustomCursor("zhunxin.png")
# 设置窗口布局
self.setCentralWidget(self.view)
self.sound()
def sound(self):
# 创建音效
self.player = QMediaPlayer()
self.player2= QMediaPlayer()
self.player3= QMediaPlayer()
self.player4= QMediaPlayer()
self.player5= QMediaPlayer()
self.player6= QMediaPlayer()
soundFile2 = QUrl.fromLocalFile("shoot.mp3")
soundFile = QUrl.fromLocalFile("niganma.mp3")
soundContent = QMediaContent(soundFile)
soundContent2 = QMediaContent(soundFile2)
self.player.setMedia(soundContent)
self.player2.setMedia(soundContent2)
self.player3.setMedia(soundContent2)
self.player4.setMedia(soundContent2)
self.player5.setMedia(soundContent2)
self.player6.setMedia(soundContent2)
self.soundtrack=[self.player2,self.player3,self.player4,self.player5,self.player6]
def xunhuan(self):
self.soundtrack[self.i].play()
self.i+=1
if self.i==5:
self.i=0
def setCustomCursor(self, cursor_image):
cursor_pixmap = QPixmap(cursor_image)
# 设置光标形状为自定义图像
cursor = QCursor(cursor_pixmap)
# 将光标设置为应用程序的全局光标
QApplication.setOverrideCursor(cursor)
# 创建按钮
def create_btn(self,text,x,y,w,h,gamechoose):
btn = QPushButton(text, self.view)
btn.setGeometry(x, y, w, h)
btn.clicked.connect(gamechoose)
return btn
# 选择难度按钮
def choosebtn(self):
self.gameover.hide()
self.btn.hide()
self.btn1=self.create_btn("简单难度",280,100,100,30,self.startGame1)
self.btn2=self.create_btn("中等难度",280,250,100,30,self.startGame2)
self.btn2.setStyleSheet("background-color: green; color: white")
self.btn3=self.create_btn("困难难度",280,400,100,30,self.startGame3)
self.btn3.setStyleSheet("background-color: yellow")
self.btn4=self.create_btn("地狱难度",280,550,100,30,self.startGame4)
self.btn4.setStyleSheet("background-color: red; color: white")
self.btn1.show()
self.btn2.show()
self.btn3.show()
self.btn4.show()
# 隐藏选择按钮并重新计数
def hide_btn(self):
self.btn1.hide()
self.btn2.hide()
self.btn3.hide()
self.btn4.hide()
self.counter = 0
self.btn_click.setText("计数:" + str(self.counter))
# 计数窗口
def count_window(self):
self.btn_click = self.scene.addSimpleText("计数:" + str(self.counter))
self.btn_click.setZValue(2)
self.font = QFont()
self.font.setPointSize(20)
self.btn_click.setFont(self.font)
self.btn_click.setPos(0,0)
self.timer.start(self.interval, self)
self.mole.show()
self.hide_btn()
def startGame1(self):
self.interval = 1000
self.count_window()
def startGame2(self):
self.interval = 500
self.count_window()
def startGame3(self):
self.interval = 300
self.count_window()
def startGame4(self):
self.interval = 100
self.count_window()
def timerEvent(self, event):
if event.timerId() == self.timer.timerId():
# 随机移动地鼠
self.mole.setPos(random.randint(0, 600), random.randint(0, 600))
else:
super().timerEvent(event)
def mousePressEvent(self, event):
# 点击地鼠时,将地鼠隐藏
if self.mole.isUnderMouse():
self.mole.hide()
self.gameover.show()
self.restart = QPushButton("重新开始", self.view)
self.restart.setGeometry(280, 250, 100, 30)
self.restart.show()
self.score.setText("你用"+ str(self.counter)+"下抓住了坤坤")
self.score.show()
self.restart.clicked.connect(self.choosebtn)
self.restart.clicked.connect(self.restartclick)
self.btn_click.hide()
self.player.play()
if self.background.isUnderMouse():
self.counter += 1
self.click_text()
# 跟踪鼠标
pixmap = QPixmap("dankong.png")
self.gun=self.scene.addPixmap(pixmap)
self.gun.setPos(event.pos() - pixmap.rect().center())
self.pixmap_items.append(self.gun)
self.xunhuan()
def click_text(self):
self.btn_click.setText("计数:" + str(self.counter))
def restartclick(self):
self.restart.hide()
self.btn_click.hide()
self.score.hide()
for gun in self.pixmap_items:
self.scene.removeItem(gun)
# 清空图像项的引用列表
self.pixmap_items = []
if __name__ == "__main__":
app = QApplication(sys.argv)
game = WhackAMole()
game.show()
# game.startGame()
sys.exit(app.exec_())
没有合适的资源?快使用搜索试试~ 我知道了~
使用pyqt5实现坤坤小游戏
共214个文件
qm:95个
dll:91个
pyd:12个
需积分: 0 0 下载量 68 浏览量
2023-11-25
09:25:06
上传
评论
收藏 60.73MB RAR 举报
温馨提示
最近刚开始接触pyqt5,边学边玩,学习创建画布、按钮、文字,信号与槽,利用这些基本功能简单做了一个小游戏,代码参考https://blog.csdn.net/pippizer/article/details/134607128?spm=1001.2014.3001.5501,本资源为打包好的代码、图片以及exe文件
资源推荐
资源详情
资源评论
收起资源包目录
使用pyqt5实现坤坤小游戏 (214个子文件)
opengl32sw.dll 19.95MB
Qt5Gui.dll 6.68MB
Qt5Core.dll 5.74MB
Qt5Widgets.dll 5.24MB
python38.dll 4.01MB
d3dcompiler_47.dll 3.98MB
Qt5Quick.dll 3.96MB
Qt5Qml.dll 3.43MB
libGLESv2.dll 3.23MB
libcrypto-1_1-x64.dll 3.06MB
libeay32.dll 1.9MB
qwindows.dll 1.41MB
Qt5Network.dll 1.28MB
ucrtbase.dll 993KB
qminimal.dll 825KB
qoffscreen.dll 737KB
Qt5Multimedia.dll 729KB
libssl-1_1-x64.dll 666KB
MSVCP140.dll 577KB
MSVCP140.dll 576KB
qwebp.dll 498KB
qwebgl.dll 471KB
Qt5QmlModels.dll 428KB
Qt5DBus.dll 426KB
qjpeg.dll 411KB
qtiff.dll 381KB
ssleay32.dll 354KB
Qt5Svg.dll 323KB
dsengine.dll 294KB
wmfengine.dll 203KB
Qt5WebSockets.dll 146KB
qwindowsvistastyle.dll 141KB
VCRUNTIME140.dll 99KB
VCRUNTIME140.dll 99KB
qtaudio_wasapi.dll 96KB
qxdgdesktopportal.dll 67KB
qtuiotouchplugin.dll 66KB
qtmedia_audioengine.dll 66KB
qtaudio_windows.dll 62KB
qgenericbearer.dll 52KB
python3.dll 51KB
qicns.dll 44KB
VCRUNTIME140_1.dll 43KB
VCRUNTIME140_1.dll 43KB
qsvgicon.dll 41KB
qgif.dll 38KB
qico.dll 37KB
qtmultimedia_m3u.dll 33KB
qsvg.dll 31KB
qtga.dll 31KB
MSVCP140_1.dll 31KB
qwbmp.dll 29KB
api-ms-win-crt-math-l1-1-0.dll 27KB
libEGL.dll 24KB
api-ms-win-crt-stdio-l1-1-0.dll 24KB
api-ms-win-crt-string-l1-1-0.dll 24KB
api-ms-win-crt-runtime-l1-1-0.dll 23KB
api-ms-win-crt-convert-l1-1-0.dll 22KB
api-ms-win-core-file-l1-1-0.dll 22KB
api-ms-win-core-localization-l1-2-0.dll 21KB
api-ms-win-crt-time-l1-1-0.dll 21KB
api-ms-win-core-processthreads-l1-1-0.dll 20KB
api-ms-win-crt-filesystem-l1-1-0.dll 20KB
api-ms-win-core-synch-l1-1-0.dll 20KB
api-ms-win-crt-process-l1-1-0.dll 19KB
api-ms-win-core-processenvironment-l1-1-0.dll 19KB
api-ms-win-crt-heap-l1-1-0.dll 19KB
api-ms-win-core-sysinfo-l1-1-0.dll 19KB
api-ms-win-crt-conio-l1-1-0.dll 19KB
api-ms-win-core-libraryloader-l1-1-0.dll 19KB
api-ms-win-core-console-l1-1-0.dll 19KB
api-ms-win-core-processthreads-l1-1-1.dll 19KB
api-ms-win-core-synch-l1-2-0.dll 19KB
api-ms-win-core-heap-l1-1-0.dll 19KB
api-ms-win-core-memory-l1-1-0.dll 19KB
api-ms-win-core-rtlsupport-l1-1-0.dll 19KB
api-ms-win-core-timezone-l1-1-0.dll 19KB
api-ms-win-crt-utility-l1-1-0.dll 19KB
api-ms-win-crt-environment-l1-1-0.dll 19KB
api-ms-win-crt-locale-l1-1-0.dll 19KB
api-ms-win-core-file-l2-1-0.dll 18KB
api-ms-win-core-interlocked-l1-1-0.dll 18KB
api-ms-win-core-errorhandling-l1-1-0.dll 18KB
api-ms-win-core-debug-l1-1-0.dll 18KB
api-ms-win-core-file-l1-2-0.dll 18KB
api-ms-win-core-util-l1-1-0.dll 18KB
api-ms-win-core-namedpipe-l1-1-0.dll 18KB
api-ms-win-core-datetime-l1-1-0.dll 18KB
api-ms-win-core-string-l1-1-0.dll 18KB
api-ms-win-core-handle-l1-1-0.dll 18KB
api-ms-win-core-profile-l1-1-0.dll 18KB
击毙坤坤.exe 1005KB
niganma.mp3 92KB
niganma.mp3 92KB
shoot.mp3 26KB
shoot.mp3 26KB
background.png 6KB
background.png 6KB
enemy.png 4KB
enemy.png 4KB
共 214 条
- 1
- 2
- 3
资源评论
pippizer
- 粉丝: 5
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功