Apply your knowledge to build something amazing!
:information_source: Project Overview :emoji: Racing Turtles Challenge
- Difficulty: ⭐⭐⭐ (3 out of 5 stars)
- Time to Complete: 45-60 minutes
- Skills Practiced:
- Python Turtle Graphics
- Variables and loops
- Random numbers
- Conditional statements (if/else)
- Lists and indexing
- What You'll Build: An exciting turtle race game where colorful turtles compete to reach the finish line! Watch as they move at random speeds - who will win? :emoji:
📍 Part 1: Basic Race
Step 1: Setup & Import → Step 2: Draw Finish Line → Step 3: Create Racing Turtles → Step 4: Race Logic → Success! 🎉
📍 Part 2: Advanced Race
Step 1: Multiple Turtles → Step 2: Loop Setup → Step 3: Dynamic Racing → Epic 7-Turtle Race! 🏆
Go to EduBlocks page and login with your Google account: https://app.edublocks.org/login
Everytime you want to start a new project, click on the "Editor" icon. To resume your project, click on the "Projects" icon.
:warning: Important! Save your work frequently! Click the "Save" button every 5-10 minutes to avoid losing your amazing race game. EduBlocks will remind you, but it's good to develop this habit early! :emoji:
Using the Python Turtle Graphics Library, we will create a race track and see 2 Turtles (Green and Red), racing each other to the finish line!
:bulb: Fun Fact! :emoji:
Did you know? Python's Turtle Graphics is based on the Logo programming language from the 1960s! It was designed to help young programmers learn coding by drawing shapes and patterns. Today, you're continuing that awesome tradition!
First, we need to bring in the special tools (modules) that Python needs for our race:
:bulb: Block Location
Find these import blocks in the Imports category on the left sidebar of EduBlocks!
Now let's create the finish line that our turtles will race towards:
What's happening? The line turtle draws a vertical line by turning left (facing up) and moving forward. This creates our finish line!
Time to create our first racer!
:warning: Common Mistake Alert! Make sure to use pen up before moving the turtle to its starting position, then pen down after. Otherwise, you'll see unwanted lines on your screen!
Now for our second racer!
Pro tip: Notice the green turtle starts at y=40 while red starts at y=60. This puts them on different racing lanes!
Before continuing, make sure you have:
Let's add some excitement with a race announcement!
a. Using "# your own code" block, write the following:
line.write(" Ready? Go!")
This will allow the line Turtle to say something.
b. Set the time to sleep 1 second. This just means the program will pause for 1 second and then resume.
:bulb: Understanding "# your own code"
The "# your own code" block is super powerful! It lets you type Python code that isn't available as a ready-made block. You'll find it in the Statements category. Think of it as your secret weapon for adding custom features!
This is where the magic happens! We'll use a loop to keep the race running until someone wins.
a. Create a variable called run and set the value to True
b. While run:
i. Set the red Turtle to move forward by a random integer from 1 to 3
ii. Set the green Turtle to move forward by a random integer from 1 to 3
iii. Set the time to sleep 0.1 second. This is to set the speed of the race. The lower the sleep time, the faster the 2 Turtles run.
iv. If the x coordinate of red Turtle is greater or equal to -10:
red.write(" I win!")
v. Else, if the x coordinate of green Turtle is greater or equal to -10:
green.write(" I win!")
:warning: Debugging Tip If your turtles aren't moving, check:
- Did you set run = True before the while loop?
- Are you using random.randint(1,3) correctly?
- Is your while loop properly indented?
Remember: In EduBlocks, proper block connections = proper indentation!
:bulb: Understanding Coordinates
The finish line is at x = 0. The turtles start at x = -100. When a turtle's x coordinate reaches -10 or higher, they've crossed the finish line! That's why we check if
xcor() >= -10
.
Your basic race is complete when:
Want to make your race even cooler? Try these ideas:
:information_source: Level Up! :rocket: Ready for a challenge? In Part 2, we'll use the power of loops and lists to create an EPIC 7-turtle race! This is where your Python skills really shine!
For the part 2 version of the project, recreate the program so that this time it uses loops to create multiple racing Turtles.
We will have 7 colours for each of the 7 Turtles, following the colour of "red", "green", "blue", "purple", "pink", "orange", "black".
Import these modules: a. Turtle Graphics module b. Time module c. Random module
For the line Turtle, we will have to use a longer line that extends from (0, -100) to (0, 100).
We need two important lists:
:bulb: Understanding Lists
Think of lists like a container that can hold multiple items. The colours list holds color names, while the turtles list will hold actual turtle objects!
a. This turtles list will be used to append 7 Turtle() to it through a for loop with the range function in the length of the colours list.
b. By using the "# your own code" block, we can then give commands to each of those Turtles in the turtles list with the turtles[i] syntax.
For example:
turtles[i].shape("turtle")
turtles[i].color(colours[i])
turtles[i].penup()
turtles[i].goto(-100, 60 - i*20)
turtles[i].pendown()
The i is the index number of the for loop with the range function.
:warning: Advanced Concept Alert! The formula
60 - i*20
creates evenly spaced starting positions:
- Turtle 0: y = 60 - 0*20 = 60
- Turtle 1: y = 60 - 1*20 = 40
- Turtle 2: y = 60 - 2*20 = 20
- And so on...
This creates a nice vertical spacing of 20 pixels between each turtle!
After announcing the race begins, while race is running:
a. For x in turtles list:
i. x.forward(random.randint(1,3))
ii. Set the sleep time. The shorter the sleep time the faster the turtles run.
iii. if x.xcor() is greater than -10:
:bulb: Pro Racing Tip
Try setting sleep time to 0.01 for a super-fast, exciting race! The turtles will zoom across the screen!
Your advanced race is complete when:
Having trouble? Here are common issues and solutions:
"Turtle not defined" error
Turtles not moving
All turtles the same color
Race never ends
Blocks won't connect
Take your 7-turtle race to the next level:
Make sure your Racing Turtles project includes:
:bulb: Final Check
Run your program 3 times to make sure:
- Different turtles win each time (randomness works!)
- No error messages appear
- The winner announcement displays correctly
In this project, you've practiced real Python programming concepts:
When you have completed your "Racing Turtles" project, submit it using the link below: Submit Your Project Here
:warning: Before You Submit! Test your program thoroughly:
- Run the advanced version at least 3 times
- Check that all 7 turtles appear and race
- Verify the winner announces correctly
- Make sure there are no error messages
Note: You need to submit 1 project link for this project - the advanced version (Part 2) which incorporates all the concepts from Part 1.
You've created an awesome turtle racing game! You've learned about loops, lists, randomness, and game logic - all fundamental concepts in programming. Keep experimenting and making it your own! :emoji::trophy: