Welcome to DS2000 CS Practicum 3!
Today we are going to combine our knowledge of functions, while loops and the Python turtle visualization module to create a simulation of a race between two turtles. We will also learn about an indispensable element of game programming: the main loop. In the main loop each character (here: turtle 1 and turtle 2) makes one step, then the loop continues. It looks like this:def main(): # create the initial conditions .... # now, run the game: while True: # continues forever update(tagger, target) # each character makes one step in this function if game_over(tagger, target): # did the tagger catch up with the target? break # if yes finish the game # if no, continue with the while loopLike with any other problem in programing, we'll break it down into simpler steps (Exercises 1-5 are obligatory to make full score, but give exercise 6 a shot!):
-
Ex 1: we create a turtle in a predefined location
Ex 2: we have the turtle go to finish point (300, 0)
Ex 3: add another turtle to do the same
Ex 4: we make the turtle move one step at the time
Ex 5: we use conditional statements and the
distance
function to determine weather the turtle made it to the finish line.
Ex 6: extra challenge!
Ex1: Creating (spawning) a turtle at predefined location
During the lecture you learnt that:
-
before creating the turtle you need to let python know we're going to use the turtle library:
import turtle
You define constant values using variables spelled with capital letters at the beginning of the file, like
TURTLE_1_X = -100 TURTLE_1_Y = -150to create create an actual turtle:
myturtle = turtle.Turtle()
to have the turtle move to a location, you use the goto
function line myturtle.goto(x, y)
-
import the turtle library
create the constants with the coordinates of our turtle
Write a function
spawn(x, y)
that creates a turtle, moves it to the specified coordinates (x, y) and returns the turtle.
Write a main()
function that calls our spawn
function with the constants as arguments for x
and y
and saves the output to turtle1
variable.
Call the main function to make sure the turtle is created in the right place
''' pr03 - turtles ''' import turtle TURTLE_1_X = -100 TURTLE_1_Y = -150 def spawn(x, y): myturtle = #how do we create a turtle? # in this line move turtle to x, y return myturtle def main(): turtle1 = spawn(# where?) main()Save your code as
pr03_ex1.py
for submission.
Ex2: Moving the turtle
Now that we have our turtle, let's have it go towards a goal. Add the finish coordinates in your file.FINISH_X = 300 FINISH_Y = 0The easiest way to have the turtle move is to use the function
goto(x, y)
like in the previous exercise. However, you see from the animation, that the turtle is not really facing the direction in which it's moving. Let's fix that. Create a function mygoto(turtle_obj, x, y)
that will:
-
determine the angle the turtle should face (Hint: use the
turtle.towards(target_x, target_y)
function as explained in https://docs.python.org/2/library/turtle.html#turtle.towards.
rotate the turtle to the determined angle (Hint: use the turtle.setheading(angle)
function as explained in https://docs.python.org/2/library/turtle.html#turtle.setheading
move the turtle to the target using the movement functions we already know
pr03_ex2.py
for submission.
Ex3: Adding more competitors
We now have a turtle that starts in one point and the moves to the finish point. Let's add a competitor to this race, starting at another location:TURTLE_2_X = -150 TURTLE_2_Y = -50Extend your main function so now it also creates
turtle2
with the new coordinates and also moves it to the finish point.
Save your code as pr03_ex3.py
for submission.
Ex4: Introducing: The main loop!
As you ran the previous exercise you noticed that depending on the order of your movement instructions, the first turtle to move makes it to the finish line faster than the other, regardless, how far they are. That's not fair! Let's fix it by making sure that at any given instant each turtle only gets to make one step. We will need to first modify ourmygoto(turtle, x, y)
function to only make one step at a time and (2) introduce the loop so that our turtles continue taking turns to move.
-
modify your
mygoto(turtle, x, y)
so rather than going straight to the finish, it takes one step - rather than using the turtle.goto(x, y)
use turtle.forward(step_count)
, like this:
def mygoto(turtle, x, y): # in this line calculate the angle # in this line set the heading turtle.forward(1)Introduce the main loop, where at each iteration each of the turtles moves one step towards the goal:
TURTLE_1_X = -100 TURTLE_1_Y = -150 TURTLE_2_X = -150 TURTLE_2_Y = -50 FINISH_X = 300 FINISH_Y = 0 def spawn(x, y): # create a turtle # move it to the x, t position return the turtle def mygoto(turtle, x, y): # in this line calculate the angle # in this line set the heading turtle.forward(1) def main(): turtle1 = spawn(TURTLE_1_X, TURTLE_1_Y) turtle2 = spawn(TURTLE_2_X, TURTLE_2_Y) while 1: mygoto(turtle1,FINISH_X, FINISH_Y) mygoto(turtle2,FINISH_X, FINISH_Y) main()
pr03_ex4.py
for submission.
Remember: The program will be stuck in the loop of moving turtles, you can break this "forever loop" by (1) closing the visualization window or (2) clicking on the interpreter (not the visualization window) and pressing Ctrl+C (Windows, Mac, and Linux).
Ex5: Introducing: The Game-Over condition.
We now have two turtles racing but they never quite quit moving. Let's break this vicious cycle of turtle abuse and say the game is over if either of the turtles is within 5 from the finish line. Combine theturtle.distance(x, y)
function https://docs.python.org/2/library/turtle.html#turtle.distance and an if statement: if the distance is lower than 5, break the loop using the break
keyword like this:
def main(): turtle1 = spawn(TURTLE_1_X, TURTLE_1_Y) turtle2 = spawn(TURTLE_2_X, TURTLE_2_Y) while 1: mygoto(turtle1,FINISH_X, FINISH_Y) mygoto(turtle2,FINISH_X, FINISH_Y) if # is the distance between turtle 1 and the finish point lower than 5? print('Turtle1 won!') break if # is the distance between turtle 2 and the finish point lower than 5? print('Turtle2 won!') breakSave your code as
pr03_ex5.py
for submission.
Ex6: Extra challenge
Rather than having the turtles run for the finish line, have one run away from the other and the other chase it. The end condition is when the running turtle leaves the screen or the chasing turtle catches up. Extra-extra: try adding some randomness to every move of the turtle that tries to run away to make it more realistic and difficult. To generate a random number:import random number = random.random() # a random number between 0 and 1 number = random.random() * 2 # a random number between 0 and 2 number = random.random() * 2 - 1 # random number between -1 and 1