Get the App
SLTechnology News&Howtos  ›  Development  › 

How to achieve simple shooting Mini Game by Python+Pygame

Shulou Source: shulou.com Published: 2022-05-31 19:16:25 10月15日 Update

In this article, the editor introduces in detail "how to achieve simple shooting Mini Game by Python+Pygame" with detailed contents, clear steps and proper handling of details. I hope that this article "how to achieve simple shooting Mini Game by Python+Pygame" can help you solve your doubts.

I. operating environment

1) Environmental installation

Python3, Pycharm, Pygame module part of the built-in module is not displayed.

Installation of third-party libraries: pip install pygame or

Pip install-I https://pypi.douban.com/simple/ + module name with mirror source

2) material (pictures, music, etc.)

Second, code display

Main program

Import pygame,os,randomfrom pygame.locals import * from pygame.sprite import * def load_image (name): fullname=os.path.join (os.path.join (os.path.split (os.path.abspath (_ file__)) [0], "filedata"), name) image=pygame.image.load (fullname) return imagedef load_sound (name): fullname=os.path.join (os.path.join (os.path.split (os.path.abspath (_ file__)) [0], "filedata") Name) sound=pygame.mixer.Sound (fullname) return soundWIDTH=700HEIGHT=600class Explosion (Sprite): def _ _ init__ (self,screen,posrect): super (Explosion) Self). _ init__ () self.screen=screen self.posrect=posrect self.image=load_image ("explosion.png") self.rect=self.image.get_rect () self.rect=self.posrect self.rates=0 def update (self): self.rates+=1 if self.rates > = 40: self.kill () class Enemy (Sprite): def _ init__ (self,screen): super (Enemy) Self). _ init__ () self.screen=screen self.screenrect=self.screen.get_rect () self.image=load_image ("eatingfood.png") self.rect=self.image.get_rect () self.rect.center= (random.uniform (50) WIDTHMur50), random.uniform (50) HEIGHT-50)) self.xspeed=random.uniform (1, 2) self.yspeed=random.uniform (1, 2) if random.choice ([True,False]): self.xspeed=-self.xspeed if random.choice ([True]) False]): self.yspeed=-self.yspeed def update (self): self.rect.centerx+=self.xspeed self.rect.centery+=self.yspeed if self.rect.top > self.screenrect.height or self.rect.bottomself.screenrect.width or self.rect.right

Tags: Mini Game shooting effects articles modules code content environment proper main program dynamic picture Arial ideas screenshots new knowledge more steps game interface interface Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology Microsoft Docker Shulou Information