Tower Defense Game with Python: Basics #1

Tower Defense Game with Python: Basics #1
Page content

This post will talked about a project I’m working on. This project is a tower defense with python, I made this project to help my little brother to learn how to code.

I want to experiments so many things that’s why I will make a series of blog post directly linked to this project.


What is a Tower Defense ?

A tower defense is a genre of strategy games where the goal is to defend a player’s territories or possessions by obstructing the enemy attackers, usually achieved by placing defensive structures on or along their path of attack.

Image of Tower Defense
Image of Tower Defense


The beginning

To make this game, we will use a library called pygame . Pygame is a standard graphic library which is used to make games with python.

Basics elements

We will start by creating some components with pygame such as the game window, set the background images and a base game loop.

To start we will just use a simple background but we will be create a the background in a procedural way in the next episodes.

Result get with above code with the basic background.

Game information ( HUD )

We will now add some ingame information in the screen like money , time , remaining monsters and health point. To group all these information we will crete our first python class “GameInformation”. GameInformation class code is really short but we will had a lot of info later in coming episodes.

Let’s create a basic hud window which will contains useful game information.

For the moment we will create a gray rectangle with some placeholders to illustrate the look of the HUD. We will also create e function to display a text at a specific location using pygame.

Hitbox Class

In order to simplify the detection of any collide between monsters, projectiles and towers we will create a dedicated class called “Collide”.

This class will contains position and size of the object and the related hitbox (pygame rect).

Monster Class

We will now create a first monster by coding the class monster.

This monster will be a represented by a placeholder for the moment.

The class only contains the constructor for the moment, but we will add some methods later

We use a list to store all the monsters as below.

We also create a function which display both monster hitboxs and placeholders.

Pink rectangle are monsters placeholders.

Tower Class

We will now create the first Tower which will shoot projectiles and do damage to monsters.

Let’s code a tower class.

This class contains a lot of informations about the tower including a shoot method which create an instance of the projectile class.

Find the target

In order to find the correct target for each tower, we will use 2 functions .

  • get_targeatable_monsters which will get monsters within the range of the tower.

  • current_tower_target which will get the target for each tower in the list of targeatable monsters.

Projectile Class

Current Result

It shows 2 monsters moving and a tower which shoots the ennemy if he is inside the range.

Pink rectangle are monsters placeholders.