In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use Python to achieve Mini Game aircraft war". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. Environmental installation
This article is written by the game code, based on the Pygame source project, here is their own version of the aircraft war 1.0, relatively crude.
Prepare: Python3, Pycharm, pygame modules and some of the modules that come with them.
Pip install-I https://pypi.douban.com/simple/ pygame II. Our aircraft def main (): global is_hit global nums screen = pygame.display.set_mode ((480,852), 0 32) background = pygame.image.load (". / feiji/background.png") # create the aircraft hero = HeroPlane (screen) enermy_plane = EnermyPlane (screen) while True: global hero_nums global enermy_nums # display background screen.blit (background, (0) 0) # shows that our aircraft hero.display () # tests whether hero.test (enermy_plane.bullets) if hero.is_hit: hero_nums + = 1 if hero_nums = = 10: hero.image = pygame.image.load (". / feiji/hero_blowup_n1.png") Elif hero_nums = = 20: hero.image = pygame.image.load (". / feiji/hero_blowup_n2.png") elif hero_nums = = 30: hero.image = pygame.image.load (". / feiji/hero_blowup_n3.png") elif hero_nums = = 40: hero.image = pygame.image.load ( ". / feiji/hero_blowup_n4.png") elif hero_nums > 50: break III. Enemy aircraft # shows enemy aircraft enermy_plane.display () enermy_plane.test (hero.bullets) if enermy_plane.is_hit: enermy_nums + = 1 if enermy_nums = = 10: enermy_plane.image = pygame.image.load (". / feiji/enemy0_down1.png") elif enermy_nums = = 20: Enermy_plane.image = pygame.image.load (". / feiji/enemy0_down2.png") elif enermy_nums = = 30: enermy_plane.image = pygame.image.load (". / feiji/enemy0_down3.png") elif enermy_nums = = 40: enermy_plane.image = pygame.image.load (". / feiji/enemy0_down4.png") Elif enermy_nums > 50: enermy_plane = EnermyPlane (screen) enermy_nums = 0 else: enermy_plane.move () enermy_plane.fire () # Control aircraft left and right key_control (hero) # Refresh screen pygame.display.update () # Program hibernates for 0.01s time.sleep (0.01s) Control keyboard movement def key_control (hero): for event in pygame.event.get (): if event.type = = QUIT: print ("exit") exit () elif event.type = = KEYDOWN: if event.key = = Kattora or event.key = = K_LEFT: hero.move_left () print ("left" ") elif event.key = = krypd or event.key = = K_RIGHT: hero.move_right () print (" right ") elif event.key = = K_SPACE: hero.fire () print (" space ") 5. Detect bullet collision class BasePlane (object): def _ init__ (self) Screen, x, y, image): self.x = x self.y = y self.screen = screen self.image = pygame.image.load (image) self.is_hit = False # this flag is used to indicate whether the plane was hit or not self.bullets = [] def test (self Bullets): for bullet in bullets: if self.x < bullet.x < self.x + self.image.get_width () and\ self.y < bullet.y < self.y + self.image.get_height (): self.is_hit = True # detect bullet collision for item in Self.bullets: for bullet in bullets: if item.x < bullet.x < item.x + item.image.get_width () and\ item.y < bullet.y < item.y + item.image.get_height (): item.is_hit = True bullet.is_hit = True VI, Effect picture
This is the end of the content of "how to use Python to achieve Mini Game aircraft Battle". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 294
*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.