PYTHON
Recreating the classic Bomberman
When we said “go out with a bang”, Andrew Smith took it to heart and decided to blow more than just the blinking doors off!
OUR EXPERT
Andrew Smith works as a developer for NHS Digital when he’s not updating ancient gaming code.
Grab the code for this tutorial with: git clone https:// github.com/ asmith1979/ lxf299_ bomberman/
Bomberman, developed by Hudson Soft in 1983 and currently owned by Kanomi, is a game in which a player has to eliminate every other player on the game grid to win. We’re looking at a clone of Bomberman created by Michal Sliva and Tomas Novak, the source code of which has been adapted for the benefit of this tutorial. The original source code for the project can be retrieved from https://github.com/Forestf90/Bomberman.
On a clean dev system, we need to install and set up Python 3.10. If you already have Python and PyGame installed, Python 3.8+ should be a good enough version to use with this tutorial. Type the following to install Python 3.10 and PyGame: $ sudo apt-get update $ sudo apt-get install python3.10 $ sudo apt-get install python3-pip $ python3.10 -m pip install pygame
To make sure you are calling the correct Pip version, check in the Scripts folder of the Python installation folder. There may be a version number of Pip to use – Pip 3.8, for example.
Check the Python version and PyGame version, then Git clone from the repository: $ git clone https://github.com/asmith1979/lxf299_ bomberman/
The source code and project can also be retrieved from the Linux Format archive. This tutorial focuses on the source code located in the folder called lxf299_ bomberman, so if not already in that folder, type $ cd lxf299_bomberman to get into the folder and gain access to the Python source code.
Game project
In the root of the lxf299_bomberman folder, there exist some of the Python files we will be looking at in more detail as part of this tutorial, including menu.py, game.py, enemy.py and player.py. Also in the folder is a folder called images, which stores all the images that are used in the game. The files are:
• menu.py– contains all code for the mouse-driven menu system.
A successful clone from the project repository. Once cloned, all the files are present, ready to view or edit.
DEPTH-FIRST SEARCH
Depth-first search (DFS) is an algorithm for searching or traversing through graph and tree data structures. In the context of this game project, the algorithm is used to help an enemy character choose what action to take depending on the current situation. On each level of the map, various actions are attempted, such as:
• Place a bomb + move down
Sometimes these moves may not be possible because of a box crate, bomb or explosion nearby, so another move by the enemy character may be taken instead. The enemy character is placed in a corner of the game grid, allowing analysis of the row of the grid they are situated in. The code for the depth-first search algorithm is located in the Enemy class implemented in enemy.py, mainly in the methods dfs and dfs_rec. The move that the DFS enemy character chooses to undertake is mainly based on the content outlay of the map – for example, where the box crates, bombs, explosions and other enemy characters are placed on the map. In the Python script file menu.py, there is a boolean variable called show_path that by default is set to True. While this is true, the path of the enemy character is shown that the enemy character will travel along.