🔬 These two blocks let you move left and right. What happens if you change -= to += in the first block? What would the player do then?
if (keyIsDown(LEFT_ARROW) || keyIsDown(65)) { // LEFT_ARROW or 'A' key
player.x -= player.speed;
}
if (keyIsDown(RIGHT_ARROW) || keyIsDown(68)) { // RIGHT_ARROW or 'D' key
player.x += player.speed;
}
🔬 These four lines run every time you dodge an object. What happens if you remove the line that increases objectSpeed so the objects never get faster?
// Remove objects that go off-screen and update score
if (obj.y > height + obj.height / 2) {
fallingObjects.splice(i, 1); // Remove object from array
score++; // Increase score
objectSpeed += 0.05; // Slightly increase object speed for difficulty
objectSpawnRate = max(30, objectSpawnRate - 0.5); // Slightly decrease spawn rate (more objects)
function draw() {
background(220); // Clear the background each frame
if (gameState === 'playing') {
// --- Player Movement ---
if (keyIsDown(LEFT_ARROW) || keyIsDown(65)) { // LEFT_ARROW or 'A' key
player.x -= player.speed;
}
if (keyIsDown(RIGHT_ARROW) || keyIsDown(68)) { // RIGHT_ARROW or 'D' key
player.x += player.speed;
}
// Keep player within canvas boundaries
player.x = constrain(player.x, player.width / 2, width - player.width / 2);
// --- Spawn New Objects ---
// Spawn a new object every 'objectSpawnRate' frames
if (frameCount % objectSpawnRate === 0) {
fallingObjects.push({
x: random(player.width / 2, width - player.width / 2), // Random x position
y: -10, // Start just above the canvas
width: 30,
height: 30,
speed: objectSpeed
});
}
// --- Update, Draw, and Check Collisions for Objects ---
// Loop through falling objects from end to beginning to safely remove them
for (let i = fallingObjects.length - 1; i >= 0; i--) {
let obj = fallingObjects[i];
obj.y += obj.speed; // Move object down
// Draw object
fill(0, 100, 200); // Blue color
noStroke();
rectMode(CENTER); // Draw rectangle from its center
rect(obj.x, obj.y, obj.width, obj.height);
// Check collision with player (simple AABB collision detection)
// This checks if the bounding boxes of the player and object overlap
if (
player.x - player.width / 2 < obj.x + obj.width / 2 && // Player's left edge is left of object's right edge
player.x + player.width / 2 > obj.x - obj.width / 2 && // Player's right edge is right of object's left edge
player.y - player.height / 2 < obj.y + obj.height / 2 && // Player's top edge is above object's bottom edge
player.y + player.height / 2 > obj.y - obj.height / 2 // Player's bottom edge is below object's top edge
) {
// Collision detected
gameState = 'gameOver';
}
// Remove objects that go off-screen and update score
if (obj.y > height + obj.height / 2) {
fallingObjects.splice(i, 1); // Remove object from array
score++; // Increase score
objectSpeed += 0.05; // Slightly increase object speed for difficulty
objectSpawnRate = max(30, objectSpawnRate - 0.5); // Slightly decrease spawn rate (more objects)
}
}
// --- Draw Player ---
fill(255, 0, 0); // Red color
rectMode(CENTER);
rect(player.x, player.y, player.width, player.height);
// --- Display Score ---
fill(0); // Black color
textSize(24);
textAlign(LEFT, TOP);
text('Score: ' + score, 10, 10);
} else if (gameState === 'gameOver') {
// --- Game Over Screen ---
fill(0);
textSize(48);
textAlign(CENTER, CENTER);
text('Game Over!', width / 2, height / 2 - 50);
textSize(32);
text('Final Score: ' + score, width / 2, height / 2);
textSize(24);
text('Press Space to Restart', width / 2, height / 2 + 50);
}
}