Project: The Python Turtle Race
Project: the Python turtle race. so far, you’ve learned how to customize your
turtle environment, program your turtle to move around the screen, and use loops and conditional statements to improve your code.
00:13 Now it’s time for the most important part of your programming journey, and in this section, you’ll be implementing all that you’ve learned into a single program by creating a fun game that you can play with your friends. Before you begin, here’s what you need to know about the game.
00:27 Firstly, the objective. This is simple. The first turtle home wins. Secondly, how to play. Each player rolls a dice to get a number, the player then moves their turtle by that many steps, the players alternate until one of them wins. Finally, the structure.
Each turtle has a home position that it must reach. And each player uses a die to choose a value at random for their turn. In your program, the die is represented by a list of numbers from
So here, you can see the code is going to be created as a
.py file in Visual Studio Code—but you could use any editor such as IDLE—and then run it using the command
python and then the name of the file you’ve created.
So as you’ve seen previously,
player_one is going to be created using
turtle.Turtle() and then
player_one will be customized—firstly, with a color of
"green", next up, with a shape being
And we’re going to set player one’s pen to be up so it won’t create a line as it moves. It’s going to go to its start position,
-200, on the left of the screen, and
100, which is up from center.
02:29 Next up, setting up homes for the turtles. These homes will act as the finishing points for each turtle. Each of the turtles’ homes will be represented by a circle. Here, you need to make sure that both homes are equidistant from the starting point.
So to run the code, you can see I’m switching to the terminal window that you’ve seen previously in the course. I’m typing
python and then the name of the file, which in this case, is
03:28 Hitting Enter runs the code, and you can see our turtle graphics appear and our turtles do their work. That’s looking good, so the visual aspects are complete. Now we need to create the game play.
03:40 This starts by creating the die. There are many other ways we could do this, but creating a list is a simple one. It’s generally easy to understand. Here we have our six numbers and if you felt like it, you could extend the game by changing the numbers present or increasing them.
04:02 It’s time to start developing the code for the rest of the game. You’re going to be using a loop with conditional statements here, so you need to be careful with indentations and spaces and follow along exactly as you see onscreen. To start, take a look at the steps your program will need to take to run the game.
04:33 Step three: players will take it in turns to roll the die and move their respective turtle. Those steps will keep repeating until one of them wins and the loop gets broken out of. Let’s see that code being created.
Firstly, we have a
for loop with
20 steps. That will be more than enough for one player to be declared the winner. Next, the program checks to see if
player_one has gone past the finishing line.
This is then printed to the screen so P1 sees what they rolled. The number of steps that the turtle will take is calculated and printed out. And then that same calculation is applied by moving
And that’s the whole code of the game! The
else part of the loop will keep being repeated until one player or the other crosses the finish line and wins. When that happens,
break will occur and we will move out of the loop and the program will be finished.
We see the turtles creating their homes and going back to the start. Now we’re going to move the graphics window over so you can see what’s happening in both clearly. P1 hits Enter to roll, rolls the dice, gets a
80 steps forward. P2 rolls
120, and so on. The race progresses with each player taking a turn and rolling the dice. It looks like player one is going to win…though player two catches up.
Become a Member to join the conversation.