Before you start
Make sure you have the necessary hardware and software. A Raspberry Pi with the Raspian operating system and the Chip Snips red Raspberry Pi bridge with set of sensors are required. The following links will help you set everything up.
Run the Python Driver
Open Python 2. Click the Raspberry in the upper-left corner of your Desktop. Go to "Programming --> Python 2"
Warning: Do not open Python 3. This script was written for Python 2.
A Python "Shell" will open. In the menu bar, select "File --> Open" and navigate to "/home/pi/Desktop/raspberrypibridge/Bridge_1" and open the Python file "scratch_I2C_driver.py"
Once open, you will see the driver program. This program will need to run in the background. To start it, select "Run --> Run Module F5"
When the script runs, you will see the output in the Python Shell window. If Scratch is not open a message will appear letting you know it could not connect and will try again every 10 seconds. You may safely ignore it. Opening Scratch will stop the messages.
Go to the Raspberry in the upper-left corner and select "Programming --> Scratch"
Warning: Do not open "Scratch 2." The sample programs are only compatible with the original Scratch.
Within Scratch, go to "File --> Open..."
Click on the large "Desktop" button and go to the "raspberrypibridge/Bridge_1" directory and open example program named "ChipSnipAnalogInput."
View Analog Signals
Once the "Chip-Snip_Analog_Input" program is open, click on the green flag in the upper right-hand corner of Scratch. This will execute the program.
Choose a sensor and attach it to the free end of the Chip Snip wire module. The small screen in the upper-right hand corner of Scratch will draw a line that represents the analog output signal sent from your sensor. Vary the sensor's input (by applying pressure, light, changing orientation, or whatever causes your sensor's output to change) and watch how the line changes. You will see something like this:
The program's output draws a line that represents the change in your sensor's input on the Y-axis over time on the X-axis.
The orange box in the lower-left-hand corner contains a number that represents the analog signals current magnitude. This number is generated by the little black chip on the red Raspberry Pi Bridge, read by the Python script over the I2C port, and sent to Scratch where the current program displays the value and draws the lines.
Play around with several sensors to get a feel for them.
Program a Game
In Scratch, open the example game. Go to "File --> Open" and select "Helicopter-game-demo." The demonstration game will show a cartoon helicopter with cloudy blue sky and green ground.
Go ahead and click on the green flag in the upper-left-hand corner of Scratch. Your helicopter should wobble a bit as it tries to fall toward the earth, but is repelled by it. Now, vary the sensor's input and see what the helicopter does. (It should move up and down with the amplitude of the analog signal provided by the sensor stimulus you provide.)
Use the right and left arrow keys on your keyboard to move your helicopter. Once you get a good feel for how the helicopter moves, ask yourself:
- Is the helicopter moving too fast up and down or side-to-side?
- Is the sensor control intuitive? If the chopper moves up when your hand moves down, you may want to change it.
Once you've answered these, you'll most likely want to change your program (if not, just do it for fun).
Chopper Control Changes
To change the chopper's response to your controls, you need to understand the program. Let's walk through the major code functions.
All of the code functions begin with a green flag similar to the one you use to start the game. That green flag "hat" block means anything attached underneath it begins to execute when the green flag is clicked and the game starts.
This block reduces the size of the chopper to 25% of the original and sets the variables 'scrollX' and 'height' to 0 and -180. This places the chopper in the lower-left-hand area of the screen to start. Because there are no loops, these 3 blocks execute once at start and never run again (unless you restart the program).
The next two functions control the chopper's direction. Both contain a "forever if" block. This block executes the code within it, top to bottom, constantly. Anytime the program detects the left and right arrow are held down, the scrollX variable that controls the chopper's location on the X axis changes by 5 pixels forward or back.
- Can you change how fast the chopper moves when the arrow keys are pressed?
- Can you change which keys control the direction?
Finally, the chopper's altitude control function. This function is more complex that the others. It has a "forever" block which executes only until you stop the program or your computer stops for any reason. It's first task is to get the value from your Chip Snip sensor and apply it to the Y position of the chopper. The name of the sensor is "adcInput." The value is divided by 2.8 and added to the current height of the chopper. Dividing by 2.8 reduces the amplitude of the sensor input. Feel free to change it and see what happens. Adding this value to the variable "height" means even small changes to your input signal cause the chopper to rise, wherever it may be. This allows the chopper to rise quickly or slowly depending on how great the input signal is.
There are also two "if" block immediately under the "forever" block. These blocks have to handle what to do if the chopper is touching the ground or not. Since the ground is green, we detect whether the chopper is touching that color or not. Touching the ground pushes up on the chopper up by 4 pixels. Not touching the ground causes the chopper to fall by two pixels at a time.
You likely noticed these "if" blocks have an "if" block nested inside them. This makes sure the chopper does not rise over the top of the screen or fall below the bottom of the screen.
Go ahead and make any changes to the chopper's response to inputs.
How can a player finish your game?
Most games have a goal to reach in order to win. This game's goal may be to land safely on a helipad. Create one that is easy to see. Stop the game for a few seconds with a message such as "You made it!" then, start over again.
Any good video game has a number of obstacles and perils to overcome. Add one or more of the following to your game:
- Place towers or trees at different heights as obstacles. If the helicopter collides with an obstacle, display "Game Over" and start again.
- Create a heat-seeking missile that follows the helicopter. If it hits the helicopter, replace the helicopter with an explosion, display "Game Over" and start again.
- Start visible a timer at the beginning of each game. Display time when the helicopter makes it to the end.
- Duplicate the helicopter and turn the game into a two-player race.
- Change the new helicopter into another image for the second player.
- Determine another method to control the second player.