Skip to main content

A Wireless Controller for a Scratch Game

Submitted by DaveBakker on Tue, 04/10/2018 - 02:36

Let's create a game in Scratch that is controlled by a wireless controller using the PocketLab accelerometer. PocketLab can connect directly and wirelessly to Scratch, and can send sensor data in real time. This game uses the PocketLab accelerometer to move sprites around the screen.

This program is for intermediate or advanced Scratch users, and if you would like a simpler starting point to get started with connecting Scratch to the outside world through PocketLab, you can start here:

Using Scratch with PocketLab

Programming Sensors in Scratch

Let's Jump Right In!

To run Scratch and load the PocketLab extensions, click on this link: http://scratchx.org/?url=https://roozeboom.github.io/pocketlab-scratchx/pocketlab_extension.js#scratch

This game can be downloaded from a link on GitHub here: https://github.com/ThePocketLab/ScratchX/blob/gh-pages/SpaceBlaster.sbx

(Save the file, and remember where you saved it)

This is a ‘space blaster’ game, where you control the spaceship by tilting the PocketLab left, right, up, and down. It is using the same principle as the first code we wrote above, which uses the x and y axis accelerometer data to tell the sprite to move when you tilt the pocketlab. It also uses the z axes in a unique way to ‘shoot’ a projectile - if you flip the PocketLab over quickly, the z axes changes polarity from + to -, the code captures this and ‘shoots’ a ball. You have to be quick so that it doesn’t register on the x and y axes and make the spaceship move. It is a fun and challenging mechanic.

PocketLab makes science easy and fun

To load and run the game: In the ScratchX window (after clicking “Continue”), go to File/Load Project, and load the Space Blaster game from where you saved it. Every time you reload the ScratchX program it will cause the PocketLab App window to refresh, and you may have to connect to your PocketLab again, it should just be one click in the PocketLab App window.

Make sure the PocketLab is displaying the accelerometer data (x, y, and z axes). The default opening screen for PocketLab is the accelerometer set to g as units and a data rate of 10 points/sec. It is good to verify this, it will of course greatly affect the program.

You can go to ScratchX and click on the flag to start the program. You should be able to use the PocketLab with the logo side facing up, and control the spaceship on the screen by tilting the PocketLab.

The idea of the game is to avoid the planets! If you hit one, your spaceship will die. Your spaceship can “shoot” balls at the planets. You can do this by quickly flipping over the PocketLab upside down and then right back to right-side up. It has to be pretty fast, or it will trigger the y axes movement! This will shoot a ball and return to controlling the spaceship. (there is a video of this game here: <video link>)

Note: As you may know if you have some programming experience, different computers have different processing power. This program is optimized to run well on an OSX Mac laptop. If you have more or less processing power, the program may be sluggish or hard to control. As you go through the program, look for settings that you can adjust, for example glide speed. It is good to change these anyway and see what happens to the program - that is a very good way to learn how Scratch works. You can also adjust the data rate of your PocketLab on the PocketLab app window where it says “Freq”. Try various settings - this is part of optimizing machine control.

Let’s examine the program

The main program controls the spaceship. Click on the Spaceship sprite:

Scratch spaceship sprite

And in the programming window you will see this code, which is moving the sprite according to the direction that you tilt the PocketLab. The glide function is reading the accelerometer, and using it to move the sprite left, right, up, and down on the stage (The beginner section explains how this works). There is one addition, and that is the ‘abs’ operator (green programming block) that makes sure the acceleration value falls within -1.1 to 1.1. It will ignore values outside of this range, because they would make the sprite move too fast.

Wireless accelerometer control code

The rest of the code is just orienting the sprite, setting size, and making sure you don’t run into the asteroids (which are the “Earth” sprites). If an asteroid does hit you, there is a sound and an animation and the game goes back to the beginning and resets your score to zero (see the score variable in the orange data block).

It’s actually pretty simple - the only code that touches the PocketLab data in this entire program is the one ‘glide’ function! We will suggest this many times: change the specified amounts in the blocks and see that happens. What happens if the glide amount is faster or slower? What happens if the limit on the accelerometer values is changed? What happens if the glide x and y sizes are changed?

Asteroids!

There are two ‘Earth’ pixels used as asteroids:

Scratch Earth pixels as asteroids

The code for these pixels is below. They start at random times, have a random color, start at a random x location along the top of the stage, and travel in a random direction. If they touch a ball that you shoot, they shrink and add one point to the ‘score’ variable, and make a ‘pop’ sound. To make the game more challenging as time goes on, they move by a number of steps that increments with the score, meaning the higher your score, the faster they move!

Scratch asteroid code

Using the z-axis to “shoot”

If you flip the PocketLab upside down quickly, it will ‘shoot’ a ball that expands and hopefully hits an asteroid and adds a point to your score. This program uses the z-axis data to shoot, and this is the code in the ball sprite. The ball remains hidden and follows the spaceship (see the blue ‘go to’ function), until  z goes below 0g, which means you have turned the PocketLab upside down. Then the ball sprite ‘shoots’ forward hopefully touching one of the asteroids (the two Earth sprites). If it touches, it adds one to the score variable.

Scratch z axis shooter code

Space Blaster game conclusion

This programming example is a lot of fun, and you can see there is a lot you can do just by reading the x, y, and z data from the PocketLab accelerometer. Go through the program and try changing some settings and see how it affects the program. You can always reload the program if something is broken.

Image
Wireless Controller Game for Scratch
Subject