Week 13

Making a Simple “Platform” Game

Part 1 – platform “landscape” with horizontal scrolling

platforms is an array of simple objects with x, y and w (width) fields. All platforms are 10 pixels thick (see draw()). Platforms are “shifted” by offset when we draw them. When new platforms are needed (as the offset increases), we create new platforms. Old platform objects are removed as they scroll off the left edge of the canvas.

Part 2 – adding a “mario” character

We always draw the character in the middle of the canvas, so we only worry about the vertical (Y) coordinate. We need to find which platform is currently in the middle of the screen, so we do a linear search to find it. Then we move the “mario” based on whether it is above, on, or below the relevant platform. The character automatically “wraps around” when it falls, which means we need no interaction to do some testing.

Part 3 – Adding “jump” command and simple physics for falling

To implement jumping, we start by adding marioDy, which is the velocity of the “mario” character. We have to add more logic for controlling position, but now the “jump” command is simply setting marioDy to -10 to give it an upward velocity. (See line 113.)

Here is the sketch so far

Note that Dy (marioDy) continues to increase when the character is on a platform, and the character does not fall in a plausible way from the platform, so there is more work to do here.

game-part3