In this project, we'll finish up our accelerometer shake game by randomizing the subsequent command to the user and verifying that the requested action was taken.

You'll need to wire up the Accelerometer as done in previous projects. Here is what your breadboard should look like once the accelerometer is properly wired:

Here is what the code for this project should look like at this point (with new changes highlighted with arrows):

from rstem.accel import Accel from rstem.sound import Speech import random import time ready_tts = Speech("Ready to play?") tilt_left_tts = Speech("Tilt left!") *tilt_right_tts = Speech("Tilt right!") *tilt_up_tts = Speech("Tilt up!") *shake_me_tts = Speech("Shake me!") no_action_tts = Speech("No action!") detect_tts = Speech("I detected:") wrong_tts = Speech("Oops!") correct_tts = Speech("Correct!") try_again_tts = Speech("Try again another time!") *actions_tts = [no_action_tts, tilt_left_tts, tilt_right_tts, tilt_up_tts, shake_me_tts] accel = Accel() NOACTION = 0 LEFT = 1 RIGHT = 2 UP = 3 SHAKE = 4 # Ready to play? SCORE_NEEDED = 10 period = 300 ready_tts.play().wait() while True: * request = random.randint(1,4) action = NOACTION actions_tts[request].play().wait() * tilt_left_score = 0 * tilt_right_score = 0 * tilt_up_score = 0 shake_me_score = 0 for i in range(period): x, y, z = accel.forces() if x > 0.75: tilt_left_score += 1 * if x < -0.75: * tilt_right_score += 1 * if y > 0.75: * tilt_up_score += 1 * if (z < 0.7 or z > 1.3) and (-0.5 < x < 0.5) and (-0.5 < y < 0.5): * shake_me_score += 1 if tilt_left_score > SCORE_NEEDED: action = LEFT break * elif tilt_right_score > SCORE_NEEDED: * action = RIGHT * break * elif tilt_up_score > SCORE_NEEDED: * action = UP * break * elif shake_me_score > SCORE_NEEDED: * action = SHAKE * break time.sleep(0.01) if request == action: # Correct action performed! correct_tts.play().wait() else: # Timeout - let user know what we detected wrong_tts.play().wait() detect_tts.play().wait() actions_tts[action].play().wait() break * period = int(period * 0.95) time.sleep(0.5) try_again_tts.play().wait()

Now, let's take a look at what our code is doing...

The bulk of the changes above are to add 3 new tilt methods: tilt right, tilt up, and "shake me". After adding these, we'll now have a real game! For each new tilt type, we've duplicated the code that handles the left tilt, and changed the variable names and detection to handle right tilt, tilt up, and "shake me".

The "shake me" detection is a little different, and needs some explanation. To correctly do a "shake" you must keep the CREATOR Kit level, and shake up and down. This is necessary — if we allowed the kit to be tilted while it was shaken, we wouldn't be able to tell the difference between a tilt and a shake.

So, to test for "shaking" we test to make sure that `x` is small (it's not tilted left or right) and that `y` is small (it's not tiled up or down). Then we check that `z` is far away from 1 (which is its normal position). Remember: when the CREATOR Kit is sitting flat on a table, `z` will be 1 — it means that the force of the earth is pulling it down. When the kit is moved up quickly `z` will go a good bit higher than 1. When the kit is moved back down quickly `z` will go a good bit lower than 1.

Besides testing for the new tilts, there are some other minor changes:

- On Line 34, we no longer always request `LEFT` tilt. Instead, we now randomly choose a tilt, from 1 to 4, which corresponds to `LEFT`, `RIGHT`, `UP`, or `SHAKE`.

- On Line 58, we change the period, after each time the user performs the correct action. This will make the game go faster and faster until it's crazy hard to respond with the correct action in time.

Now that the game is complete, give it a try and see how well you can do!

1. Can you make the game harder? Perhaps change all the actions to single word, like "LEFT" and "SHAKE" to speed up the command and having the time-out be shorter.

2. Can you add a "tilt down" action that is the opposite of tilt up?

3. Can you keep a count of how many correct actions were performed correctly, and print out the number of correct actions once the game is over?

4. Instead of printing the number of correct actions (from the last question), can you have the code say the number out loud? (Hint: you may need to create a new `Speech()` for each number you want to say — so you might need to limit the answer to numbers under, say, 10)

5. Can you add a sideways shake action (that is, shake left and right, while keeping the kit level)?