Rabbits Won’t Win This Race – Create Your Own Racing Turtle Game with Python & Raspberry Pi!
In the classic tortoise and the hare story, everybody learns that the slow and steady turtle wins the race. Now with the help of Raspberry Pi and Python, you can pit several racing turtles against one another in your own fun electronic game!
Our team here at Vilros has had so much fun playing this game! Here’s how it works: you’re going to use Loops to draw the race track and get turtles to move a number of random steps. With 3 or 4 people playing, you can see whose turtle will go the furthest after 10 rounds.
Will your turtle be the winner?
- Parts You Will Need
- Python 3 (Recommended: Trinket for writing code online)
- Your computer – connected to the internet
- Build Your Race Track
Your first task is to create the race track for your turtles.
Mostly, you’d want to be using the “write” function, and filling in numbers to establish markings. The code will be rather repetitive, with just the numbers changing. That’s why the easiest way to do this will be to use a “for” loop with Python!
You’ll also want to create some track markings. For this step you will use vertical lines to make different tracks.
You can find more details on the tutorial here.
- Get Your Turtles Ready
Now, that the track is ready, it’s time to bring on the turtles!
Using “ada = Turtle()” you’ll create your first turtle and also specify its color. Then, you’ll use the “randint” function from Pyton’s “random” library in order to make your turtle move a random number of steps. Every time you activate your turtle, it will move a random number between 1-5 steps.
Now, it’s time to add more turtles to the race! Make sure that each turtle you add is a different color so it is clear which turtle is which. You can find lots of color options at http://jumpto.cc/colours.
For more details on this step, check out this section of the tutorial.
Note: every turtle will need to be in the same “for” loop so that they each move on every round.
- Ready, Set, Go!
Now, it’s time to race! This game is best played with at least a few friends.
How many turtles will you have on your track?
- Take Your Game to the Next Level
Now that you’ve got the game going, it’s time to take things to the next level!
One fun addition is to add a twirl move. If you add the code “ada.right (72)” that will make your turtle turn 72 degrees. Do it 5 times, and your turtle will make one complete spin!
Another way to make your game more interesting is to use dashed track lines instead of solid lines. Now it will look more like a real race track! You’ll want to use a code that draws a straight line and alternate picking up and putting down the “pen”. For example, try: “for, forward(), penup(), and pendown()”
Congratulations – you’ve now got your very own turtle game that can be hours of fun for you and your friends! If you want more details about this project, you can find all the information on this tutorial.
All of us here at Vilros can’t wait to hear more about your turtle races! Let us know what you think about this game, and be sure to come back to our Vilros Projects blog for more Raspberry Pi and Arduino project inspiration.