console
var canvas = document.getElementById('stage');
var context = canvas.getContext('2d');
Math.randomRange = function (min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
var player = {
x: 0,
y: 300,
width: 30,
jumping: false,
power: 0,
speedY: 10
}
var gameover = false;
var cameraMoving = false;
var timerPowerUp;
var platforms = [];
platforms.push({
x: 0,
width: 100
}, {
x: 200,
width: 100
});
var lastPlatform = platforms[platforms.length - 1];
var currentPlatform = platforms[0];
var drawPlayer = function () {
context.fillStyle = "blue";
context.save();
context.fillRect(player.x, player.y, player.width, player.width);
context.restore();
}
var drawPlatform = function () {
context.fillStyle = "#A3A3A3";
context.save();
platforms.forEach(function (platform) {
context.fillRect(platform.x, 300 + player.width, platform.width, 300);
});
context.restore();
}
var drawTextFailed=function() {
if (gameover) {
context.fillStyle = "#000";
context.save();
context.font = "normal 30px sans-serif";
context.textBaseline = 'top';
var text = "FAILED";
context.fillText(text, canvas.clientWidth / 2 - text.length * 30 / 2, 300);
context.restore();
}
}
var playerJumping = function () {
if (player.jumping) {
player.y -= player.speedY;
player.speedY -= 0.7;
player.x += player.power;
if (player.y >= 300) {
player.jumping = false;
player.y = 300;
player.power = 0;
if (!iscurrentplatform()) {
if(isfailed()){
gameover=true;
}else{
currentPlatform = lastPlatform;
generateNextPlatform();
cameraMoving = true;
}
}
}
}
}
var generateNextPlatform = function () {
var x = Math.randomRange(lastPlatform.x+100, 500);
var width = Math.randomRange(player.width, 100);
lastPlatform = {
x: x,
width: width
}
platforms.push(lastPlatform);
}
var iscurrentplatform = function () {
return player.x > currentPlatform.x - player.width && player.x < currentPlatform.x + currentPlatform.width;
}
var isfailed = function () {
return player.x <= (lastPlatform.x - player.width) || player.x >= (lastPlatform.x + lastPlatform.width);
}
var moveCamera = function () {
if (cameraMoving) {
player.x -= 10;
platforms.forEach(function (platform) {
platform.x -= 10;
});
if (player.x <= 0 || currentPlatform.x <= 0) {
cameraMoving = false;
if (player.x < 0) {
player.x = 0;
} else if (currentPlatform.x < 0) {
currentPlatform.x = 0;
}
}
}
}
var PowerUping = function () {
player.power += 1;
}
var PowerUp = function () {
if (!gameover&&!player.jumping) {
timerPowerUp = setInterval(PowerUping, 100);
}
}
var PowerUpStop = function () {
if (!gameover&&!player.jumping){
clearInterval(timerPowerUp);
player.jumping = true;
player.speedY = 10;
}
}
var withFrameUpdate = function () {
playerJumping();
moveCamera();
}
var drawFrame = function () {
window.requestAnimationFrame(drawFrame, canvas);
context.clearRect(0, 0, canvas.width, canvas.height);
drawPlayer();
drawPlatform();
drawTextFailed();
withFrameUpdate();
}
document.addEventListener("mousedown", PowerUp);
document.addEventListener("mouseup", PowerUpStop);
drawFrame();
<body oncontextmenu="return false">
<canvas id="stage" width="800" height="600"></canvas>
</body>