Question

In: Computer Science

In Python, I have a turtle that is already coded to move in correlation with the...

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

Solutions

Expert Solution

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

Related Solutions

is it possible to have more than one ellipse on python turtle.
is it possible to have more than one ellipse on python turtle.
2.) Postfix to infix translator (NOTE: This is NOT the evaluator you have already coded!) •...
2.) Postfix to infix translator (NOTE: This is NOT the evaluator you have already coded!) • Create a java class called PostfixToInfixTranslator that includes a main method. • The code you write should prompt the user for an expression in postfix notation and use ArrayStack to output the equivalent expression in infix. • See the following session: Enter a postfix expression: 3 4 + 2 * In infix notation that is: ((3+4)*2) Translate another expression [y/n]? y Enter a postfix...
For question 1 , consider that inside the class Sky, we have already coded the following:...
For question 1 , consider that inside the class Sky, we have already coded the following:     public class Sky      {         private Color color;         public Sky( Color c)           {               color = c;           }       }   Consider the following method header:     public Color getColor(); Is this method a constructor, mutator or accessor?   Inside a method main, we see code like:      Airplane.foo3(34.6); From this, reconstruct the header of method foo3 (which belongs to class...
Needs to be coded in Python. Hello i am working on a project for my computer...
Needs to be coded in Python. Hello i am working on a project for my computer programming course and i am having trouble with one part. The code needs to be able to produce two versions of an output given inputs by the user. for instance. Here is the code i have to produce the following output. The input from the user are num1 num2 and asc which is just asking if they want the output to be ascending or...
How to make a python program that imports only turtle and math library where I can...
How to make a python program that imports only turtle and math library where I can click once on the screen to set the center of the square, move the mouse to define the edge-length of the square; click a second time to draw the square with the defined edge-length and center point?
This is my coded "multipath adventure" that I created. Now I have to modify it to...
This is my coded "multipath adventure" that I created. Now I have to modify it to include loops but I am really struggling to do so. Below are the instructions and my current code that needs to be modified. Thank you in advance! You must have at least 1 for loop in your code The for loop must actually be used as a loop, and it must have real effect in your program (it is theoretically possible to use the...
I need to take the code i already have and change it to have at least...
I need to take the code i already have and change it to have at least one function in it. it has to include one function and one loop. I already have the loop but cant figure out how to add a function. I thought i could create a funciton to call to the totalCost but not sure how to do it. Help please. #include #include //main function int main(void) {    char userName [20];    char yesOrNo [10];   ...
I need to write these three functions in Python using turtle module. def line(t, coord1, coord2)...
I need to write these three functions in Python using turtle module. def line(t, coord1, coord2) t is a turtle object passed in coord1 and coord2 are 2-element lists that represent x, y coordinates draws a line from coord1 to cord2 def poly(t, *coords) t is a turtle object passed in *coords is any number of x, y coordinates each as a 2-element list draws a polygon based on coords (will close off polygon by drawing a line from last...
with python, create a fake logo for a company using turtle graphics
with python, create a fake logo for a company using turtle graphics
Must be coded in python. Consuming A Web Service with Python Consuming data from programmable web...
Must be coded in python. Consuming A Web Service with Python Consuming data from programmable web based API’s is growing rapidly. According to ProgrammableWeb, in just six years (2010-2016), web API counts increased 758 percent with the majority being REST based. This is driven by a myriad of factors but growth of cloud based applications, (distributed computing by another name), and the resulting need to integrate the different categories is a major factor. Description Write a client to consume a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT