Code a gravity-defying platformer mechanic | Wireframe #65
Walk on the ceiling in our homage to Metal Storm on the NES. Mark Vanstone has the code in the latest issue of Wireframe magazine, out now.
At the start of 1991, a refreshingly new platformer came out for the NES. Metal Storm was unique in that it allowed the player to switch the direction of gravity so that their M-308 Gunner mech could walk on floors or ceilings. With the range of enemies and bosses sent out to destroy the player, it all made for quite a complex, challenging game for its time.
For this example, we’ll focus on this gravity mechanic to code a Pygame Zero version of the game. Metal Storm features a parallax scrolling background, which we create by drawing two background layers. The first is a blue squiggly pattern and will move at half the speed of the main platform layer. We track the position of the background X coordinate with the variable
mapx and divide that by 2 when drawing the blue background. Then we draw the section of the platform map that will appear on the screen.
To define where our platforms and walls are positioned, we use a small image with one pixel representing each platform block. We have green pixels for the green blocks, grey pixels for the grey blocks, and red pixels for the areas through which you can see the background. Each block is drawn as a 75 × 75 pixel image.
Next, we need our Gunner. A quick trip to spriters-resource.com will provide some suitable animation frames to make into an Actor. Our player character will normally be positioned in the centre of the screen on the X axis and move up and down on the Y axis.
We can set the left and right arrow keys to move the platforms behind the Gunner as it walks, but we need to check our minimap image to make sure it doesn’t walk through any walls. We can add a bit of code to the
movement function to check if our Gunner has reached the edge of the map, and if so, move the Gunner Actor instead of the platforms. The walking animation is done by cycling through numbered frames for either left or right movement.
We also want our Gunner to be able to jump. By default, we set our Actor to fall downwards until it reaches a platform. Some of the platforms will be low enough to jump up onto, and we can assign the SPACE bar to perform the jump animation. When the SPACE bar’s pressed, we assign a value to the jump property of the Gunner Actor. When we update the Actor, if the
jump property is greater than zero, we boost the Actor into the air and reduce the
jump value by 1. Once
jump reaches zero, we stop boosting and let the Actor fall by applying the default gravity calculation.
Now we have a Gunner walking and jumping around, we need to be able to switch the gravity from one direction to the other. To do this, we add a
gravity property to the Gunner Actor. To start with, we’ll set this to 1, and any calculations we do that involve movement on the Y axis, we multiply by this value. With this value as 1, everything will stay the same, the Gunner will fall downwards and jump upwards. But if we change it to -1, the same calculations will cause the Gunner to fall upwards and jump downwards, providing the effect of gravity being switched. This also means the mech can get past obstacles that we wouldn’t be able to jump over. We set this switch from 1 to -1 to happen when the up arrow is held down and the SPACE bar is pressed.
And there we have it: a gravity-switching, Metal Storm-style platformer. All that’s needed now is a range of enemy robots and a way to shoot them, but we’ll leave those additions to you.
Get your copy of Wireframe #65
You can read more features like this one in Wireframe issue 65, available directly from Raspberry Pi Press – we deliver worldwide.
And if you prefer your magazines in digital form, you can also download Wireframe issue 65 as a free PDF!
Metalstorm was an underrated game. I loved playing it back in the day, on the original NES console. :) Great gameplay physics.
Comments are closed