Tightrope
Tightrope
org/en/projects/tightrope/print
Projects
Tightrope
Tilt your Sense HAT to guide a character along a path.
Step 1 Introduction:
In this project you will create a game in which you have to tilt your Sense HAT to guide a character along a path. If you
fall o� the path, you have to start again from the beginning!
If you need to print this project, please use the Printer friendly version (https://projects.raspberrypi.org/en/proje
cts/tightrope/print).
1 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
Introduction:
In this project, children will learn about the Sense HAT orientation sensor by creating a line-follownig game. The
player tilts the Sense HAT to move the character along a path. Deviating from the path sends the player back to the
start!
Online Resources
This project uses Python 3. We recommend using Trinket (https://trinket.io/) to write Python online. This project
contains the following Trinkets:
O�ine Resources
• tightrope/main.py
• tightrope/snippets.py
You can also �nd a completed version of this project in the ‘Volunteer Resources’ section, which contains:
• tightrope-�nished/main.py
• tightrope-�nished/snippets.py
(All of the resources above are also downloadable as project and volunteer .zip �les.)
Learning Objectives
This project covers elements from the following strands of the Raspberry Pi Digital Making Curriculum (http://rp
f.io/curriculum):
Challenges
2 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
Project materials
Project resources
3 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
First let’s draw the path that your character must follow.
The code to set up the Sense HAT has been included for you.
• Let’s start by creating variables to store the colours you want to use. Remember that to set the colour of an
individual LED, you need to say how much red, green and blue it should have.
To create yellow, you’ll need maximum red and green, and no blue:
(If you prefer, you can go to jumpto.cc/colours (http://jumpto.cc/colours) and choose any colour you like!
• You’ll also need black pixels (or any colour you like) around the path.
• To draw your path, you �rst need to create a list containing the colour of each pixel.
4 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
To save typing, you can copy the code from snippets.py in your project.
• Next, you need to call set_pixels to display your path image on the Sense HAT.
• Click ‘Run’ to test your code. You should see a yellow pixel in the places that you’ve used your y variable, and no
colour in the places that you’ve used x.
5 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
Make sure that your path starts at the top-left of the display.
6 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
• First, create another colour variable for your character. Here’s how to create blue:
• Next you need to create variables to store your character’s x and y position. To start with, we’ll set these both to 0,
which is the top-left of the Sense HAT.
• To display your character, use set_pixel. You need to tell set_pixel the x and y position of the pixel to set, as
well as the colour.
• Test your code, and you should now see your character in the top-left of the screen.
7 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
8 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
You’ll be tilting the Sense HAT to move your character. Let’s start by �nding out the orientation (the position) of your
Sense HAT.
• The Sense HAT can detect its roll, pitch and yaw.
• Try dragging the Sense HAT to change its roll, pitch and yaw values to see how it moves.
Press the reset button to put the Sense HAT back to the starting position when you’ve �nished testing.
• We only need the pitch and the roll for this project, so add 2 lines of code to get these values from the Sense HAT.
9 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
• Run your code to test it, and change the pitch of the Sense HAT to tilt it to the right. You’ll notice that the printed
pitch value doesn’t change!
• The problem is that you are only getting and printing the pitch and roll once.
To do this repeatedly, you’ll �rst need to indent all of your code for setting the pixels, as well as getting and
printing the pitch and roll values.
• You can then add while True: above the indented code to run it forever.
• Test your code again, and this time you should see the printed pitch value change.
10 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
11 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
Now let’s move your character when the Sense HAT is tilted.
• Let’s move your character to the right if the Sense HAT’s pitch is between 270 and 315 degrees.
• Add this code to change the character’s x position if the pitch is between 270 and 315:
Make sure that this code is indented, so that the character moves repeatedly if the Sense HAT is tilted.
• Tilt your Sense HAT so that the pitch is between 270 and 315 degrees. You should see that your character
moves to the right, but keeps going o� the display!
You will also see an error, because the character’s x position goes above 7, which is not a valid position on the
display.
12 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
• To �x this, you only want to move your character to the right if its current position is less than 7.
• Test your improved code, and you should now see that your character moves until it gets to the right side of the
display.
• We also want to move your character to the left when the pitch is between 45 and 90 degrees.
• Add this code to move your character to the left if the pitch is between 45 and 90, but only if the character
isn’t already at the far left of the display.
• Test your code to make sure that you can tilt your character back to the left.
• Next, let’s add code to change your character’s y-position, moving it down when the roll value is between 45
and 90.
13 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
• test this code to see if you can tilt the Sense HAT to move your character down.
• If you want to slow your game down, you can add short sleep at the end of your while True: loop.
14 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
Can you add code to move your character up (by changing your chary variable by -1) when the roll is between
270 and 315?
The code you’ll need to add will be very similar to the code for moving your character to the left.
15 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
Let’s put your character back to the start when it falls o� the path.
• You may have noticed that nothing happens when the character falls o� the path.
• To �x this, we’re going to send the character back to the start if they’re standing on a black pixel.
Let’s start by getting the colour of the pixel the character has moved to.
• If the colour of the current pixel is black, then send the character back to the start.
• Test your code and you should see your character move back to the start if they fall o� the path.
16 of 17 8/30/2023, 9:43 PM
Tightrope https://projects.raspberrypi.org/en/projects/tightrope/print
Ask some friends to test your game. Did they �nd it too easy or too hard? If so, you could make some changes:
17 of 17 8/30/2023, 9:43 PM