Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use python to realize the Bomber College Game

Shulou Source: shulou.com Published: 2022-06-03 10:16:31 09月28日 Update

This article mainly introduces how to use python to achieve the bomber academy games, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand.

Text

The rules of the game are still clear, ha, I do not do more introduction is not clear how to play can be under Baidu!

First of all, prepare the corresponding material: [the part is as follows]

Bomber master program:

Import sysimport cfgimport randomimport pygamefrom modules import * 'game main program' 'def main (cfg): # initialize pygame.init () pygame.mixer.init () pygame.mixer.music.load (cfg.BGMPATH) pygame.mixer.music.play (- 1) 0) screen = pygame.display.set_mode (cfg.SCREENSIZE) pygame.display.set_caption ('bomber-Source Base: # 959755565) # start interface Interface (screen, cfg, mode='game_start') # Game main loop font = pygame.font.SysFont (' Consolas', 15) for gamemap_path in cfg.GAMEMAPPATHS: #-Map map_parser = mapParser (gamemap_path Bg_paths=cfg.BACKGROUNDPATHS, wall_paths=cfg.WALLPATHS, blocksize=cfg.BLOCKSIZE) #-Fruit fruit_sprite_group = pygame.sprite.Group () used_spaces = [] for i in range (5): coordinate= map_parser.randomGetSpace (used_spaces) used_spaces.append (coordinate) fruit_sprite_group.add (Fruit (random.choice (cfg.FRUITPATHS), coordinate=coordinate Blocksize=cfg.BLOCKSIZE) #-our Hero coordinate= map_parser.randomGetSpace (used_spaces) used_spaces.append (coordinate) ourhero = Hero (imagepaths=cfg.HEROZELDAPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser) Hero_name='ZELDA') #-computer Hero aihero_sprite_group = pygame.sprite.Group () coordinate= map_parser.randomGetSpace (used_spaces) aihero_sprite_group.add (Hero (imagepaths=cfg.HEROBATMANPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser) Hero_name='BATMAN') used_spaces.append (coordinate) coordinate= map_parser.randomGetSpace (used_spaces) aihero_sprite_group.add (Hero (imagepaths=cfg.HERODKPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser) Hero_name='DK')) used_spaces.append (coordinate) #-bomb bomb bomb_sprite_group = pygame.sprite.Group () #-flag is_win_flag = False #-main loop screen = pygame.display.set_mode (map_parser.screen_size) clock = pygame.time.Clock () while True: dt = clock.tick (cfg.FPS) for event in pygame.event.get (): if event.type = = pygame.QUIT: pygame.quit () sys.exit (- 1) #-↑↓←→ key controls up and down The space bar drops bombs elif event.type = = pygame.KEYDOWN: if event.key = = pygame.K_UP: ourhero.move ('up') elif event.key = = pygame.K_DOWN: ourhero.move (' down') elif event.key = pygame.K_LEFT: ourhero.move ('left') elif event.key = = pygame.K_RIGHT: ourhero.move (' right') elif event.key = = pygame.K_SPACE: if ourhero.bomb_cooling_count = len (instances_list [0]) or Instances_list [self.coordinate [1]] [xmax] in ['w' 'x roles,' z']: break explode_area.append ([xmax, self.coordinate [1]]) return explode_area''role classes' 'class Hero (pygame.sprite.Sprite): def _ _ init__ (self, imagepaths, coordinate, blocksize, map_parser * * kwargs): pygame.sprite.Sprite.__init__ (self) self.imagepaths = imagepaths self.image = pygame.image.load (imagepaths [- 1]) self.image = pygame.transform.scale (self.image, (blocksize, blocksize)) self.rect = self.image.get_rect () self.rect.left Self.rect.top = coordinate [0] * blocksize Coordinate [1] * blocksize self.coordinate = coordinate self.blocksize = blocksize self.map_parser = map_parser self.hero_name = kwargs.get ('hero_name') # Health self.health_value = 50 # bomb cooldown Self.bomb_cooling_time = 5000 self.bomb_cooling_count = 0 # Random move cooldown (for AI computers only) self.randommove_cooling_time = 100self.randommove_cooling_count = 0 'character move' 'def move (self Direction): self.__updateImage (direction) if direction = = 'left': if self.coordinate [0]-1

< 0 or self.map_parser.getElemByCoordinate([self.coordinate[0]-1, self.coordinate[1]]) in ['w', 'x', 'z']: return False self.coordinate[0] = self.coordinate[0] - 1 elif direction == 'right': if self.coordinate[0]+1 >

= self.map_parser.width or self.map_parser.getElemByCoordinate ([self.coordinate [0] + 1, self.coordinate [1]]) in ['wicked,' xtriple,'z']: return False self.coordinate [0] = self.coordinate [0] + 1 elif direction = = 'up': if self.coordinate [1]-1

< 0 or self.map_parser.getElemByCoordinate([self.coordinate[0], self.coordinate[1]-1]) in ['w', 'x', 'z']: return False self.coordinate[1] = self.coordinate[1] - 1 elif direction == 'down': if self.coordinate[1]+1 >

= self.map_parser.height or self.map_parser.getElemByCoordinate ([self.coordinate [0], self.coordinate [1] + 1]) in ['wicked,' x' 'z']: return False self.coordinate [1] = self.coordinate [1] + 1 else: raise ValueError ('Unknow direction% s..'% direction) self.rect.left, self.rect.top = self.coordinate [0] * self.blocksize Self.coordinate [1] * self.blocksize return True''Random Action (for AI computers)' 'def randomAction (self, dt): # cooling countdown if self.randommove_cooling_count > 0: self.randommove_cooling_count-= dt action = random.choice ([' left', 'left'] 'right',' right', 'up',' up', 'down',' down', 'dropbomb']) flag = False if action in [' left', 'right',' up', 'down']: if self.randommove_cooling_count

Tags: Bomb explosion screen life fruit computer article character countdown element map time range cooling academy reality main program area coordinates effect Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux Redmi Huawei Shulou Information