Pygame actual combat: this little game of "happy playing hamster" makes hundreds of millions of people "addicted"“

Keywords: Python Programming Back-end Programmer

Introduction

​​

Have nothing to do:

The excrement shoveling officer made a hamster toy for the cat owner with waste cardboard boxes. Look, the owner has a lot of fun

I knocked it out - eh, why, why are there two? Isn't it exciting? This is a good chance to improve your relationship with the master!

​​

Ha ha ha! After reading the above introduction, you know what game I'm going to play today ~ hum, yes, today I'll write you a little game of beating hamsters

Play, you can play when you are bored at home! However, there is also a summary of games at the end of the article. So many previous game projects can be changed with

Then we can choose by ourselves. We can try any game we want to play~

Text

Brief introduction:

Hamster is a classic agile game. We can see this kind of game console at the door of many shopping malls, on computers and hands

There are many similar games on the machine. Because the large game machine is inconvenient to carry, and the Games in computer and mobile phone are not as good as swimming

The experience effect of the play machine is good. So I used Python to make this hamster box, which players can pack and carry freely to work

When you are bored, the computer can run directly, and you can also play games to play soy sauce. Hahaha ~ today's game is a simple version of the code

Not many ha, there will be time to do an upgraded version ~ you can also optimize yourself!

Rules of the game:

There are special records for hitting one point, hitting several points within the specified time and fleeing several points. Game initial settings

It's set to be 30 seconds, but you can set the time yourself. The more you hit, the better. You can also compete with your friends.

​​

I. preparation

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

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

Big wolf in pot:

​​

Another example is that a cat catches a mouse. It's very simple** Change the pattern of gopher, and then change the hammer into a pan ~ * * here is a useful png

Just use the image generated website directly:

https://www.remove.bg/zh/upload

2, Environment installation

The environment involved in this article: Python 3, pychar, Pygame, Pyqt5 and some built-in modules.

Module installation: you can use whatever you are used to. Using image source installation is faster and not easy to report errors.

pip install +Module name or image source with watercress  pip install -i https://Pypi. Doublan. COM / simple / + module name

III. start typing code

1) Import module

import sysimport osfrom PyQt5 import QtCorefrom PyQt5 import QtWidgetsfrom PyQt5.QtCore import *from PyQt5.QtGui import *from PyQt5.QtWidgets import *from PyQt5.QtSql import QSqlDatabase, QSqlQueryimport randomimport pygame

2) Different picture settings

The gopher pictures produced by running the code, the gopher after being smashed, and the hammer pictures smashed and not smashed are in different states.

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 / cave 2.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)}")  
# 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("Eliminate hamsters games")        self.setWindowIcon(QIcon(r'sucai/Icon.jpg'))        
self.timer = QTimer()  
# Global timer        
self.timer.timeout.connect(self.gameover)  
# Called at the end        
self.virustimer = QTimer()  
# Occurrence time timer self.virustimer.timeout.connect (self. Virusactive) self.remaintimer = qtimer()  
# Remaining time timer self.remaintimer.timeout.connect (self. Remaintimeshow) self.virussign = none        
self.user = None        
global score        
score = 0

4) Music settings

Run the code to have background music, click pause, mute, etc.

 # 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) The right window control has different button settings

# Right fixed in window control        
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)  
# Binding signal        
self.textBrowser = QTextBrowser()        self.textBrowser.setText('Game not started')        self.textBrowser.setFixedSize(70, 40)        
self.killBrowser = QTextBrowser()        
self.killBrowser.setText('Number of hamsters destroyed:0')        self.killBrowser.setFixedSize(70, 50)        
self.escapeBrowser = QTextBrowser()        self.escapeBrowser.setText('Number of hamsters fleeing:0')        self.escapeBrowser.setFixedSize(70, 50)        self.remaintimeText = QTextBrowser()        self.remaintimeText.setText('Remaining time:\n30s')        self.remaintimeText.setFixedSize(70, 55)        self.endPushButton = QPushButton("End the 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)

4, Effect display

Maybe the material of the picture is not very good! So it seems that the boundaries are clear! Self optimization ha~

Part 1 interface initialization

Part 2 Click the game start button

Countdown starts - missed

Part 3 hit the hamster

5, Little cartoon

summary

National beating of hamsters: slow response when you get old? "Beating the hamster" game, test how fast you react! Hahaha, children can still use it

Let's exercise our reaction ability and brain active cells. Of course, we only play when we're bored fishing! Ha ha ha ha

Your support is my biggest motivation!! Remember three times in a row, oh ~mua welcome to read previous articles~

Click on the article summary for more games -

Private letter Xiaobian 06 can be obtained for free~

Posted by jbruns on Thu, 25 Nov 2021 23:51:13 -0800