Master the half-pipe with our retro skating minigame challenge, programmed in Python and Pygame Zero. Mark Vanstone has the code, straight from this month’s Wireframe magazine
Skate or Die! came out in 1987 for the ZX Spectrum, NES, and other computers. Players were treated to five different skateboarding events including half-pipe stunts and downhill races. The aim was to score points by completing the challenges faster or with better sequences of stunts.
For our Pygame Zero take on Skate or Die!, we’ll focus on the half-pipe minigame. The original had quite complicated controls to perform tricks and steer the skateboard, but for this code example, we’ll just focus on the skateboard speed and making turns at the apex of the half-pipe. We will use the left and right arrows to increase speed in each direction and the up arrow to do a turn.
The first thing to do is draw our background. For this example, we’ve used the background from the C64 version, but you could make up your own version. We also need two skater images: one facing left and one facing right. We’ll start the skater at the top of the half-pipe on the right-hand side of the screen. Once we’ve defined our skater as an Actor, we can add properties like direction and speed. We’ll control the speed value with the arrow keys. The left arrow will reduce the speed value, and the right arrow will increase the speed value. A minus value will mean the skater moves left and a positive value means the skater moves right.
Now to make our skater move with the contour of the half-pipe. We need a way to work out what angle the ground is and where the skater is, and to do this we can make a gradient map. We use black pixels to indicate 90 degrees, white pixels to represent flat ground, and shades of grey for everything in between. So when our skater is over white pixels on the gradient map, they’ll move horizontally, and as the pixels tend towards black, the movement will be more vertical. We don’t have to draw this gradient map to the screen, but we access it by loading it with the Pygame image module and then using the
image.get_at() function to read the pixel colour directly under our skater Actor.
As the gradient map is being read, we should see our skater moving down the half-pipe, across the flat floor, and then up the other side. We next need to change the angle of the skater to match the angle of the half-pipe, and we can get that from the gradient map, too. White pixels set the skater to 0 degrees; if they’re on the right-hand side of the screen, black pixels mean 90 degrees, while on the left side, -90 degrees. Now we have an angle for our skater, we need to apply some gravitational effect to the speed so that as they’re heading downwards, they accelerate, and when going up, they slow down. This will mean that the skater needs to be going fast enough to reach the apex of the half-pipe in order to do a turn in the air, but if they go too fast, they’ll end up on the flat ground on the other side.
To get a turn in the air, we’ll detect the up arrow key press. If the skater angle is greater than 75 degrees or less than -75, we trigger a turn. We set a countdown variable called
switch, which we’ll check in the
update() function and move the skater vertically up for 30 frames, switch their direction, and then move them down again to complete the turn. We can count a score of how many times our skater completes a turn without ending up back on the flat ground at the top of the half-pipe. If the skater fails too many times, they’ll end up falling off the bottom of the half-pipe. We can use the SPACE bar to reset them back to the starting point.
And that’s the mechanics of our half-pipe game. The original Skate or Die!’s minigames each used slightly different controls and backgrounds, but we’ll leave you to adapt our existing project to those challenges.
Get your copy of Wireframe issue 58
You can read more features like this one in Wireframe issue 58, available directly from Raspberry Pi Press – we deliver worldwide, and there are plenty of subscription offers to save you money on the price of an issue.
And if you prefer your magazines in digital form, you can also download Wireframe issue 58 as a free PDF!