Bouncing Ball Catcher with p5play

Bouncing Ball Catcher

In this tutorial, we'll create a simple game where balls fall due to gravity, and the player moves a paddle to bounce them back up.

We'll use p5play, a physics engine for p5.js, which makes it easier to handle movement, collisions, and gravity.


Step 1: Setting Up p5play

What’s new?

  • In p5.js, we create a canvas(width, height).

  • In p5play, we use new Canvas("1:1"), which automatically scales the canvas to a 1:1 ratio.

Code:

function setup() {
  new Canvas("1:1"); // Auto-resizing canvas
  world.gravity.y = 1; // Enable gravity
}

Why is this useful? With p5play, you don’t have to manually update positions—gravity and physics handle it for you!


🏓 Step 2: Creating a Paddle

What’s new?

  • Instead of a rect(), we create a paddle sprite using new Sprite().

  • The KINEMATIC collider makes it immovable by physics, but we can still move it with code.

Code:

Why is this useful? In plain p5.js, you’d have to manually handle movement, but p5play handles collisions automatically.


Step 3: Adding Balls

What’s new?

  • Instead of using an array of objects, we create physics-enabled balls using new Sprite().

  • We set speed and direction instead of manually updating x and y.

Code

Why is this useful? Instead of manually updating positions, p5play handles physics like gravity and movement automatically.


🎮 Step 4: Adding Player Controls

What’s new?

  • Instead of checking keyIsDown(), we use p5play’s built-in kb.pressing().

  • We update the paddle’s velocity (paddle.vel.x), instead of setting paddle.x directly.

Code:

Why is this useful?

  • Instead of manually setting position, velocity-based movement feels smoother.

  • kb.pressing() is cleaner than keyIsDown().


🔄 Step 5: Handling Collisions

What’s new?

  • Instead of writing a manual collision check, we use paddle.collides(ball).

  • When the paddle hits the ball, we reverse the ball’s velocity to bounce it.

Code:

Why is this useful?

  • In p5.js, we’d have to manually calculate distance for collisions.

  • p5play automatically detects when objects touch!


🎯 Step 6: Adding Score & Removing Balls

What’s new?

  • We introduce a score that increases when catching balls.

  • If a ball falls off the screen, we remove it and decrease the score.

Code:

Why is this useful?

  • Instead of manually checking ball positions, p5play makes collision detection easy.

  • The game is now interactive and rewarding!


Step 7: Spawning More Balls Over Time

What’s new?

  • Every 60 frames (≈1 second), we add a new ball to keep the game challenging.

Code:

Why is this useful? This makes the game progressively harder, adding a natural difficulty curve.


🎉 Final Code


🎯 Recap

  • p5play simplifies physics!

  • No need to manually check collisions!

  • Smooth movement with vel.x instead of updating x directly.

  • Gravity, bouncing, and collision detection are built-in.

Would you like me to add extra challenges or an extension for this lesson? 😊

Last updated