const canvas = document.getElementById('game');
const context = canvas.getContext('2d');
// width and height of each platform and where platforms start
const platformWidth = 65;
const platformHeight = 20;
const platformStart = canvas.height - 50;
// player physics
const gravity = 0.33;
const drag = 0.3;
const bounceVelocity = -12.5;
// minimum and maximum vertical space between each platform
let minPlatformSpace = 15;
let maxPlatformSpace = 20;
// information about each platform. the first platform starts in the
// bottom middle of the screen
let platforms = [{
x: canvas.width / 2 - platformWidth / 2,
y: platformStart
}];
// get a random number between the min (inclusive) and max (exclusive)
function random(min, max) {
return Math.random() * (max - min) + min;
}
// fill the initial screen with platforms
let y = platformStart;
while (y > 0) {
// the next platform can be placed above the previous one with a space
// somewhere between the min and max space
y -= platformHeight + random(minPlatformSpace, maxPlatformSpace);
// a platform can be placed anywhere 25px from the left edge of the canvas
// and 25px from the right edge of the canvas (taking into account platform
// width).
// however the first few platforms cannot be placed in the center so
// that the player will bounce up and down without going up the screen
// until they are ready to move
let x;
do {
x = random(25, canvas.width - 25 - platformWidth);
} while (
y > canvas.height / 2 &&
x > canvas.width / 2 - platformWidth * 1.5 &&
x < canvas.width / 2 + platformWidth / 2
);
platforms.push({ x, y });
}
// the doodle jumper
const doodle = {
width: 40,
height: 60,
x: canvas.width / 2 - 20,
y: platformStart - 60,
// velocity
dx: 0,
dy: 0
};
// keep track of player direction and actions
let playerDir = 0;
let keydown = false;
let prevDoodleY = doodle.y;
// game...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.