AI - Bouncing Ball with Motion Sensor (Difficulty: 3)
CreatiCode last edited by info-creaticode
Motion sensors can detect the motion of objects, and such motion can be used as user input for our games or applications. Users no longer need to use a mouse or keyboard for their inputs, which has several key benefits:
- For disabled users, they may not be able to use their hands for inputs, so they need to have alternative input methods
- Some patients need to do more exercise for health or rehabilitation reasons, so having them play such a game is a fun way to make them move more.
- Motion sensor games only require a camera to play, so there is no need for the mouse or keyboard.
In this tutorial, you will build a game where players move a board by swinging their bodies to make sure a ball does not fall below the board.
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
Next, create a sprite for the bouncing board. You can remove the dog sprite, and paint a rectangle in the costume of the “Empty1” sprite. Also please rename this sprite as “Board”.
Please make sure it is symmetric left and right.
When the game starts, the board should be moved to the bottom center of the stage:
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:
Next, we want to use a new variable “board speed” to represent how fast the board should move. We will link the speed of the board with the value of “motion”, so that if the player is making big moves, then the board will move faster. For example, we can set the board speed to be 1/8 of the motion value. We will also ignore small motions below 20 by setting the board speed to 0.
You can verify that the board speed is 0 when you are not moving:
Now we can change the board’s x position using “board speed” and “direction”. If the motion direction is pointing to the right, its value should be between 0 and 180; otherwise, its value should be between 0 and -179.
Therefore, if “direction” is positive, then we move the board to the right; otherwise, we move the board to the left.
Now the board should follow your movement pretty well.
Currently, the board might go into the edge on the left or right side. We should keep it visible within the stage. To do that, we can add some limits to its X position. For example, after moving the board, we move the board back if its X position is more than 150 or less than -150.
Now the board will stay within the stage:
Now we will start to work on the ball sprite. Please add a new “Beachball” sprite, set its size to 50, and make it go to the top of the stage when the program starts:
Next, let’s make the ball point in a random direction. You can pick a range so that the ball points to the bottom right, like between 105 and 165 degrees:
Now let’s use a forever loop to make the ball move and bounce forever and rebound whenever it touches any edge.
This is what you should have:
The player will fail when the ball touches the bottom. We can check if the Y position of the ball is lower than a threshold, such as -160.
Now the ball will stop when it touches the bottom edge:
When the ball touches the board, we need to make it rebound in the opposite direction. A bit of math is needed here.
When the direction is greater than 0, the ball must be moving towards the bottom right. For example, if the ball’s direction is 160 degrees, then the new direction after rebound should be 20 degrees. If the ball’s direction is 120 degrees, then the new direction should be 60 degrees. Therefore, the new direction is always 180 minus the old direction.
Similarly, you will find that if the old direction is negative, then the new direction must be -180 minus the old direction. For example, it may go from -135 degrees to -45 degrees.
Therefore, here is the code for the direction calculation:
And now the ball will bounce off the board:
To make it fun, let’s print out how many times the ball has been saved by the board. We can use a variable named “counter” to keep track of the count. It should start as 0, then increase by 1 each time the ball touches the board. To make it easy to see, we can print it out on each rebound:
Now we get the counter on the top right:
If you prefer, you can hide the camera video by making it 100% transparent, then you can use a better backdrop on the stage.
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:
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 avoid this, we can add a condition to the board rebound logic: when the ball touches the board, we would only rebound the ball if its direction is downwards. When the ball is going downwards, its direction has to be between -90 and -180, or between 90 and 180. We can just check if its absolute value is more than 90.
Here is the final result:
The current game logic is very simple. There are many ways you can make it more fun. Here are some examples:
Speed up the ball: You can make the ball go faster each time it rebounds off the board
Change ball rebound direction: You can turn the ball’s direction a bit when it touches the board. For example, if the board is moving to the right when it touches the ball, we can turn the ball to the right a bit. This way, the player can control which way the ball goes.
Hitting Obstacles: You can add some obstacles in the air, and the player has to hit all of them with the ball to pass a level.