Rik Cross, Senior Learning Manager here at the Raspberry Pi Foundation, shows you how to recreate the deadly explosions in the classic game, Bomberman.
Creating Bomberman
Bomberman was first released in the early 1980s as a tech demo for a BASIC compiler, but soon became a popular series that’s still going today. Bomberman sees players use bombs to destroy enemies and uncover doors behind destructible tiles. In this article, I’ll show you how to recreate the bombs that explode in four directions, destroying parts of the level as well as any players in their path!
The game level is a tilemap stored as a two-dimensional array. Each tile in the map is a Tile
object, which contains the tile type, and corresponding image. For simplicity, a tile can be set to one of five types; GROUND
, WALL
, BRICK
, BOMB
, or EXPLOSION
. In this example code, BRICK
and GROUND
can be exploded with bombs, but WALL
cannot, but of course, this behaviour can be changed.
Each Tile
object also has a timer, which is decremented each frame of the game. When a tile’s timer reaches 0, an action is carried out, which is dependent on the tile type. BOMB
tiles (and surrounding tiles) turn into EXPLOSION
tiles after a short delay, and EXPLOSION
tiles eventually turn back into GROUND
. At the start of the game, the tilemap for the level is generated, in this case consisting of mostly GROUND
, with some WALL
and a couple of BRICK
tiles. The player starts off in the top-left tile, and moves by using the arrow keys. Pressing the SPACE
key will place a bomb in the player’s current tile, which is achieved by setting the Tile
at the player’s position to BOMB
. The tile’s timer is also set to a small number, and once this timer is decremented to 0, the bomb tile and the tiles around it are set to EXPLOSION
.
The bomb explodes outwards in four directions, with a range determined by the RANGE
, which in our code is 3. As the bomb explodes out to the right, for example, the tile to the right of the bomb is checked. If such a tile exists (i.e. the position isn’t out of the level bounds) and can be exploded, then the tile’s type is set to EXPLOSION
and the next tile to the right is checked. If the explosion moves out of the level bounds, or hits a WALL
tile, then the explosion will stop radiating in that direction. This process is then repeated for the other directions.
There’s a nice trick for exploding the bomb without repeating the code four times, and it relies on the sine and cosine values for the four direction angles. The angles are 0° (up), 90° (right), 180° (down) and 270° (left). When exploding to the right (at an angle of 90°), sin(90) is 1 and cos(90) is 0, which corresponds to the offset direction on the x- and y-axis respectively. These values can be multiplied by the tile offset, to explode the bomb in all four directions.
Get your copy of Wireframe issue 12
You can read the rest of the feature in Wireframe issue 12, available now at Tesco, WHSmith, and all good independent UK newsagents.
Or you can buy Wireframe directly from Raspberry Pi Press – delivery is available worldwide. And if you’d like a handy digital version of the magazine, you can also download issue 12 for free in PDF format.
Make sure to follow Wireframe on Twitter and Facebook for updates and exclusives. Subscribe on the Wireframe website to save up to 49% compared to newsstand pricing!
Website: LINK