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 realize Tower Defense Game with Python Pygame

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Python Pygame how to achieve tower defense game", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "Python Pygame how to achieve tower defense game" bar!

I. Environmental requirements

Windows system, python3.6+

Installation module

Pip install pyqt5pip install pygame II, introduction to the game 1, game objectives

According to the checkpoint, design different tower defense maps (the blogger only designed three passes, interested students, after learning, you can draw your own map), set up three kinds of battery, each kind of battery fires different shells, each kind of shell has different lethality to the enemy, each kind of shell is different.

The price of the fort is also different. Players buy forts through coins and are equipped with the operation of cutting out forts (similar to shovels in plants vs. zombies). The enemy successfully reached the tower and the game was over.

2. Go to the game effect picture first.

3. Complete development process 1. Project main structure

First of all, first of all, sort out the main structure of the project. In fact, if you take a look at the main structure, it will be basically clear.

2. Detailed configuration

Config.py

In the configuration file, you need to introduce the os module, configure the screen size of the open game, and insert the pictures and audio referenced in the resources into the appropriate location.

Because of our maze game, we need to split the module.

'' profile''import os' screen size 'SCREENSIZE = (800,600)' Picture path 'IMAGEPATHS = {' choice': {'load_game': os.path.join (os.getcwd (),' resources/images/choice/load_game.png'), 'map1': os.path.join (os.getcwd (),' resources/images/choice/map1.png')) 'map1_black': os.path.join (os.getcwd (), 'resources/images/choice/map1_black.png'),' map1_red': os.path.join (os.getcwd (), 'resources/images/choice/map1_red.png'),' map2': os.path.join (os.getcwd (), 'resources/images/choice/map2.png'),' map2_black': os.path.join (os.getcwd ()) 'resources/images/choice/map2_black.png'),' map2_red': os.path.join (os.getcwd (), 'resources/images/choice/map2_red.png'),' map3': os.path.join (os.getcwd (), 'resources/images/choice/map3.png'),' map3_black': os.path.join (os.getcwd (), 'resources/images/choice/map3_black.png') 'map3_red': os.path.join (os.getcwd (), 'resources/images/choice/map3_red.png'),},' end': {'gameover': os.path.join (os.getcwd (),' resources/images/end/gameover.png'), 'continue_red': os.path.join (os.getcwd (),' resources/images/end/continue_red.png') 'continue_black': os.path.join (os.getcwd (), 'resources/images/end/continue_black.png'),},' game': {'arrow1': os.path.join (os.getcwd (),' resources/images/game/arrow1.png'), 'arrow2': os.path.join (os.getcwd (),' resources/images/game/arrow2.png') 'arrow3': os.path.join (os.getcwd (), 'resources/images/game/arrow3.png'),' basic_tower': os.path.join (os.getcwd (), 'resources/images/game/basic_tower.png'),' boulder': os.path.join (os.getcwd (), 'resources/images/game/boulder.png'),' bush': os.path.join (os.getcwd ()) 'resources/images/game/bush.png'),' cave': os.path.join (os.getcwd (), 'resources/images/game/cave.png'),' dirt': os.path.join (os.getcwd (), 'resources/images/game/dirt.png'),' enemy_blue': os.path.join (os.getcwd (), 'resources/images/game/enemy_blue.png') 'enemy_pink': os.path.join (os.getcwd (), 'resources/images/game/enemy_pink.png'),' enemy_red': os.path.join (os.getcwd (), 'resources/images/game/enemy_red.png'),' enemy_yellow': os.path.join (os.getcwd (), 'resources/images/game/enemy_yellow.png') 'godark': os.path.join (os.getcwd (), 'resources/images/game/godark.png'),' golight': os.path.join (os.getcwd (), 'resources/images/game/golight.png'),' grass': os.path.join (os.getcwd (), 'resources/images/game/grass.png'),' healthfont': os.path.join (os.getcwd ()) 'resources/images/game/healthfont.png'),' heavy_tower': os.path.join (os.getcwd (), 'resources/images/game/heavy_tower.png'),' med_tower': os.path.join (os.getcwd (), 'resources/images/game/med_tower.png'),' nexus': os.path.join (os.getcwd (), 'resources/images/game/nexus.png') 'othergrass': os.path.join (os.getcwd (), 'resources/images/game/othergrass.png'),' path': os.path.join (os.getcwd (), 'resources/images/game/path.png'),' rock': os.path.join (os.getcwd (), 'resources/images/game/rock.png'),' tiles': os.path.join (os.getcwd ()) 'resources/images/game/tiles.png'),' unitfont': os.path.join (os.getcwd (), 'resources/images/game/unitfont.png'),' water': os.path.join (os.getcwd (), 'resources/images/game/water.png'),' Xerox: os.path.join (os.getcwd (), 'resources/images/game/x.png'),} 'pause': {'gamepaused': os.path.join (os.getcwd (),' resources/images/pause/gamepaused.png'), 'resume_black': os.path.join (os.getcwd (),' resources/images/pause/resume_black.png'), 'resume_red': os.path.join (os.getcwd (),' resources/images/pause/resume_red.png'),} 'start': {'play_black': os.path.join (os.getcwd (),' resources/images/start/play_black.png'), 'play_red': os.path.join (os.getcwd (),' resources/images/start/play_red.png'), 'quit_black': os.path.join (os.getcwd (),' resources/images/start/quit_black.png') 'quit_red': os.path.join (os.getcwd (),' resources/images/start/quit_red.png'), 'start_interface': os.path.join (os.getcwd (),' resources/images/start/start_interface.png'),},}''Map path' MAPPATHS = {'1roads: os.path.join (os.getcwd (),' resources/maps/1.map')) '2characters: os.path.join (os.getcwd (), 'resources/maps/2.map'),' 3characters: os.path.join (os.getcwd (), 'resources/maps/3.map'),}' 'font path' FONTPATHS = {'Calibri': os.path.join (os.getcwd (),' resources/fonts/Calibri.ttf'), 'm04font: os.path.join (os.getcwd ()) 'resources/fonts/m04.ttf'),' Microsoft Sans Serif': os.path.join (os.getcwd (), 'resources/fonts/Microsoft Sans Serif.ttf'),}' 'settings'''DIFFICULTYPATHS = {' easy': os.path.join (os.getcwd (), 'resources/difficulties/easy.json'),' hard': os.path.join (os.getcwd (), 'resources/difficulties/hard.json') 'medium': os.path.join (os.getcwd (),' resources/difficulties/medium.json'),}''Audio path' 'AUDIOPATHS = {' bgm': os.path.join (os.getcwd (), 'resources/audios/bgm.mp3'),} 3, define the classes of enemies, towers, bullets

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