In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to make a whack-a-mole Mini games with Python." The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of small editor to study and learn "how to make a whack-a-mole Mini games with Python" together!
game screen
First of all, the game screen layout,
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'))app = QApplication(sys.argv)Display = TopWindow()Display.setFixedSize(900, 600)Display.show()sys.exit(app.exec_())
For PyQt5, you need to create a global Application object whose parameters are a list of command-line parameters. You can set the size of the game box by setFixedSize.
We also set the virusnum class variable as a mole count
Next, we create a gopher, and we arrange the burrows.
class virus(QPushButton): def __init__(self, parent=None): super().__ init__(parent) self.setFixedSize(160, 120) self.setStyleSheet("QPushButton{border-image: url(sucai/earthhole2.png)}") self.upTime = QTimer() self.upTime.timeout.connect(self.up)
Create 25 more holes.
for i in range(25): exec("self.virus{0}=virus()".format(i)) for i in range(5): for j in range(5): exec("self.imagelayout.addWidget(self.virus{0},{1},{2})".format(t, i, j)) t += 1
The effect of our program is as follows
sets tool bar
Let's add the necessary tool bars to the game page, such as the Start button, the game points box, and so on
#Fixed controls in right window 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)
Then create the relevant buttons and input boxes
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 viruses killed: 0') self.killBrowser.setFixedSize(70, 50) self.escapeBrowser = QTextBrowser() self.escapeBrowser.setText ('escape virus count: 0') self.escapeBrowser.setFixedSize(70, 50) self.remaintimeText = QTextBrowser() self.remaintimeText.setText ('time remaining: \n30s') self.remaintimeText.setFixedSize(70, 55)
here, that start button is handle differently than the other buttons, and when the program is in the game, the button change to "end game," so let's look at handle_play_button
@QtCore.pyqtSlot() def handle_play_button(self): btn = self.sender() if btn is not None: text = btn.text() if text == "Start the game": btn.setText("End game") self.gamestart() else: btn.setText("Start game") self.gameover()
For the two functions gamestart and gameover used, the code is as follows
def gamestart(self): score = 0 self.textBrowser.setText("playing") self.timer.start(30000) #1 execution in 30 seconds self.virustimer.start(1000) self.remaintimer.start(1000) def gameover(self): self.timer.stop() self.virustimer.stop() self.textBrowser.setText("Game over") self.mousenum = 0 for i in range(25): exec("self.virus{0}.flag = 0".format(i))
Finally, add buttons to the right side of the game page
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)
Now our game page looks like this
game points
Since it was a game, of course it had to have a point function. For gopher games, when we click the mouse, we hit the gopher.
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")))
And then we go through the logic of scoring.
def kill(self): try: if self.flag == 1: self.setStyleSheet("QPushButton{border-image: url(sucai/killvirus2.png)}") #hamsters smashed global score score += 1 self.flag = 0 except: pass Thank you for reading, the above is "how to use Python to make a whack-a-mole Mini games" content, after the study of this article, I believe everyone on how to use Python to make a whack-a-mole Mini games this problem has a deeper understanding, the specific use of the situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.