Use the Finch as a game controller!
This project is aligned with Common Core math standards that the coordinate grid (5.G.A and 6.NS.C) and proportions (6.RP.A and 7.RP.A). At the high school level, students can create custom blocks to represent functions relating the accelerometer value to a screen position (HSF.IF.B and HSF.BF.A).
In this project, you will use the Finch as a game controller! You will create a version of Pong. A ball will fall from the top of the screen, and you will try to catch it with a sprite that you control with the Finch.
To start, you will need two sprites, a ball and a paddle (or a basket, if you prefer). The sprites shown are from Scratch, but you can draw these sprites in Snap!.
In this activity, you will use the y-acceleration of the Finch to move the paddle left and right along the bottom of the stage.
Start by declaring a variable named yAccel. Use a forever loop to continually set the value of this variable equal to the y-acceleration of the Finch. The code here is shown in Scratch but would be similar in Snap!.
Next, declare another variable named xPaddlePosition. You will use this variable to determine the x-position of the paddle on the stage. As you tilt the Finch, the value of the y-acceleration is between -1 and 1. What number will you need to put in the blank below to make the paddle move from -240 to 240?
Place a go to block (Motion menu) at the bottom of your forever loop. Use this block to continually set the x-position of the paddle sprite to xPaddlePosition. The y-coordinate for the sprite can be -180; this is the bottom of the stage.
Run your script. You will notice that when you tilt the Finch to the left, the paddle moves to the right. When you tilt the Finch to the right, the paddle moves to the left. How can you adjust your code to fix this problem?
Now that your paddle is working, is it time to write some scripts to control the ball. Let’s start by making the ball fall down the screen when you press the ‘a’ key. Start your script by placing the ball at (0,180). This is the center of the top of the stage.
You can make the ball move to the bottom of the screen in a series of small steps by using a repeat loop to change the y-value of the ball. A negative value in the change y by block will make the ball move down. The code below moves the ball down by 1 each time the loop is repeated. What number should you place in the repeat block to make the ball move downward until it reaches the bottom of the screen?
You have probably noticed that the ball is falling very slowly. How can you modify the change y by block to make it fall faster? Remember, when you change the value in this block, you will need to change the number of times that the loop repeats so that the script ends when the ball gets to the bottom of the stage. Otherwise, you won’t be able to press the ‘a’ button again until the script stops running. Work on your script until you are happy with how fast the ball falls and the script stops running when the ball gets to the bottom.
Right now, the ball always falls down the center of the stage. This isn’t very interesting. You can use the random block to vary where the ball starts. This block is on the Operators menu. Click on it several times. You will see that it doesn’t show the same value every time you click on it. Instead, it chooses a random number between 1 and 10.
You can use the random block inside the go to block at the beginning of your ball script. What numbers should you put in this block so that the ball can start at any x-value?
There is only one more step! After the ball reaches the bottom of the screen, you want to check if the user caught it with the paddle. If the user caught it, they should get a point. Start by creating a variable called score.
You can use the touching? block to see if the ball is touching the paddle. This block is true when the ball is touching the paddle and false otherwise.
You can use an if block to check if the paddle is touching the ball. The code below will increase the user’s score if the paddle is touching the ball. Put this code at the bottom of your ball script to keep score. You will learn more about if statements in the next Finch lesson; this is just a preview!
You have created a basic Pong game. Now it is time to make it your own! What features do you want to add? You can make the ball fall multiple times or add sounds and graphics. You can even make the ball and/or paddle move both horizontally and vertically. Use your imagination!
Make Your Own Game!
You can also use the Finch as a game controller for many other kinds of games! Try making a maze game, Finch Pac Man, or create your own orignal game. Share your game with us via email (firstname.lastname@example.org) or twitter (@birdbraintech).