Upgrade your turtle race game by choosing how many turtles you want to race each time, compile the results to check which turtle has gone the furthest, and finally announce the winner. Play your new and improved game with as many family and friends as you like!
A fun turtle racing game like this one:
By upgrading your turtle race game, you will learn how to:
Use functions to set up the scope of a game before running it
Use global and local variables
Create and then sort a nested list of results
Use an if statement to check and announce the winner
A computer or tablet capable of accessing the online interactive Python environment called Trinket. You can create a trinket.io account using your google school account; or
If you don’t want to create an account you can use the embedded interactive trinkets below to run code and see what happens; or
You can use a computer that has the Python programme installed.
Sign in to your trinket.io account, click on your name and then select ‘My Trinkets’ and open your Turtle Race programme.
You have typed many lines of code just to get 4 turtles to race. What if you wanted 8 racing turtles? You can use functions to simplify your code and to set up the scope of the game before running it. Start by creating a function to draw the racetrack def drawRacetrack(). Specify the starting position using goto(-150, 150) and then move your code for drawing the lines here.
You can vary the length of the lines depending on how many turtles you have racing by creating a global variable (a variable created in the main part of the code has global scope so it can be used by multiple functions) t = 8. You can then use t as the parameter in the range function.
As a nice finishing touch, how about giving the game a name? Use the goto(0, 170) function to send the turtle to the middle top of the screen and then use the write function to add a title write("Turtle Race", align="center", font=("Monospace", 16)). The text for the title is known as a string (a collection of characters - letters, numbers, punctuation and spaces) as opposed to an integer (always a whole number). The parameters 'align' and 'font' can be changed to your liking.
Create a new global variable list turtles = [ ] where you can store the list of turtles you will make, and another global variable list colours = ["red", "blue", etc.]. Make sure you have enough colours for the number of turtles you want to race (t).
Define a function to create the turtles for you def createTurtles(). Start a for loop, using t as the parameter in the range function, and type turtles.append(Turtle()). This command creates a new turtle and adds (appends) it to your list of turtles. You can now copy your code for ada.turtle and paste it underneath. Change ada to turtles[-1], which specifies the last element in that list:
turtles[-1].shape("turtle")
turtles[-1].color(colours[i])
turtles[-1].penup()
turtles[-1].goto(-170, 128-(i*30))
turtles[-1].pendown()
As this new function will create all your turtles, you can delete the code for ada, bob, li and moh.
Next, create a function to make the turtles race def raceTurtles(). Use your for loop with range(100) but replace the code for each turtle with a nested loop:
for j in range(t):
turtles[j].fd(randint(0,6))
Finally, call all your new functions to start the race!
drawRacetrack()
createTurtles()
raceTurtles()
Sometimes it can be hard to tell which turtle is the winner. To check which turtle has traveled the furthest, you need to know their final x coordinate. Create a new function def writeResult(): and inside the function create a new local variable (a variable created inside a function has only a local scope and is forgotten once the function ends) to store the list of turtle x coordiantes xcors = [ ].
As you did when creating the turtles, use a for loop with range(t) to add (append) the x coordinates to the xcors list. The function xcor() will return the x value for each turtle.
for i in range(t):
xcors.append(turtles[i].xcor())
You need to combine the lists you have for the xcoordinates and the colours so it will be possible to tell which colour has won. Use the zip() function to create a list of results where each entry is a tuple (a list that cannot be changed once created) of the xcor and colour results = zip(xcors,colours). The outcome is essentially a list of lists, or a nested array (a array that contains another array as an element).
Now that you have a list of results you can sort it in descending order using the sort() function with parameter 'reverse' set to True results.sort(reverse=True).
Printing the results print(results) will show which turtle has the largest x coordinate and is thus the winner. Finally, don't forget to call this new function at the end of your code writeResult().
This is fine, but it would be even better to have an announcement of the winner! Use the goto(0, 150-(t+1)*30) function to get ready to write the result at the bottom middle of the screen.
Now use an if statement (specifies a condition to test followed by a block of code to run if True e.g. if A is true then print B else print C) to check whether there is a clear winner or a draw. First check for a draw, which will be the case if the first two items in the results list are the same. If this is True then use the write() function to announce a draw. If it is not true then there will be a clear winner so under the else condition use the write() function to declare the winning colour, which will be the second item of the first tuple in the results list:
if(results[0]==results[1]):
write("It is a draw!", align="center", font=("Monospace",16))
else:
write(results[0][1] + " is the winner!", align="center", font=("Monospace",16))
Try out your new code with different values for t and then play the game with your family and friends.