Project: The Python Turtle Race
00:00
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.
00:47 Firstly, each player has a turtle, which is indicated by a different color. You can have more than two players, but for the sake of this tutorial, you’ll be creating a two-player game.
00:57
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 1
to 6
.
01:09 Now that you understand the logic of the game, you can go ahead and begin creating it. First up, we’ll need to set up the environment.
01:18
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.
01:32
We’re going to start out the coding by importing the turtle
library and also importing random
, which we’ll need for the dice throws later on. Next up, creating the turtles.
01:43
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 "turtle"
.
01:58
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:11
player_two
is created by cloning player_one
, setting the color to "blue"
, making sure the pen is up, and then changing the position to (-200,-100)
, so on the left lower side of the screen.
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.
02:43
So this time we’re going to get player one’s turtle to do some work. It’s going to go to (300,60)
, put the pen down, and then it’s going to draw a 40
radius circle.
02:56 Once it’s finished, the pen will go back up so it doesn’t draw a line, and then it will go back to its starting position.
03:04
You follow similar activity for player_two
, but the numbers are slightly different because player_two
is further down the screen. Now let’s have a look at that code running.
03:15
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 python_turtle_race.py
.
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.
03:55 And these will be randomly selected using code that you’re going to see a little further on in the game.
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:20 Step one: the program will check if either turtle has reached its home and if they have, declare that turtle the winner. Step two: if they haven’t, then they will keep trying by rolling die.
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.
04:48
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.
05:00
If they have, it prints out "P1 wins!!!"
and uses break
to exit the loop.
05:07
The next code checks the same for player_two
, and notice that the tuple is slightly different because player_two
’s position is different on the screen.
05:17
The else
is the main loop of the game. As neither player has won, we will carry on. So firstly, player one is asked to roll the die with a dummy input variable.
05:29 It doesn’t matter what they enter here, as long as they hit Enter.
05:34
player one’s dice outcome is created by using random.choice()
, which picks one of the values of the list die
that we created earlier on.
05:45
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 player_one
forward.
06:01
The rest of the code follows the same pattern, but is for player_two
.
06:10
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.
06:26
Let’s take a look at that in action. Here we are back in the terminal again, and we’re going to run the program once more with python python_turtle_race.py
.
06:37
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 4
, moves 80
steps forward. P2 rolls 6
for 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.
07:08 But finally, player one crosses the line and wins the game.
07:15
And there you go! You’ve created a simple implementation of a game that exists in the real world and can also exist for you using Python’s turtle
graphics to create it.
Become a Member to join the conversation.