In: Computer Science
In Python, I have a turtle that is already coded to move in correlation with the keys I set (I can control the turtle). I need to put a second turtle in that moves WHILE the first turtle is moving and being controlled. The second turtle needs to do the following while the first turtle is still listening and being controlled:
-It needs to begin facing a random direction
-It needs to move at a speed of .5 the entire time
-It needs to move completely randomly for 40 moves, and every 40 completely random moves it does, it needs to turn in a random direction and continue moving in that direction.
-If the turtle reaches more than 250 units from the middle of the screen, the turtle needs to teleport back to the center of the screen and it needs to continue moving randomly and repeat this if it reaches 250 again.
-The turtle cannot have a line behind it, i.e. the turtle should not leave a line drawn/trace
So the turtle needs to appear with the first turtle and must appear in a random direction, move randomly 40 times, then move in a random direction until it reaches 250 units from the middle of the screen (if it reached the middle of the screen during it's 40 moves, it should reset the same) and once it reaches 250, or if at any point it does, it should teleport back to the middle of the screen and go back to moving randomly. At the same time all of this is happening, the first turtle is still doing it's thing and listening to my keyboard commands.
Please provide a code for the second turtle to be doing the above directions. If I need to alter the first turtle's code to have both of them moving at the same time, please detail how and if possible provide code. Thank you
import turtle
#import the Turtle Library
import random
#import the random library to generate a random number
s=turtle.getscreen() # brings turtle to screen
t=turtle.Turtle()
dir=random.randrange(0,360) # generates a random number
between 0 and 360 to set the direction of the turtle
t.right(dir) #
rotates the turtle by dir degrees
step40=40
# steps initialized for turtle
step250=0
t.speed(0.5) #
speed for turtle
t.penup()
# disbales the lines for turtle
while step250<=300: # loop for
tutrtle until it reaches 250
if step250>=250: # if total steps taken by turtle is
greater than 250, it goes to home and restarts
t.home()
step250=0
else:
dir=random.randrange(0,360)
t.right(dir)
t.forward(step40) # forward step
taken by turtle
step250=step250+step40
dist=t.distance(t.xcor(),t.ycor()) #
finding the distance of turtle from home pos to current pos
if dist==250: # returning to home
and beginning again if total distance between home and current
pos=250
t.home()
step250=0
import turtle #import the Turtle Library
import random #import the random library to generate a random number
s=turtle.getscreen() # brings turtle to screen
t=turtle.Turtle()
dir=random.randrange(0,360) # generates a random number between 0 and 360 to set the direction of the turtle
t.right(dir) # rotates the turtle by dir degrees
step40=40 # steps initialized for turtle
step250=0
t.speed(0.5) # speed for turtle
t.penup() # disbales the lines for turtle
while step250<=300: # loop for tutrtle until it reaches 250
if step250>=250: # if total steps taken by turtle is greater than 250, it goes to home and restarts
t.home()
step250=0
else:
dir=random.randrange(0,360)
t.right(dir)
t.forward(step40) # forward step taken by turtle
step250=step250+step40
dist=t.distance(t.xcor(),t.ycor()) # finding the distance of turtle from home pos to current pos
if dist==250: # returning to home and beginning again if total distance between home and current pos=250
t.home()
step250=0