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/01 Report--
This article mainly introduces the relevant knowledge of "how Python realizes the aircraft war game". The editor shows you the operation process through the actual case, the operation method is simple and fast, and the practicality is strong. I hope this article "how to realize the aircraft war game in Python" can help you solve the problem.
Plane_main.py
Import pygamefrom aircraft Battle. Plane _ sprites import * class PlaneGame (object): def _ init__ (self): print (Game initialization) # 1. Create the game window self.screen = pygame.display.set_mode (SCREEN_RECT.size) # 2. Create the game clock self.clock = pygame.time.Clock () # 3. Create game private methods, sprite and sprite group creation self.__create_sprites () # 4. Set timer time-create enemy pygame.time.set_timer (CREATE_ENEMY_EVENT, 1000) pygame.time.set_timer (HERO_FIRE_EVENT, 500) def _ _ create_sprites (self): # create background wizard and spirit group bg1 = Background () bg2 = Background (True) self.back_group = pygame.sprite.Group (bg1 Bg2) # create Elf groups for enemy planes self.enemy_group = pygame.sprite.Group () # create Elf and Elf groups for Heroes self.hero = Hero () self.hero_group = pygame.sprite.Group (self.hero) def start_game (self): print ("Game starts") while True: # 1. Set the refresh frame rate self.clock.tick (FRAME_PER_SEC) # 2. Time monitoring self.__event_handler () # 3. Collision detection self.__check_collide () # 4. Update / draw sprite group self.__update_sprites () # 5. Update the display pygame.display.update () def _ _ event_handler (self): for event in pygame.event.get (): # determine whether to quit the game if event.type = = pygame.QUIT: PlaneGame.__game_over () elif event.type = = CREATE_ENEMY_EVENT: print (" Enemy plane comes out-") # create enemy wizard enemy = Enemy () # add enemy wizard to enemy wizard group self.enemy_group.add (enemy) elif event.type = = HERO_FIRE_EVENT: self.hero.fire () # elif Event.type = = pygame.KEYDOWN and event.key = = pygame.K_RIGHT: # print ("move to the right") # use the method provided by the keyboard to obtain the keyboard key-key tuple keys_pressed = pygame.key.get_pressed () # determine the corresponding key index value if keys_ pressed [pygame.K _ RIGHT] in the tuple: Self.hero.speed = 10 elif keys_ preset [pygame.K _ LEFT]: self.hero.speed =-10 else: self.hero.speed = 0 def _ check_collide (self): # 1. Bullets destroy enemy aircraft pygame.sprite.groupcollide (self.hero.bullets, self.enemy_group, True, True) # 2. The enemy plane collided with and destroyed the hero enemies = pygame.sprite.spritecollide (self.hero, self.enemy_group True) # determine whether the list contains content if len (enemies) > 0: # Let the hero sacrifice self.hero.kill () # end the game PlaneGame.__game_over () def _ _ update_sprites (self): self.back_group.update () self.back_group.draw (self) .screen) self.enemy_group.update () self.enemy_group.draw (self.screen) self.hero_group.update () self.hero_group.draw (self.screen) self.hero.bullets.update () self.hero.bullets.draw (self.screen) @ staticmethod def _ _ game_over (): print ("Game over") Pygame.quit () exit () if _ _ name__ = = "_ _ main__": # create the game object game = PlaneGame () # start the game game.start_game ()
Plane_sprites.py
Import randomimport pygame# screen size constant SCREEN_RECT = pygame.Rect (0,0,512,768) # refreshed frame rate FRAME_PER_SEC = 6pm create enemy aircraft timer constant CREATE_ENEMY_EVENT = pygame.USEREVENT# hero firing bullet event HERO_FIRE_EVENT = pygame.USEREVENT + 1class GameSprite (pygame.sprite.Sprite): "" aircraft War Game Wizard "" def _ init__ (self, image_name) " Speed=1): # call the initialization method of the parent class super (). _ _ init__ () # define the object property self.image = pygame.image.load (image_name) self.rect = self.image.get_rect () self.speed = speed def update (self): # move self in the vertical direction of the screen. Rect.y + = self.speedclass Background (GameSprite): "Game background wizard" def _ _ init__ (self " Is_alt=False: # 1. Call the parent class method to create the wizard super (). _ init__ (". / images/img_bg_level_5.jpg") # 2. Determine whether to alternate the image if is_alt: self.rect.y =-self.rect.height def update (self): # 1. Call the method of the parent class to implement super (). Update () # 2. Determine whether to remove the screen if self.rect.y > = SCREEN_RECT.height: self.rect.y =-self.rect.heightclass Enemy (GameSprite): "def _ init__ (self): # 1. Call the parent method to create the enemy wizard and specify the enemy image super (). _ init__ (". / images/img-plane_2.png") # 2. Specify the initial random speed of the enemy aircraft self.speed = random.randint (2,5) # 3. Specify initial random position of enemy aircraft self.rect.bottom = 0 max_x = SCREEN_RECT.width-self.rect.width self.rect.x = random.randint (0, max_x) def update (self): # 1. Call the parent method to keep flying vertically super (). Update () # 2. Determine whether or not to fly off the screen, if so, remove the enemy plane from the elf group if self.rect.y > = SCREEN_RECT.height: print ("the plane flies off the screen and needs to be removed from the elf group -") # kill method can remove the wizard from all spirit groups The wizard will automatically destroy self.kill () def _ del__ (self): # print ("enemy plane hung% s"% self.rect) passclass Hero (GameSprite): "" Hero Elf "" def _ _ init__ (self): # 1. Call the parent method and set image&speed super (). _ init__ (". / images/hero2.png", 0) # 2. Set the hero's initial position self.rect.centerx = SCREEN_RECT.centerx self.rect.bottom = SCREEN_RECT.bottom-20 # 3. Create bullet genie group self.bullets = pygame.sprite.Group () def update (self): # Heroes move horizontally self.rect.x + = self.speed # Heroes cannot leave the screen if self.rect.x
< 0: self.rect.x = 0 elif self.rect.right >SCREEN_RECT.right: self.rect.right = SCREEN_RECT.right def fire (self): for i in (0,1,2): # 1. Create bullet wizard bullet = Bullet () # 2. Set the sprite location bullet.rect.bottom = self.rect.y-iSprite 40 bullet.rect.centerx = self.rect.centerx # 3. Add the bullet to the wizard group self.bullets.add (bullet) class Bullet (GameSprite): "" bullet wizard "" def _ init__ (self): # call the parent method, set the bullet image, and set the initial speed super (). _ init__ (". / images/bullet_11.png",-5) def update (self): # call the parent method Let the bullet fly vertically off the screen super (). Update () # to determine whether the bullet flew out of the screen if self.rect.bottom < 0: self.kill () def _ _ del__ (self): print ("bullet destroyed") this is the end of the introduction to "how Python implements the game of aircraft wars". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.