https://editor.p5js.org/Lieslge/sketches/ZQpKRNqY8
This sketch is a small interactive dodge game. The player controls a purple ball at the bottom of the screen using the arrow keys, trying to avoid the blue falling balls. You get more points the longer you survive, but the speed of the falling balls also increases which makes the game harder as you score more.
// simple ball game
let player;
let balls = [];
let score = 0;
let gameOver = false;
// how fast the balls fall at the start and how often a new ball appears
let ballSpeed = 2;
let spawnRate = 35;
function setup() {
createCanvas(400, 600);
noStroke();
// make the player near bottom of screen
player = {
x: width / 2,
y: height - 50,
size: 40,
speed: 6
};
textAlign(CENTER);
}
function draw() {
background(240);
if (!gameOver) {
// left and right arrow keys to move the player
if (keyIsDown(LEFT_ARROW)) {
player.x -= player.speed;
}
if (keyIsDown(RIGHT_ARROW)) {
player.x += player.speed;
}
player.x = constrain(player.x, player.size / 2, width - player.size / 2);
// draw player
fill(190, 150, 255);
ellipse(player.x, player.y, player.size);
// new balls
if (frameCount % spawnRate === 0) {
let newBall = {
x: random(width),
y: -20,
size: random(20, 50),
speed: ballSpeed + random(1)
};
balls.push(newBall);
}
for (let i = balls.length - 1; i >= 0; i--) {
let b = balls[i];
b.y += b.speed;
fill(140, 200, 255);
ellipse(b.x, b.y, b.size);
// check if the player hits any of the fallen balls
let d = dist(b.x, b.y, player.x, player.y);
if (d < b.size / 2 + player.size / 2) {
gameOver = true;
}
// if ball goes off screen score plus 1
if (b.y > height + b.size) {
balls.splice(i, 1);
score++;
// the speed of the falling balls increases every 10 points
if (score % 10 === 0) {
ballSpeed += 0.8;
spawnRate = max(15, spawnRate - 3);
}
}
}
// drawing score
fill(0);
textSize(22);
text("Score: " + score, width / 2, 30);
} else {
// game over screen
fill(0);
textSize(32);
text("Game Over!", width / 2, height / 2 - 20);
textSize(18);
text("Press R to Restart", width / 2, height / 2 + 20);
textSize(22);
text("Final Score: " + score, width / 2, height / 2 + 60);
}
}
// restart game when "R" is pressed
function keyPressed() {
if (gameOver && (key === 'r' || key === 'R')) {
balls = [];
score = 0;
gameOver = false;
ballSpeed = 2;
spawnRate = 35;
player.x = width / 2;
}
}
Declaring variables
let player;
let balls = [];
let score = 0;
let gameOver = false;
let ballSpeed = 2;
let spawnRate = 35;
Conditional
if (!gameOver) {
if (keyIsDown(LEFT_ARROW)) {
player.x -= player.speed;
}
if (keyIsDown(RIGHT_ARROW)) {
player.x += player.speed;
}
player.x = constrain(player.x, player.size / 2, width - player.size / 2);
This line is a conditional statement. It means to only run the following code if the game is not over. The exclamation mark !means NOT, so !gameOver translates to ‘gameOver is false.’ This prevents the player from moving once the game has ended.
keyIsDown(LEFT_ARROW) checks whether the left arrow key is being pressed. If true, the player’s position is updated by subtracting its speed: player.x -= player.speed. The variable player.x is changed by a small amount each frame.
Similarly, if the right arrow is pressed, player.x will increase. Adding moves it right, subtracting moves it left — that’s how we simulate horizontal motion frame by frame.
constrain(value, min, max) is a built-in p5.js function that returns a limited value. It’s like a safety fence for variables.
value is smaller than min, it returns min.value is larger than max, it returns max.This is to keep the player on the screen.
For loops
for (let i = balls.length - 1; i >= 0; i--) {
let b = balls[i];
// move ball downward
b.y += b.speed;
// draw the ball
ellipse(b.x, b.y, b.size);
// check if hit player
let d = dist(b.x, b.y, player.x, player.y);
if (d < b.size / 2 + player.size / 2) {
gameOver = true;
}
// if ball goes off screen
if (b.y > height + b.size) {
balls.splice(i, 1);
score++;
}
}