codelab@teachingkidstocode.io

# Play Skeeball with Sphero

Whether it’s to learn or have fun, Sphero is always ready to go! In today’s edition of Thursday Learnday, we will learn to play Skeeball with Sphero! Skeeball is an application of the spring-loaded or wind-up toy cars on Sphero. When the program is run, Sphero stores the G-force generated by the lateral motion of the arm holding it. This motion can be repeated to accumulate the G-force. Simply, where the toy car travels farther the longer you wind it, Sphero also travels farther the more G-force it accumulates.

The objective of Skeeball is to have the player accumulates enough G-force on Sphero for it to reach the target spot in the play area. Skeeball also showcases the hackability of Sphero 2.0 and it’s education edition, Sphero SPRK+. Written fully in JavaScript, Skeeball! takes control and demonstrates several sensors onboard Sphero including its gyroscope, motors and LEDs.

#### Game Settings

The first 4 lines of the code sets the followings:

1. If set as ‘true’, instructs Sphero to return back to the starting point. If set ‘false’, Sphero stops at the end if its roll.
`var DO_RETURN_TO_USER = true; `

2. Adjust the roll angle to lock Sphero and its accumulated G-force.
`var TIP_ROLL_THRESH = 60.0; `

3. Adjust the speed of which Sphero rolls.Minimum speed: 0 (Sphero won’t roll); Maximum speed: 255
`var SPEED = 100.0; `

4. Sets the roll duration for each shake.
`var DURATION = 0.5;`

#### How to Play?

1. Grab Sphero in the palm of your hand firmly and hold it in front of your chest. Your palm should be facing down.
2. Ensure that Sphero’s blue tail light is pointing at your chest.
3. In one lateral motion, move your hand away from your chest, abruptly stopping at the end of the motion. Sphero should blink blue, and a sends a ding sound through the speaker.
4. Repeat the motion several times to accumulate the G-force.
5. Once you’re satisfied that you’ve collected enough G-force, set Sphero at the start line, ensuring that the blue tail light is facing you.
6. Tilt Sphero to the right to lock in the accumulated G-force. Sphero should flash red and start a countdown. You have 3 seconds to aim before Sphero starts rolling.

#### The Full Code

Use the following code to play Skeeball with Sphero! It’s easy. Copy and paste into your Sphero Edu app!

```var DO_RETURN_TO_USER = true; // true to have robot return, false to not return
var TIP_ROLL_THRESH = 60.0 // adjust roll angle to lock in distance
var SPEED = 100.0; // adjust rolling speed from 0-255 (default = 100)
var DURATION = 0.5; // duration of roll for each shake (default = 0.5 seconds)
var currentState = null;
var shakes = 0;
async function chargeOnShake() {
playSound(Sound.Effects.BeepSingle);
shakes += 1;
setMainLed({
r: 0,
g: 0,
b: 255
});
await delay(0.1);
}
async function stateCharge() {
setMainLed({
r: 0,
g: 0,
b: 0
});
if (Math.abs(getOrientation().roll) &gt; TIP_ROLL_THRESH) {
playSound(Sound.Effects.BeepDouble);
}
}
async function doNothing() {
// Does nothing.
}
registerEvent(EventType.onCollision, doNothing);
setStabilization(false);
await delay(0.75);
setMainLed({
r: 255,
g: 0,
b: 0
});
await delay(1.0);
currentState = stateSet;
};
async function stateSet() {
resetAim();
setMainLed({
r: 255,
g: 255,
b: 0
});
speak("Set!");
await delay(1.0);
currentState = stateGo;
};
async function stateGo() {
var i = 0;
setStabilization(true);
setMainLed({
r: 0,
g: 255,
b: 0
});
speak("Go!");
await delay(1);
setMainLed({
r: 255,
g: 255,
b: 255
});
for (i = 0; i &lt; shakes; ++i) {
playSound(Sound.Mechanical.EngineRev);
await roll(0, SPEED);
await delay(DURATION);
}
stopRoll(0);
await delay(2.0);
currentState = statePause;
};
async function statePause() {
stopRoll(180);
await delay(1.0);
if (DO_RETURN_TO_USER) {
currentState = stateReturn;
} else {
shakes = 0;
await prepForCharge();
}
};
async function stateReturn() {
var i = 0;
for (i=0; i&lt;shakes; ++i) {
await roll(180, SPEED);
await delay(DURATION);
}
stopRoll(180);
await prepForCharge();
};
async function prepForCharge() {
currentState = stateCharge;
setBackLed(128);
shakes = 0;
speak('Shake me to power up for skeeball.');
await delay(3.0);
registerEvent(EventType.onCollision, chargeOnShake);
};
async function startProgram() {
setStabilization(false);
await prepForCharge();
while (true) {
await currentState();
await delay(0.033);
}
};
```