xxxxxxxxxx
65
var x = 50;
var y = 50;
var playerSpeed = 0;
var gravity = 0.5;
var isJumping = false;
var moveSpeed = 5;
var platforms = [];
function setup() {
createCanvas(400, 400);
platforms.push({x: 0, y: height - 10, w: width, h: 10});
}
function draw() {
background(220);
for (var i = 0; i < platforms.length; i++) {
fill(127);
rect(platforms[i].x, platforms[i].y, platforms[i].w, platforms[i].h);
}
fill(255, 0, 0);
rect(x, y, 50, 50);
// Apply gravity
y += playerSpeed;
playerSpeed += gravity;
// Check for platform collision
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
if (x + 50 > platform.x &&
x < platform.x + platform.w &&
y + 50 > platform.y &&
y + 50 < platform.y + platform.h) {
y = platform.y - 50;
playerSpeed = 0;
isJumping = false;
}
}
if (keyIsDown(LEFT_ARROW)) {
x -= moveSpeed;
}
if (keyIsDown(RIGHT_ARROW)) {
x += moveSpeed;
}
if (y <= 0) {
textSize(32);
fill(0, 102, 153);
text("You Win!", 150, 200);
}
}
function keyPressed() {
if (keyCode === UP_ARROW && !isJumping) {
playerSpeed = -10;
isJumping = true;
}
}
function mousePressed() {
platforms.push({x: mouseX, y: mouseY, w: random(50, 150), h: random(10, 50)});
}