In: Computer Science
import random
import turtle
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()
june.shape('turtle')
while isInScreen(wn,june):
coin = random.randrange(0, 2)
if coin == 0:
june.left(90)
else:
june.right(90)
june.forward(50)
wn.exitonclick()
main()
Hi. I have answered a similar question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code
'''
File: <filename>
Author: <your name>
Description: This program creates two turtles, move them randomly until one of them goes out of window
'''
import random
import turtle
def isInScreen(win, turt):
'''
this method checks if turt is inside bounds of win
:param win: turtle screen
:param turt: turtle object
:return: True if inside, False if not
'''
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
# modified the if statement to use a different approach. (does the required task in a single step)
if turtleX > rightBound or turtleX < leftBound or turtleY > topBound or turtleY < bottomBound:
stillIn = False
return stillIn
def move(turt, distance):
'''
This method moves turtle turt in distance spaces randomly
:param turt: turtle object
:param distance: distance to be moved
:return: nothing
'''
# randomly turning left or right 90 degrees and moving forward distance spaces
coin = random.randrange(0, 2)
if coin == 0:
turt.left(90)
else:
turt.right(90)
turt.forward(distance)
def main():
'''
this is where execution starts. the method creates two turtles, move them continiously until
one of them goes outside the window. first turtle moves greater distance, but slowly,
second turtle moves quickly, but using 1/5th of the speed of first
:return: nothing
'''
wn = turtle.Screen()
june = turtle.Turtle() # first turtle
june.shape('turtle')
june.speed(0) # maximum speed
tim = turtle.Turtle() # creating second turtle
tim.shape('turtle')
tim.color('red') # red color
tim.up() # pen up
tim.goto(100, 0) # placing 100 spaces right to june
tim.down() # pen down
tim.speed(0) # max speed
# looping until june or tim goes out of bounds
while isInScreen(wn, june) and isInScreen(wn, tim):
move(june, 50) # moving june 50 spaces
for i in range(5): # looping for 5 times
move(tim, 10) # moving tim 10 spaces (1/5th of june's distance)
# after while loop, checking which turtle is up high
if june.pos()[1] > tim.pos()[1]:
june.up() # pen up
june.goto(0, 0) # moving to center
# writing a message saying that the first turtle was up high
june.write("First turtle was higher up!", align='center', font=("Arial", 16, "bold"))
else:
# doing the same with tim (second turtle)
tim.up()
tim.goto(0, 0)
tim.write("Second turtle was higher up!", align='center', font=("Arial", 16, "bold"))
wn.exitonclick()
main()
#output screenshots