xxxxxxxxxx
let player;
let obstacles = [];
let gameover = false;
function setup() {
createCanvas(800, 400);
player = new Player(50, height - 50);
obstacles.push(new Obstacle(200, height - 50));
obstacles.push(new Obstacle(400, height - 100));
obstacles.push(new Obstacle(600, height - 50));
}
function draw() {
background(220);
for (let obstacle of obstacles) {
obstacle.display();
obstacle.update();
if (player.hits(obstacle)) {
gameover = true;
}
}
player.display();
player.update();
player.checkCollision();
if (gameover) {
noLoop();
textSize(32);
textAlign(CENTER, CENTER);
fill(255, 0, 0);
text("Game Over", width / 2, height / 2);
text("Press 'R' to Restart", width / 2, height / 2 + 50);
}
}
function keyPressed() {
if (key == ' ' && !player.isJumping() && !gameover) {
player.jump();
}
if (key == 'r' && gameover) {
resetGame();
}
}
function resetGame() {
player = new Player(50, height - 50);
obstacles = [];
obstacles.push(new Obstacle(200, height - 50));
obstacles.push(new Obstacle(400, height - 100));
obstacles.push(new Obstacle(600, height - 50));
gameover = false;
loop(); // Restart the game loop
}
class Player {
// ... (same as before)
hits(obstacle) {
return (
this.x + this.size / 2 > obstacle.x &&
this.x - this.size / 2 < obstacle.x + obstacle.width &&
this.y + this.size / 2 > obstacle.y &&
this.y - this.size / 2 < obstacle.y + obstacle.height
);
}
}
// ... (rest of the code remains the same)