Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to achieve simple shooting Mini Game by Python+Pygame

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report