Introduction:

Play Whack-a-Mole?

Ha ha ha ~ today is fishing day, together to hit the gopher!

Let’s have a race to see who gets the highest score! Perfect pass!

The body of the

A brief explanation of the game rules:

After the game code runs, you can enter the game directly. At this point, there will be a screen that selects the game to start. Gophers will poke a head, or a pair of eyes, out of holes in the ground in an attempt to hide from the players. Do not softhearted, directly knock you hit the hammer, pat down, and strive to a accurate, to a hit a, to two hit a pair. This is a game of points the more the better oh ~

(1) Environmental installation:

Python3, PyCharm2021 community Edition, the main Pygame, pyQT5 module, and some built-in modules.

Module installation:

pip install  pygame  
pip install pyqt5
Copy the code

Detailed third-party module installation installation methods can be seen in the past articles have drops: module installation tutorial.

(2) The main picture materials for gopher shooting are burrow, gopher retracting, gopher coming out of the hole, etc. As follows:

​​

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)Set the background music and music interface mute, play and other functions.

Mixer.music.load (r"sucai/ game.mp3 ") PyGame.mixer.music.play () @qtCore.pyQtslot () def # Mixer.mixer.music.load (r"sucai/ game.mp3" 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

(4) The game will hit the gopher after launch, but there must be some missed!

def virusactive(self): if self.virusSign ! = None: exec("self.virus%s.flag = 0" % self.virusSign) exec("self.virus%s.setStyleSheet('QPushButton{border-image: }')" % self.virusSign) self.virusSign = int(random.random() * 25) # print(self.virusSign) exec("self.virus%s.flag = 1" % self.virusSign) exec( "self.virus%s.setStyleSheet('QPushButton{border-image: Url (./sucai/virusout2.png)}')" % self.virusSign) # self.virustimer.start(1000) self.virusnum += 1 Self. KillBrowser. SetText (" hit count: "+ STR (score). Self escapeBrowser. SetText (" escape from number:" + STR (self. Virusnum - score - 1))Copy the code

(5) There is a time limit for the game. Each time you play will be reduced to remind you of the remaining time and strengthen the sense of urgency of the game.

def remainTimeshow(self): Remaintime = int (round (self. The timer. RemainingTime () / 1000, 0)) # print (remaintime) self. RemaintimeText. SetText (" remaining time: \n" + str(remaintime) + "s") self.remaintimer.start(1000)Copy the code

Game effects:

conclusion

All right! That’s it for whac-a-Mole,

Production is not easy, remember one key three even oh!! This article complete source code old place source base: private xiaobian 06 free to get ha!

If you need a new installation package activation code, complete project + source notes, and more Python materials, you can find them in the source base!!

​​