Code your own Artillery-style tank game | Wireframe #44

Fire artillery shells to blow up the enemy with Mark Vanstone’s take on a classic two-player artillery game

Artillery Duel was an early example of the genre, and appeared on such systems as the Bally Astrocade and Commodore 64 (pictured).

To pick just one artillery game is difficult since it’s a genre in its own right. Artillery simulations and games have been around for almost as long as computers, and most commonly see two players take turns to adjust the trajectory of their tank’s turret and fire a projectile at their opponent. The earliest versions for microcomputers appeared in the mid-seventies, and the genre continued to develop; increasingly complex scenarios appeared involving historical settings or, as we saw from the mid-90s on, even offbeat ideas like battles between factions of worms.

To code the basics of an artillery game, we’ll need two tanks with turrets, a landscape, and some code to work out who shot what, in which direction, and where said shot landed. Let’s start with the landscape. If we create a landscape in two parts – a backdrop and foreground – we can make the foreground destructible so that when a missile explodes it damages part of the landscape. This is a common effect used in artillery games, and sometimes makes the gameplay more complicated as the battle progresses. In our example, we have a grass foreground overlaid on a mountain scene. We then need a cannon for each player. In this case, we’ve used a two-part image, one for the base and one for the turret, which means the latter can be rotated using the up and down keys.

Our homage to the artillery game genre. Fire away at your opponent, and hope they don’t hit back first.

For this code example, we can use the Python dictionary to store several bits of data about the game objects, including the Actor objects. This makes the data handling tidy and is quite similar to the way that JSON is used in JavaScript. We can use this method for the two cannons, the projectile, and an explosion object. As this is a two-player game, we’ll alternate between the two guns, allowing the arrow keys to change the angle of the cannon. When the SPACE bar is pressed, we call the firing sequence, which places the projectile at the same position as the gun firing it. We then move the missile through the air, reducing the speed as it goes and allowing the effects of gravity to pull it towards the ground.

We can work out whether the bullet has hit anything with two checks. The first is to do a pixel check with the foreground. If this comes back as not transparent, then it has hit the ground, and we can start an explosion. To create a hole in the foreground, we can write transparent pixels randomly around the point of contact and then set off an explosion animation. If we test for a collision with a gun, we may find that the bullet has hit the other player and after blowing up the tank, the game ends. If the impact only hit the landscape, though, we can switch control over to the other player and let them have a go.

So that’s your basic artillery game. But rest assured there are plenty of things to add – for example, wind direction, power of the shot, variable damage depending on proximity, or making the tanks fall into holes left by the explosions. You could even change the guns into little wiggly creatures and make your own homage to Worms.

Here’s Mark’s code for an artillery-style tank game. To get it working on your system, you’ll need to install Pygame Zero. And to download the full code and assets, head here.

Get your copy of Wireframe issue 44

You can read more features like this one in Wireframe issue 44, available directly from Raspberry Pi Press — we deliver worldwide.

And if you’d like a handy digital version of the magazine, you can also download issue 44 for free in PDF format.

Wireframe #44, bringing the past and future of Worms to the fore.

Make sure to follow Wireframe on Twitter and Facebook for updates and exclusive offers and giveaways. Subscribe on the Wireframe website to save up to 72% compared to newsstand pricing!

4 comments

James Carroll avatar

I got Scorched3d working on my Pi4. The game plays great but the graphics rendering is spotty. Sometimes it’ll map correctly and then other times it wont. Hopefully when working 3D happens this will be solved. It’s the ultimate tank battle game in my opinion and it’s free and open source.

Harry Hardjono avatar

For some reason, I feel like the projects aren’t something that is relevant to me. I mean, they’re cool in their own right, but those are not something I can do. I’m still using RaspiZero for my main dev, and it’s like people don’t cater to it anymore. The latest MagPi pdf is quite big, that it’s a cumbersome to do it on the Zero. Does Bookshelf actually help with that, or is it just a fancy directory viewer?
My latest project is a Typing Tutor, and well, I did it in Bash, and it only clocks in at about 4Kb. I don’t suppose anybody is interested in that anymore? But it’s relevant to me. Ever since I update my phone, I can’t use online typing tutor anymore. That’s why I wrote my own. That’s what it means to Learn to Code. Not cool stuff. Everyday stuff, as long as it’s relevant is good, too.

George avatar

After he and 27 years of programming large Client Server networks, I get to play on my own PeeWee 400. Seriously, you couldn’t build enough to meet demand???

Gunn avatar

This is really cool. I’ve been looking for something like this for a long time. I used the Scorched3d in my pie but it seems to be quite hard to do. But this made it easier. Thank you

Comments are closed