Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Pygame to realize Happy hamster Game in Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people don't understand the knowledge points of this article "How to use Pygame to realize happy whacking mouse game in Python", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "How to use Pygame to realize happy whacking mouse game in Python".

Rules of the game:

A hit points, within a specified time hit a few is a few, escape a few is a few have a special record. game setup

Set is 30 seconds, can set their own time ha, hit the more the better, but also with small partners children competition.

I. Preparations

1) Material preparation (music + background + icons, etc.)

These can be modified by yourself! In fact, just modify the next picture material can become a new game! Like flat bottom.

Guo Da Hui Tai Lang:

For example, cats catch mice and so on is very simple! Replace the hamster pattern, and then replace the hammer with a pan ~

II. Environmental Installation

The environments covered in this article: Python 3, Pycharm, Pygame, Pyqt5, and some of the modules that come with it.

Module installation: we are used to what to use, using mirror source installation faster and less easy to report errors.

pip install + module name or with Douban mirror source pip install -i https://pypi.douban.com/simple/ + module name

3. Start typing code 1) import module import sysimport osfrom PyQt5 import QtCorefrom PyQt 5 import QtWidgets from PyQt5.QtCore import *from PyQt5.QtGui import *from PyQt5.QtWidgets import *from PyQt5.QtSql import QDatabase, QSqlQuery import randomimport pygame2) different image settings

The pictures of mice that ran the code, mice that were smashed, and hammer pictures that didn't fall down were different.

class virus(QPushButton): def __init__(self, parent=None): super().__ init__(parent) self.setFixedSize(160, 120) # self.resize(40, 30) self.setStyleSheet("QPushButton{border-image: url(sucai/burrow2.png)}") #burrow self.upTime = QTimer() self.upTime.timeout.connect(self.up) def kill(self): try: if self.flag == 1: self.setStyleSheet("QPushButton{border-image: url(sucai/killvirus2.png)}") #be smashed global score score += 1 self.flag = 0 except: pass def mousePressEvent(self, event): self.setCursor(QCursor(QPixmap(r"sucai/down.png"))) self.upTime.start(100) self.kill() def up(self): self.setCursor(QCursor(QPixmap(r"sucai/up.png")))3) Interface header, title, etc. class TopWindow(QWidget): def __init__(self, parent=None): super().__ init__(parent) self.virusnum = 0 self.setWindowTitle("Destroy Gophers Mini games") self.setWindowIcon(QIcon(r'sucai/icon.jpg')) self.timer = QTimer() #Global Timer self.timer.timeout.connect(self.gameover) #Called at end self.virustimer = QTimer() #occurrence time timer self.virustimer.timeout.connect(self.virusactive) self. remainder timer = QTimer() #remainder timer self.remaintimer.timeout.connect(self.remainTimeshow) self.virusSign = None self.user = None global score score = 04) Music settings

Run the code can have background music, you can also click pause, mute and so on.

#Play music pygame.init() pygame.mixer.music.load(r"sucai/Game.mp3") pygame.mixer.music.play() def handle_music_button(self): btn = self.sender() if btn is not None: text = btn.text() if text == "Music??? ": btn.setText("Mute???? ") pygame.mixer.music.pause() else: btn.setText("Music??? ") pygame.mixer.music.unpause()5) Right Window Controls Different Button Settings #Right Fixed Window Controls self.settingslayout = QGridLayout() #Grid Layout 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 game") self.startPushButton = QtWidgets.QPushButton(text="Start game", clicked=self.handle_play_button) self.startPushButton.setFixedSize(80, 40) # self.startPushButton.clicked.connect(self.gamestart) #bind signal self.textBrowser = QTextBrowser() self.textBrowser.setText ('game not started') self.textBrowser.setFixedSize(70, 40) self.killBrowser = QTextBrowser() self.killBrowser.setText ('Number of moles killed: 0') self.killBrowser.setFixedSize(70, 50) self.escapeBrowser = QTextBrowser() self.escapeBrowser.setText ('Number of Gophers Escaped: 0') self.escapeBrowser.setFixedSize(70, 50) self.remaintimeText = QTextBrowser() self.remaintimeText.setText ('time remaining: \n30s') self.remaintimeText.setFixedSize(70, 55) self.endPushButton = QPushButton("End game") self.endPushButton.setFixedSize(80, 40) self.endPushButton.clicked.connect(self.gameover) self.pauseMusicButton = QtWidgets.QPushButton(text="Music??? ", clicked=self.handle_music_button) self.pauseMusicButton.setFixedSize(80, 40) self.settingslayout.addWidget(self.startPushButton, 0, 0) self.settingslayout.addWidget(self.textBrowser, 1, 0) self.settingslayout.addWidget(self.killBrowser, 2, 0) self.settingslayout.addWidget(self.escapeBrowser, 3, 0) self.settingslayout.addWidget(self.remaintimeText, 4, 0) # self.settingslayout.addWidget(self.endPushButton, 5, 0) self.settingslayout.addWidget(self.pauseMusicButton, 6, 0)

May be that the material for the picture is not very good! So it looks like the frame is clearly defined! Self-optimization ha ~

Part 1 Interface initialization

Part 2 Click the Start button

Countdown starts--miss.

Part 3 Hit the gophers

The above is about the content of this article on "How to use Pygame to realize happy hamster game in Python". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will be helpful to everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report