In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how Python Pygame implements Super Bomber. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
First, environmental installation 1. Material (picture)
two。 Environmental installation
This article is Mini Game written by Pygame.
Related to the running environment: Python3.7, Pycharm Community Edition, Pygame module.
Pip install + module name or pip install-I https://pypi.douban.com/simple/ + module name
Second, code demonstration 1. Profile''profile' import os' screen size 'SCREENSIZE = (640,480)' Block size 'BLOCKSIZE = 30'''FPS'''FPS = 30 games' game map path 'GAMEMAPPATHS = [os.path.join (os.getcwd (), path) for path in\ [' resources/maps/1.map'] 'resources/maps/2.map']]' 'Wall path' 'WALLPATHS = [os.path.join (os.getcwd (), path) for path in\ [' resources/images/misc/wall0.png', 'resources/images/misc/wall1.png',' resources/images/misc/wall2.png']]''Hero path' 'HERODKPATHS = [os.path.join (os.getcwd (), path) for path in\ [' resources/images/dk/left.png' 'resources/images/dk/right.png',' resources/images/dk/up.png', 'resources/images/dk/down.png'] HEROZELDAPATHS = [os.path.join (os.getcwd (), path) for path in\ [' resources/images/zelda/left.png', 'resources/images/zelda/right.png',' resources/images/zelda/up.png', 'resources/images/zelda/down.png']] HEROBATMANPATHS = [os.path.join (os.getcwd ()) Path) for path in\ ['resources/images/batman/left.png',' resources/images/batman/right.png', 'resources/images/batman/up.png',' resources/images/batman/down.png']''fruit path' 'FRUITPATHS = [os.path.join (os.getcwd (), path) for path in\ [' resources/images/misc/banana.png' 'resources/images/misc/cherry.png']]' 'background path' 'BACKGROUNDPATHS = [os.path.join (os.getcwd (), path) for path in\ [' resources/images/misc/bg0.png', 'resources/images/misc/bg1.png',' resources/images/misc/bg2.png']]''explosion and launch path' 'BOMBPATH = os.path.join (os.getcwd ()) 'resources/images/misc/bomb.png') FIREPATH = os.path.join (os.getcwd (),' resources/images/misc/fire.png') 'background music' 'BGMPATH = os.path.join (os.getcwd (),' resources/audio/bgm.mp3') 'some colors' YELLOW = (255,255,0) BLUE = (0,0,255) RED = (255,0,0) BLACK = (0,0,0) WHITE = (255,255,255) 2. Def main (cfg): # initialize pygame.init () pygame.mixer.init () pygame.mixer.music.load (cfg.BGMPATH) pygame.mixer.music.play (- 1,0.0) screen = pygame.display.set_mode (cfg.SCREENSIZE) pygame.display.set_caption ('bomber Mini Game') # start interface Interface (screen) Cfg, mode='game_start') # Game main cycle 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
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.