DWARF PROJECT
Managing game objects with Python
Tired of being called small and hairy, Andrew Smith grabs his double-headed axe and sets out to create a world full of Dwarfs.
OUR EXPERT
Andrew Smith is a software developer at NHS Digital, has a degree in software engineering and an MSc in computer networks (mobile and distributed).
T his issue we’re going to add to an already developed game platform, fully created in the Python scripting language using the PyGame library module. The theme of the project was inspired by an isometric video game in the early 1990s called HeroQuest (http://bit.ly/lxf277dwarf). This project first started off as an experiment in video game construction over one weekend when the question was asked: What if the game world moved around the main game character instead of moving the main game character around the game world?
The Dwarf Game Project that we’ll be modifying as part of this month’s tutorial consists of a constructed game map made up of interconnected rooms, game objects and enemy game characters (hostile dwarfs). The player of the game will control the main game character (a green dwarf), around the map disposing of all the enemy players to complete the game or until the main game character is killed by any of the enemy dwarf characters.
To get started, we’ll need a few things: Python, PyGame and the Dwarf Game Project.
To install Python, open a terminal window (Ctr-Alt-T) and type sudo apt-get python3 followed by sudo aptget install pip3 . Then install the PyGame module by typing pip3 install PyGame .
Finally, grab a copy of the Dwarf Game Project from the author’s GitHub repository:
mkdir lxf278dwarfproj
cd lxf278dwarfproj
git clone https://github.com/asmith1979/dwarfgame
The source code and project can also be retrieved from the Linux Format archive. Once cloned,you’ll notice the following folder structure: design, images, sfx and src. The src folder contains the Python source code and is also where the source code is executed.
cd src
python3 ./dwarfgame.py
If all goes well, you should obtain the following output of the Dwarf Game Project (see screenshot, above right):
Before continuing any further, feel free to get to know the game by moving the game character around the game world that’s been created. The cursor keys are used for movement and the Spacebar key is used for attack. The Escape key ends the game program. When you have become used to the gaming environment, press the Escape key to exit the game and continue with this tutorial.
PYGAME: THE MAIN LOOP
In every PyGame program there needs to be a main loop that renders the graphics used in the program and that also controls input for the game via keyboard and/or mouse depending on the PyGame application being developed.
Let’s take a look at the main loop implemented in this Dwarf Project.
while not programEnd:
for event in pygame.event.get():
if event.type == pygame.QUIT:
programEnd = True
if event.type == pygame.KEYDOWN
and event.key == pygame.K_ESCAPE:
programEnd = True
... As can be seen from the code, the main while loop ends on a boolean condition to identify when the program has finished completely. A variable called programEnd is used to identify this, which initially is set to a False boolean value.
In this particular main while loop, you’ll notice as you look through the code in the while loop that first the input to control the game is dealt with via the keys on the keyboard. The Escape key is used to exit the program, the cursor keys enable directional movement of the player and the Space bar invokes the attack move of the player.
Continuing on from this is the processing that’s carried out for both human and computer players, as well as screen rendering operations to process graphics for the game itself and everything in it.
Finally after doing all this we come to the final part of the loop, as shown below:
…
clock.tick(100)
pygame.display.flip()
From the above code we determine our framerate, which is set to 100, and then update the contents of the entire display.