Stop a moving Python turtle when it's close to another turtle
up vote
1
down vote
favorite
How do I stop a randomly moving turtle using a while
loop when it comes with 50 units of another turtle?
I have one turtle that randomly selects a location and creates a large dot or hole, and another turtle that moves around randomly making 90 degree turns and moving forward 50 units every time. The randomly moving turtle stops when it goes off of the end of the screen, but how do I also make the turtle stop when it gets to the hole created by the other turtle?
import random
import turtle
def turtlesClose(t1, t2):
if t1.distance(t2)<50:
return True
else:
return False
def isInScreen(win,turt):
leftBound = -win.window_width() / 2
rightBound = win.window_width() / 2
topBound = win.window_height() / 2
bottomBound = -win.window_height() / 2
turtleX = turt.xcor()
turtleY = turt.ycor()
stillIn = True
if turtleX > rightBound or turtleX < leftBound:
stillIn = False
if turtleY > topBound or turtleY < bottomBound:
stillIn = False
return stillIn
def main():
wn = turtle.Screen()
# Define your turtles here
june = turtle.Turtle()
july = turtle.Turtle()
july.shape('turtle')
july.up()
july.goto(random.randrange(-250, 250, 1), random.randrange(-250, 250, 1))
july.down()
july.dot(100)
june.shape('turtle')
while isInScreen(wn,june):
coin = random.randrange(0, 2)
dist = turtlesClose(july, june)
if coin == 0:
june.left(90)
else:
june.right(90)
june.forward(50)
if dist == 'True':
break
main()
python python-3.x turtle-graphics
add a comment |
up vote
1
down vote
favorite
How do I stop a randomly moving turtle using a while
loop when it comes with 50 units of another turtle?
I have one turtle that randomly selects a location and creates a large dot or hole, and another turtle that moves around randomly making 90 degree turns and moving forward 50 units every time. The randomly moving turtle stops when it goes off of the end of the screen, but how do I also make the turtle stop when it gets to the hole created by the other turtle?
import random
import turtle
def turtlesClose(t1, t2):
if t1.distance(t2)<50:
return True
else:
return False
def isInScreen(win,turt):
leftBound = -win.window_width() / 2
rightBound = win.window_width() / 2
topBound = win.window_height() / 2
bottomBound = -win.window_height() / 2
turtleX = turt.xcor()
turtleY = turt.ycor()
stillIn = True
if turtleX > rightBound or turtleX < leftBound:
stillIn = False
if turtleY > topBound or turtleY < bottomBound:
stillIn = False
return stillIn
def main():
wn = turtle.Screen()
# Define your turtles here
june = turtle.Turtle()
july = turtle.Turtle()
july.shape('turtle')
july.up()
july.goto(random.randrange(-250, 250, 1), random.randrange(-250, 250, 1))
july.down()
july.dot(100)
june.shape('turtle')
while isInScreen(wn,june):
coin = random.randrange(0, 2)
dist = turtlesClose(july, june)
if coin == 0:
june.left(90)
else:
june.right(90)
june.forward(50)
if dist == 'True':
break
main()
python python-3.x turtle-graphics
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
How do I stop a randomly moving turtle using a while
loop when it comes with 50 units of another turtle?
I have one turtle that randomly selects a location and creates a large dot or hole, and another turtle that moves around randomly making 90 degree turns and moving forward 50 units every time. The randomly moving turtle stops when it goes off of the end of the screen, but how do I also make the turtle stop when it gets to the hole created by the other turtle?
import random
import turtle
def turtlesClose(t1, t2):
if t1.distance(t2)<50:
return True
else:
return False
def isInScreen(win,turt):
leftBound = -win.window_width() / 2
rightBound = win.window_width() / 2
topBound = win.window_height() / 2
bottomBound = -win.window_height() / 2
turtleX = turt.xcor()
turtleY = turt.ycor()
stillIn = True
if turtleX > rightBound or turtleX < leftBound:
stillIn = False
if turtleY > topBound or turtleY < bottomBound:
stillIn = False
return stillIn
def main():
wn = turtle.Screen()
# Define your turtles here
june = turtle.Turtle()
july = turtle.Turtle()
july.shape('turtle')
july.up()
july.goto(random.randrange(-250, 250, 1), random.randrange(-250, 250, 1))
july.down()
july.dot(100)
june.shape('turtle')
while isInScreen(wn,june):
coin = random.randrange(0, 2)
dist = turtlesClose(july, june)
if coin == 0:
june.left(90)
else:
june.right(90)
june.forward(50)
if dist == 'True':
break
main()
python python-3.x turtle-graphics
How do I stop a randomly moving turtle using a while
loop when it comes with 50 units of another turtle?
I have one turtle that randomly selects a location and creates a large dot or hole, and another turtle that moves around randomly making 90 degree turns and moving forward 50 units every time. The randomly moving turtle stops when it goes off of the end of the screen, but how do I also make the turtle stop when it gets to the hole created by the other turtle?
import random
import turtle
def turtlesClose(t1, t2):
if t1.distance(t2)<50:
return True
else:
return False
def isInScreen(win,turt):
leftBound = -win.window_width() / 2
rightBound = win.window_width() / 2
topBound = win.window_height() / 2
bottomBound = -win.window_height() / 2
turtleX = turt.xcor()
turtleY = turt.ycor()
stillIn = True
if turtleX > rightBound or turtleX < leftBound:
stillIn = False
if turtleY > topBound or turtleY < bottomBound:
stillIn = False
return stillIn
def main():
wn = turtle.Screen()
# Define your turtles here
june = turtle.Turtle()
july = turtle.Turtle()
july.shape('turtle')
july.up()
july.goto(random.randrange(-250, 250, 1), random.randrange(-250, 250, 1))
july.down()
july.dot(100)
june.shape('turtle')
while isInScreen(wn,june):
coin = random.randrange(0, 2)
dist = turtlesClose(july, june)
if coin == 0:
june.left(90)
else:
june.right(90)
june.forward(50)
if dist == 'True':
break
main()
python python-3.x turtle-graphics
python python-3.x turtle-graphics
edited Nov 11 at 8:53
cdlane
16.6k21042
16.6k21042
asked Nov 11 at 4:25
Stephen Dangel
102
102
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
The problem with your code is this statement:
if dist == 'True':
You don't want quotes around True
. Although this will work:
if dist == True:
The correct way to express this is:
if dist is True:
or better yet:
if dist:
Otherwise your code seems to work. Below's a rewrite taking advantage of some turtle idioms and other code cleanup:
from random import randrange, choice
from turtle import Screen, Turtle
CURSOR_SIZE = 20
def turtlesClose(t1, t2):
return t1.distance(t2) < 50
def isInScreen(window, turtle):
leftBound = -window.window_width() / 2
rightBound = window.window_width() / 2
topBound = window.window_height() / 2
bottomBound = -window.window_height() / 2
turtleX, turtleY = turtle.position()
return leftBound < turtleX < rightBound and bottomBound < turtleY < topBound
def main():
screen = Screen()
july = Turtle('circle')
july.shapesize(100 / CURSOR_SIZE)
july.up()
july.goto(randrange(-250, 250), randrange(-250, 250))
july.down()
june = Turtle('turtle')
while isInScreen(screen, june):
if turtlesClose(july, june):
break
turn = choice([june.left, june.right])
turn(90)
june.forward(50)
main()
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
The problem with your code is this statement:
if dist == 'True':
You don't want quotes around True
. Although this will work:
if dist == True:
The correct way to express this is:
if dist is True:
or better yet:
if dist:
Otherwise your code seems to work. Below's a rewrite taking advantage of some turtle idioms and other code cleanup:
from random import randrange, choice
from turtle import Screen, Turtle
CURSOR_SIZE = 20
def turtlesClose(t1, t2):
return t1.distance(t2) < 50
def isInScreen(window, turtle):
leftBound = -window.window_width() / 2
rightBound = window.window_width() / 2
topBound = window.window_height() / 2
bottomBound = -window.window_height() / 2
turtleX, turtleY = turtle.position()
return leftBound < turtleX < rightBound and bottomBound < turtleY < topBound
def main():
screen = Screen()
july = Turtle('circle')
july.shapesize(100 / CURSOR_SIZE)
july.up()
july.goto(randrange(-250, 250), randrange(-250, 250))
july.down()
june = Turtle('turtle')
while isInScreen(screen, june):
if turtlesClose(july, june):
break
turn = choice([june.left, june.right])
turn(90)
june.forward(50)
main()
add a comment |
up vote
0
down vote
accepted
The problem with your code is this statement:
if dist == 'True':
You don't want quotes around True
. Although this will work:
if dist == True:
The correct way to express this is:
if dist is True:
or better yet:
if dist:
Otherwise your code seems to work. Below's a rewrite taking advantage of some turtle idioms and other code cleanup:
from random import randrange, choice
from turtle import Screen, Turtle
CURSOR_SIZE = 20
def turtlesClose(t1, t2):
return t1.distance(t2) < 50
def isInScreen(window, turtle):
leftBound = -window.window_width() / 2
rightBound = window.window_width() / 2
topBound = window.window_height() / 2
bottomBound = -window.window_height() / 2
turtleX, turtleY = turtle.position()
return leftBound < turtleX < rightBound and bottomBound < turtleY < topBound
def main():
screen = Screen()
july = Turtle('circle')
july.shapesize(100 / CURSOR_SIZE)
july.up()
july.goto(randrange(-250, 250), randrange(-250, 250))
july.down()
june = Turtle('turtle')
while isInScreen(screen, june):
if turtlesClose(july, june):
break
turn = choice([june.left, june.right])
turn(90)
june.forward(50)
main()
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The problem with your code is this statement:
if dist == 'True':
You don't want quotes around True
. Although this will work:
if dist == True:
The correct way to express this is:
if dist is True:
or better yet:
if dist:
Otherwise your code seems to work. Below's a rewrite taking advantage of some turtle idioms and other code cleanup:
from random import randrange, choice
from turtle import Screen, Turtle
CURSOR_SIZE = 20
def turtlesClose(t1, t2):
return t1.distance(t2) < 50
def isInScreen(window, turtle):
leftBound = -window.window_width() / 2
rightBound = window.window_width() / 2
topBound = window.window_height() / 2
bottomBound = -window.window_height() / 2
turtleX, turtleY = turtle.position()
return leftBound < turtleX < rightBound and bottomBound < turtleY < topBound
def main():
screen = Screen()
july = Turtle('circle')
july.shapesize(100 / CURSOR_SIZE)
july.up()
july.goto(randrange(-250, 250), randrange(-250, 250))
july.down()
june = Turtle('turtle')
while isInScreen(screen, june):
if turtlesClose(july, june):
break
turn = choice([june.left, june.right])
turn(90)
june.forward(50)
main()
The problem with your code is this statement:
if dist == 'True':
You don't want quotes around True
. Although this will work:
if dist == True:
The correct way to express this is:
if dist is True:
or better yet:
if dist:
Otherwise your code seems to work. Below's a rewrite taking advantage of some turtle idioms and other code cleanup:
from random import randrange, choice
from turtle import Screen, Turtle
CURSOR_SIZE = 20
def turtlesClose(t1, t2):
return t1.distance(t2) < 50
def isInScreen(window, turtle):
leftBound = -window.window_width() / 2
rightBound = window.window_width() / 2
topBound = window.window_height() / 2
bottomBound = -window.window_height() / 2
turtleX, turtleY = turtle.position()
return leftBound < turtleX < rightBound and bottomBound < turtleY < topBound
def main():
screen = Screen()
july = Turtle('circle')
july.shapesize(100 / CURSOR_SIZE)
july.up()
july.goto(randrange(-250, 250), randrange(-250, 250))
july.down()
june = Turtle('turtle')
while isInScreen(screen, june):
if turtlesClose(july, june):
break
turn = choice([june.left, june.right])
turn(90)
june.forward(50)
main()
answered Nov 11 at 8:50
cdlane
16.6k21042
16.6k21042
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245825%2fstop-a-moving-python-turtle-when-its-close-to-another-turtle%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown