Introduction:
Idle:
Shovel-dung officer with old cardboard boxes, to the cat master made a dozen moles toy, look, the master is playing very happy
Knocked out — why, there are, there are two more? Is it very enchanted? This is a great opportunity to bond with 🐱 master!
Ha ha ha! After reading the introduction above, I know what game I want to make today, huh? Today I want to write a whack-a-Mole game for everyone
Have fun while you’re at it! However, there is also a summary of games at the end of the article, so many game items can be randomly selected by everyone, want to play any game can try
Oh ~
The body of the
Introduction:
Whac-a-mole 🐀 is a classic agile game that can be found in many shopping malls, as well as on computers and mobile phones
Games, because of large game consoles are not convenient to carry, and the games in computers and mobile phones are not as good as the experience of game consoles. So, I use
Python created this whac-a-Mole game box that you can pack and take with you when you’re bored at work
Hahaha, this game is a simple version of the code is not a lot of ha, when there is time to do an upgraded version of it, we can also optimize it!
Rules of the game:
Hit a product of one point, the specified time to hit a few is a few, escape a few is a few have special records. The game starts with 30
Seconds, can set their own time of ha, hit the more the better, but also with small partners competition.
One, under preparation
1) Material preparation (music + background + icon, etc.)
These can be modified by yourself! In fact, just modify the image material can become a new game! For example, the frying pan for Grey Wolf:
Again for instanceCats catch mice and so on very simple! Replace the gopher and the hammer with a pan. Here’s a handy PNG image generation site to use:
https://www.remove.bg/zh/upload
Copy the code
Two, environmental installation
Environments covered in this article: Python3, Pycharm, Pygame, Pyqt5, and some built-in modules.
Module installation: we used to use what with what, using the image source installation more quickly not easy to report errors.
PIP install + module name or douban image source PIP install -i https://pypi.douban.com/simple/ + module nameCopy the code
Start typing code
1) Import modules
import sys
import os
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
import random
import pygame
Copy the code
2) Different picture Settings
The gopher picture produced by running the code, the gopher after being hit, and then hit down, the hammer picture is not the same state.
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/pit 2. PNG)} ") # pit self. UpTime = QTimer () the self. The upTime. The timeout. Connect (self. The up) def kill (self) : try: if self.flag == 1: self.setStyleSheet("QPushButton{border-image: 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")))Copy the code
3)Interface header, title, etc.
class TopWindow(QWidget): def __init__(self, parent=None): Super ().__init__(parent) self.virusnum = 0 self.setwindowTitle (" kill Geeky ") self.setwindowIcon (QIcon(r'sucai/ icon.jpg ')) Self. The timer = QTimer () # global timer. The self timer. The timeout. Connect (self. Gameover) # at the end of the call self. Virustimer = QTimer (#) in the timer Self. Virustimer. A timeout. Connect (self. Virusactive) self. Remaintimer = QTimer (#) for the rest of the timer self.remaintimer.timeout.connect(self.remainTimeshow) self.virusSign = None self.user = None global score score = 0Copy the code
4) Music Settings
You can run code with background music, pause, mute, and so on.
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. The pause () else: BTN. The setText (" music 🎵 "). Pygame mixer. Music. Unpause ()Copy the code
5) Different button Settings for the right window control
Self.settingswidget = QWidget() self.settingSlayOut = QGridLayout() self.settingsWidget.setFixedSize(80, 350) self.imagelayout.addWidget(self.settingsWidget, 0, 5, 1, 5) the self. SettingsWidget. SetLayout (self. Settingslayout) # self. StartPushButton = QPushButton (" game ") self. StartPushButton = QtWidgets. QPushButton (text = "game", clicked = self. Handle_play_button) self. StartPushButton. SetFixedSize (80, # 40) self. StartPushButton. Clicked. Connect self. (self. Gamestart) # binding signal textBrowser = QTextBrowser () Self. TextBrowser. SetText (' before the game started) self. TextBrowser. SetFixedSize (70, 40) self. KillBrowser = QTextBrowser () the self. The killBrowser. The setText (' destroy rat number: 0 ') self. KillBrowser. SetFixedSize (70, 50) self. EscapeBrowser = QTextBrowser (self). EscapeBrowser. The setText (' fleeing to rat number: 0 ') self. EscapeBrowser. SetFixedSize (70, 50) self. RemaintimeText = QTextBrowser (self). RemaintimeText. The setText (' remaining time: \ 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)Copy the code
Iv. Effect display
May be the picture of the material is not very good! So it looks like the boxes are well defined! Self-optimization ha ~
Part 1 Interface initialization
Part 2 Click the game start button
The countdown began — missed
Part 3 Hit a gopher
Five, small cartoon
conclusion
Whac-a-mole for all: Slow to react in old age? Whack-a-mole! Test your reflexes! Hahaha children can also be used to exercise reaction ability, exercise
Brain active cells, we ~ of course is bored when fishing only play! Ha ha ha ha
For the full free source code:
For complete project source code + source material base see: # private letter xiaobian 06# or click on the blue text to add you can get free benefits!
Your support is my biggest motivation!! Mua welcomes you to read the previous article
Article summary —
Python 1.1-2021 | for the article summary | continuously updated, direct look at this article is enough ~