Welcome to DS2000 CS Practicum 4!Since there was no lecture this week, we're going to just practice more of the skills necessary to complete your homework 3:
whileloops conditional statements (
if condition:) taking user input and making sure it's sane
Before we get to the actual exercise, a few reminders from last week that will help you understand your problems
Problem: there are more turtles on the screen than I would expect. Solution: This happens when you call any function directly on the library
turtlerather than on a turtle instance. Remember to call functions like
towardson a particular turtle, for example to calculate the distance between turtle1 and finish use
distance = turtle1.distance(FINISH_X, FINISH_Y), and not
distance = turtle.distance(FINISH_X, FINISH_Y). Problem: I calculate the angle but my turtle still faces right wherever it goes. Solution: make sure you use the
setheading()function with the calculated angle as input! Problem: When I spawn the turtles they leave a trace while going to their starting positions. Solution: use
penup()after creating the turtle but before moving and
pendown()after moving the turtle to the starting position.
Ex0: complete the extra challenge from last week
If you already did the extra challenge, please proceed to exercise 1.Your code from exercise 5 last week should have two turtles racing to the finish point. Change it so now turtle1 chases turtle2, while turtle2 tries to run away. Your end condition will be that the distance between the two turtles is less than 5 or the turtle that's running away runs off screen (the screen spans from -400 to 400 on the x axis and from -300 to 300 on the y axis). Here are some pointers to get you started:
you will need to find the coordinates (position) of turtles for this. Try searching for the appropriate functions in the documentation!
running away from something is equivalent to running towards something with the angle different by 180 degrees.
turtle1.distace(turtle2) will tell you the distance between the two turtles without having to lookup the coordinates of turtle2
don't worry about the randomness part right now, work on it if you have extra time at the end of the practicum.
Ex1: taking user inputExtend your main function so that before the main loop starts, your program asks the user for the starting positions of the turtles and their speed.
Acceptable coordinates for the x coordinate are from -350 to 350 (keep asking until the user gives acceptable coordinates)
Acceptable coordinates for the y coordinate are from -250 to 250 (keep asking until the user gives acceptable coordinates)
Try looking up in the documentation what the acceptable values for speed are.
Ex2: ending the gameCreate a
gameover(turtle1, turtle2)function. It takes the two turtles as input, and it returns
Trueif they are within 5 from each or if the running turtle went off screen. Call this function as your condition of the main
def main(): # ask the user for starting positions and speed # create the turtles while not gameover(turtle1, turtle2): # your gameplayNote that there is a
notnegation operator before
gameover. The expression of
while not gameover(turtle1, turtle2):means "while the game is not over do the following".