Day 1: Building a game in Python with Pygame

Objective: To begin our work on a small tower defense game inside of the pygame framework.

This is a fun little project where I will build a tower defense game using the Pygame libraries. Pygame is designed to drive game development with the Python language. In order for this project to begin, we will have to download and install Python onto our machine as well as import the Pygame libraries.

I wont go over installing Python or Pygame onto your machine as you can follow simple installation instructions which can be found online with google or by going directly to the Python we page. For this project I use Pycharm as my IDE of choice. I prefer Pycharm because of how user firendly and simple it is. Also, pycharm can read the folders you store game dev related content(i.e.: images for hte bg and characters and etc.) and recommend these files to you with intellisense.

Once you have python installed we now need to import it into our project simply by writing import pygame at the top of our main script.

After initializing Pygame, we need to set the dimensions of our game window with the variables we just created. we will use pygame.set_mode() and we will pass in our _screenHeight and _screenWidth variables into this function as seen in the image below. The next line sets the header text for the game window. Then we initialize the games loop time as well as the frame rate we desire.

Now in order for the window to stay open and draw our objects each frame we will need to set the game loop like this

while the game is running, if the user decides to exit the game by hitting the x button at the top right corner, then we simply quit the application. pygame.display.update() will come in handy soon after load our images. After setting up the game loop and screen size and header we will see a game window similar to the image below.

Now we have a game window, so we need to load our bg image so that we have a nice background for our game sprites to live on. We will load a UI image from a folder in the parent directory of our project

. a lot of times missing images will happen when we dont type the address in correctly as it is spelled.

now we are ready to assign our bg image to the game window. we will use the .Blit() method to draw the background image each frame.

We will now get the effect as seen in the image below.

Now that we have a background for our game, it would be nice to set up the castle object. This will be an instance of a class that we will write in python. First we will meed to define an image to represent the caslte.

We will use object oriented programming to create an instance of a Castle class. we define a class and its properties and functions as seen in the image below. We create a class called Castle and we create a constructor with def __init__(self, image, x value, y value, scale). we also create a Draw function that we can call to draw this object each frame.

Now we will update the display of the game window and draw the castle each frame with the code in the image below.

we now have somewhere to start with this small game and the effect can be seen in the image at the top of this article. What you see is the game window rendering our bg image file each frame and drawing the instance of our Castle class at our specified location every frame while the game is running.

Unity Dev . Web Dev . RN