> For the complete documentation index, see [llms.txt](https://slyford.gitbook.io/turtle-porject/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://slyford.gitbook.io/turtle-porject/master.md).

# Project: 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. Now it’s time to put all of these elements together and develop your first fun computer game.&#x20;

Before you begin, here’s what you need to know about the game:

1. **The Objective:** The player whose turtle crosses the finish line first wins the game.
2. **How to Play:**
   * The players alternate turns to spin a wheel that will randomly move the turtle around the screen.
3. **The Structure:**
   * Each player has a turtle indicated by a different color. You can have more than two players, but for the sake of this demo, you’ll be creating a two-player game.
   * Each player spins the wheel that will generate a random (x, y) position on the screen that the turtle will move to.
   * The game ends once a player's turtle crosses the finish line.

Now that you’ve understood the logic of the game, you can go ahead and begin creating it! First, you’ll need to set up the environment.

## Setting up the environment

Start by typing in the following import statements:

```
import turtle
from random import randint
```

### New Modules and functions

In order to make our game have random behavior we need to import a new module, logically enough called “random”. The `random` module, like the `turtle` module, brings additional functionality into our program so we can use it.

The import statements doesn't do anything immediately,  but now our program has access to the functions in the `random` module specifically the `randint` method which we will use to generate random x and y coordinates for the turtle on the screen.

**Pick a Number, Any Number: `random.randint()`**

The module `random`, as the name suggests, creates randomness. We’ll use the functions in the random module to make our turtle movement less predictable, and maybe more interesting for the game.

One of those functions on the module is called `randint()`, and it generates random integers.&#x20;

Enter this into our **IDLE Shell command line** to try the `randint()` function out:

```
>>> randint(0, 10)
4
>>> randint(0, 10)
10

```

In the above lines we’ve used the `randint()` function twice and it returned a different number each time. You will very (likely) get different values than the ones shown above in the code sample.  This is what `randint()` does, it returns randomly generated integers.&#x20;

The two numbers we passed to it (0 and 10) are parameters telling `randint()` the beginning and ending limits of numbers we want it to generate. In our case we want integer numbers ranging from 0 to 10, including both 0 and 10. Random number generators are used a lot in game programming to create unexpected behavior and challenges for the player.

Once these libraries are successfully called into your environment, you can proceed with the rest of your program.

## Setting up the turtles and few variables

First, we will set the background of the window to black.&#x20;

```
turtle.bgcolor("black")
```

Next, we will store some important variables that we will use later in the game.&#x20;

```
s = turtle.getscreen()
leftBound = - s.window_width() / 2
rightBound = s.window_width() / 2
topBound = s.window_height() / 2
bottomBound = -s.window_height() / 2
```

These variables represent the coordinates of the screen. `leftBound` and `topBound` identify the top left coordinates of the screen, `rightBound` and `bottomBound` identify the bottom right coordinates of the screen. These are calculated by calling the `window_width()` and `window_height()` methods to retrieve the  total height and width of the screen and dividing the return value by 2.&#x20;

You now have to create the two turtles that will represent the players. Each turtle will be a different color, corresponding to the different players. Here, player one is **green** and player two is **blue.** Once you create the turtles, place them at their starting positions and make sure that these positions are aligned.&#x20;

```
#Creating the first player green turtle
t1 = turtle.Turtle()
t1.color("green")
t1.shape("turtle")
t1.penup()
t1.goto(200,-200)

#Creating the second player blue turtle
t2 = turtle.Turtle()
t2 = t1.clone()
t2.color("blue")
t2.penup()
t2.goto(-200,-200)
```

Note that you created player two’s turtle by cloning player one’s turtle, changing its color, and placing it at a different starting point.

You now need to setup the finish line for the turtles. We'll use a third turtle to draw the red finish line which will be located at the top of the screen.

```
t3 = turtle.Turtle()
t3 = t1.clone()
t3.color("red")
t3.penup()
t3.goto(leftBound,topBound-100)
t3.pendown()
t3.goto(rightBound, topBound-100)
```

Awesome! The visual aspects of your game are complete. It should look something similar to this:

![](/files/-Mje9KhWg1rj0s0aX2uG)

You can now start programming the game simulating players spinning wheels that randomly move the corresponding turtles of each player on the screen.&#x20;

## Developing the game

It’s time to develop the code for the rest of the game. You’ll be using loops and conditional statements here, so you need to be careful with the indentations and spaces. To start, take a look at the steps your program will need to take to run the game:

1. **Step 1:** You’ll start by telling your program to check if either turtle has crossed the finish line.
2. **Step 2:** If they haven’t, then you’ll tell your program to allow the players to continue trying.
3. **Step 3:** In each loop, you tell your program to randomly pick new x and y coordinates for the turtle.
4. **Step 4:** You then tell it to move the respective turtle to the new randomly generated position.

The program keeps repeating this process, and stops once one of the turtles crosses the finish line. Here’s how the code looks:

```
t1.penup()
t2.penup()

for i in range(100):
    if t1.ycor()>= (topBound-100):
        print("Player One Wins!")
        break
    elif t2.ycor()>= (topBound-100):
        print("Player Two Wins!")
        break
    else:
        player_one_turn = input("Press 'Enter' to move your turtle ")
        x= randint(leftBound, rightBound)
        y= randint(bottomBound,topBound)
        print (x, y)
        t1.goto(x,y)
        
        player_two_turn = input("Press 'Enter' to move your turtle ")
        x= randint(leftBound, rightBound)
        y= randint(bottomBound,topBound)
        print (x, y)
        t2.goto(x,y)
```

In summary, this is what the code is doing:

1. **Lines 1 and 2** lift up the two turtles pen so that their traces won't show as they move randomly around the screen.
2. **Line 4** sets up a `for` loop with a range from 1 to 100 (just a big enough value to allow one of the turtles to reach the finish line).
3. **Lines 5 through 10** check if either player has crossed the finish line. If one of them has, then the program prints out the corresponding statement and breaks the loop.
4. **Line 11** moves the program on to the next set of steps if neither player has won.
5. **Line 12** prints out a statement asking player one to press the Enter key to move their turtle (it's like spinning the wheel).
6. **Lines 13 and 14** generates a random x and y coordinates inside the screen boundaries.
7. **Line 15** prints the outcome of the random coordinates generated.
8. **Line 16** moves player one's turtle to the randomly generated x and y coordinates.
9. **Lines 18 to 22** repeat these steps for player two.

&#x20;The entire `for` loop is repeated until one of the player’s turtles reaches the finish line.
