<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.10.0/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.10.0/addons/p5.sound.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
<meta charset="utf-8" />
</head>
<body>
<main></main>
<script>
let playerX = 0;
let playerZ = 0;
let playerY = -500;
let speed = 5;
let speedIncrement = 0.01;
let trees = [];
let score = 0;
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
resetGame();
createScoreElement();
}
function draw() {
background(200);
// Set up some lighting
directionalLight(255, 255, 255, 0, -1, -1);
ambientLight(150);
// Set camera position to first-person perspective
camera(playerX, playerY, playerZ, playerX, playerY, playerZ - 500, 0, 1, 0);
// Draw an infinite-looking ground plane
push();
fill(100, 200, 100);
translate(0, -400, 0);
rotateX(HALF_PI);
plane(10000, 10000); // Make a large single plane to give the appearance of an infinite ground
pop();
// Update and draw trees
for (let tree of trees) {
tree.z += speed; // Trees move towards the player
if (tree.z > playerZ + 500) {
tree.z = random(-5000, -500); // Reset tree position behind the player
tree.x = playerX + random(-400, 400); // Trees respawn near the player's x position
score++; // Increase score when successfully dodging a tree
updateScoreElement();
speed += speedIncrement; // Increase the speed after dodging a tree
}
// Check for collision with the player
if (abs(tree.x - playerX) < 50 && abs(tree.z - playerZ) < 50) {
resetGame();
return;
}
// Draw tree trunk
push();
translate(tree.x, playerY, tree.z);
fill(139, 69, 19); // Brown color for the trunk
box(30, 200, 30);
pop();
// Draw tree foliage
push();
translate(tree.x, playerY - 150, tree.z);
fill(34, 139, 34); // Green color for foliage
sphere(100); // Use a sphere to represent the foliage
pop();
}
// Handle movement
if (keyIsDown(LEFT_ARROW)) {
playerX -= 10;
}
if (keyIsDown(RIGHT_ARROW)) {
playerX += 10;
}
if (keyIsDown(UP_ARROW)) {
playerZ -= 10;
}
if (keyIsDown(DOWN_ARROW)) {
playerZ += 10;
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
document.addEventListener("click", () => {
if (document.pointerLockElement !== document.body) {
document.body.requestPointerLock();
}
});
function createScoreElement() {
const scoreDiv = document.createElement("div");
scoreDiv.id = "score";
scoreDiv.style.position = "absolute";
scoreDiv.style.top = "10px";
scoreDiv.style.left = "10px";
scoreDiv.style.color = "black";
scoreDiv.style.fontSize = "24px";
scoreDiv.style.fontFamily = "Arial, sans-serif";
document.body.appendChild(scoreDiv);
updateScoreElement();
}
function updateScoreElement() {
const scoreDiv = document.getElementById("score");
if (scoreDiv) {
scoreDiv.textContent = `Score: ${score}`;
}
}
function resetGame() {
playerX = 0;
playerZ = 0;
speed = 5;
score = 0;
trees = [];
for (let i = 0; i < 50; i++) {
let treeX = random(-1000, 1000); // Trees start far away from the player's x position // Trees start near the player's x position
let treeZ = random(-5000, -500); // Trees start behind the player and move towards them
trees.push({ x: treeX, z: treeZ });
}
if (document.getElementById("score")) {
updateScoreElement();
}
}
</script>
</body>
</html>
The problems were that game was 2D, so it was harder to make the trees look like they are moving past the player.
I’ve modified it so that the game is 3D, so it is easier to make the trees look like they are moving past the player.
Sorry for not making the code very similar to your original, as I had tried many runs with. In the end, I could only fix it by making it as similar as possible from scratch.
Thanks for the challenge!