AI - Bouncing Ball with Motion Sensor (Difficulty: 3)
-
Key Topics Covered
Introduction
Motion sensors allow your program to detect movement from the camera and use it as a form of input — no keyboard or mouse needed! This opens up exciting possibilities:
- People with disabilities who can’t use a keyboard or mouse can still interact with your project.
- Patients needing physical rehabilitation can benefit from motion-based games that get them moving.
- It’s a hands-free way to play — all you need is a camera!
In this tutorial, you’ll build a game where the player moves a paddle using body motion to stop a ball from falling off the screen.
Step 1 - Add the Video Sensing Extension
In the CreatiCode playground, create a new project, and add the video sensing extension first. See this page for instructions:
https://www.forum.creaticode.com/topic/458/the-video-sensing-extension
Step 2 - Draw a Bouncing Board
Delete the default dog sprite. Then, paint a new costume for the “Empty1” sprite — draw a symmetrical rectangle that will serve as the paddle. Rename the sprite to “Board”.
Step 3 - Set Initial Board Position
When the game starts, the board should be moved to the bottom center of the stage:
Step 4 - A Forever Loop to Read the Motion Data
Since the board will be controlled by the video sensor data, let’s use a forever loop to repeatedly read the motion data into 2 variables named “motion” and “direction”:
Now you can see these 2 variables’ values as you move left or right in front of the camera:
Step 5 - Set “Board Speed”
Create a variable named “board speed” to represent how fast the board moves. We’ll link it to the “motion” value. If the motion is strong, the board should move faster. For example:
- Set board speed to motion / 8
- If motion is below 20, set board speed to 0 (to ignore tiny movements or noise)
You can verify that the board speed is 0 when you are not moving:
Step 6 - Move the Board Left or Right
Use “board speed” and “direction” to control the board’s position:
- If direction > 0, the motion is to the right — move the board right.
- If direction < 0, the motion is to the left — move the board left.
Now your board should follow your body movements smoothly:
Step 7 - Keep the Board Inside the Stage
The board might drift off the screen edges. Prevent this by checking its X position and keeping it between -150 and 150.
Now the board will stay within the stage:
Step 8 - Add a New Sprite for the Ball
Add a new sprite: Beachball
- Set its size to 50
- Move it to the top of the stage (y = 160) when the program starts
It will look like this:
Step 9 - Point the Ball in a Random Direction
Make the ball start by pointing down-right, using a random angle between 105 and 165 degrees.
Step 10 - Move and Bounce Forever
Use a forever loop to keep the ball moving. Let it bounce whenever it hits the edge.
Here’s what it should look like:
Step 11 - Stop When the Ball Falls to the Bottom
The game ends when the ball hits the bottom of the stage. You can check if its Y position is less than -160 to stop the program.
Now the ball stops if it falls to the bottom:
Step 12 - Make the Ball Bounce Off the Board
If the ball touches the board, it should bounce upward. The math is simple:
- If the direction is positive (e.g., 160°), the rebound is 180 - direction (e.g., 20°).
- If the direction is negative (e.g., -135°), the rebound is -180 - direction (e.g., -45°).
Therefore, here is the code for the direction calculation:
Now the ball correctly bounces off the board:
Step 13 - Count the Saves
To make it fun, let’s keep a score for how many times the ball has been saved by the board.
- Create a variable called “counter”.
- Set it to 0 at the start.
- Increase it by 1 every time the ball touches the board.
- Display it on the stage:
Now you’ll see the score on the top right:
Step 14 - Hide the Camera Video (Optional)
If you prefer, you can hide the camera video by making it 100% transparent, then you can use a regular backdrop instead.
Step 15 - Fix a Ball Rebound Bug
If you test the game for a while, you will find a small issue. Sometimes the ball would get stuck on the board like this:
Try to fix this issue yourself before continuing.
The problem is that when the ball rebounds off the board and moves up, it might touch the board again in the next update, so it will rebound towards the bottom again.To fix this, only allow a rebound if the ball is moving downward. That means:
- Its direction is between 90° to 180° or -90° to -180°.
- In other words, if abs(direction) > 90, then allow rebound.
Here is the final result:
Additional Challenges
You can now customize and improve your game! Here are some fun ideas to try:
- Increase difficulty: Make the ball move faster after each bounce.
- Smarter bouncing: Adjust the ball’s rebound direction based on how the board is moving.
- Add obstacles: Include some targets that the player must hit with the ball to win the game.
-
info-creaticode