Created
December 1, 2014 05:30
-
-
Save hoserdude/aabfd055ac7eac7825ee to your computer and use it in GitHub Desktop.
Coyote and RoadRunner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle | |
from random import random | |
#Function to make a coyote (a custom turtle) | |
def make_coyote(): | |
coyote = turtle.Turtle() | |
coyote.speed( 10 ) | |
coyote.color( 80, 80, 80 ) | |
return coyote | |
#Function to make a roadrunner (a custom turtle) | |
def make_roadrunner(): | |
roadrunner = turtle.Turtle() | |
roadrunner.speed( 10 ) | |
roadrunner.color( 128, 128, 255 ) | |
return roadrunner | |
# Setup coyote | |
wiley = make_coyote() | |
# Turn a random degree | |
wiley.setheading( random() * 360 ) | |
# Go forward 150 + a number between 1-100) | |
wiley.forward( 50 + (random() * 100) ) | |
# Setup road runner | |
speedy = make_roadrunner() | |
# Turn a random degree | |
speedy.setheading( random() * 360 ) | |
# Go forward 100 | |
speedy.forward( 100 ) | |
avg = 0 | |
for i in range(50): | |
# turn towards speedy, then run full speed | |
wiley.setheading( wiley.towards( speedy ) ) | |
wiley.forward( 20 ) | |
# if wiley is close turn and run in a random direction | |
dist = speedy.distance( wiley ) | |
avg = avg + dist | |
#If Wiley is getting too close, turn and run | |
if dist < 50: | |
speedy.setheading( random() * 360 ) | |
speedy.forward( 20 ) | |
# Display how warm Wiley is getting | |
if dist > 30: | |
# Far away | |
wiley.dot(10, "green") | |
else: | |
if dist < 30 and dist > 20: | |
# getting close | |
wiley.dot(10, "yellow") | |
else: | |
# dangerously close | |
wiley.dot(10, "red") | |
if dist == 0: | |
print "Caught you!" | |
wiley.dot(50, "red") | |
#stop the chase | |
break | |
avg = avg / 20 | |
print avg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment