In: Computer Science
What would the Python code be to create the following working program using turtle:
-Make 4 turtles that are YELLOW, and 4 turtles that are GREEN. Create these turtles using one or more lists.
-Make the yellow turtles start at a different randomized location somewhere on the left side of the screen within the range of (-100,100) and (0, -100)
-Make the green turtles start at a different randomized location somewhere on the right side of the screen within the range of (0,100) and (100, -100)
-Both the yellow and green turtles should be moving randomly.
-Create a boundary for the turtle game using a circle with a 500 unit radius using an invisible turtle- i.e.: it does not count as one of the turtles on either separately colored team.
-If a turtle hits the boarder, the turtle should have it's color changed to the color red and should teleport back to the center of the screen at (0,0) and continue moving with random movements.
-Every turtle should have a speed of 1.5
-All of the turtles should continue randomly moving and preforming the code above UNTIL 1500 time units have gone by.
Answer-
-Make 4 turtles that are YELLOW, and 4 turtles that are GREEN. Create these turtles using one or more lists.
Firstly you have to use the color() method of turtle module for changing color of turtle.
Now use color() method and take color name as argument and then write the whole code
import turtle
#create first turtle
my_turtle1 = turtle. Turtle()
my_turtle1.shape("turtle")
my_turtle1.color('yellow') #specify turtle color
(write this three more times)
#create second turtle
my_turtle2 = turtle. Turtle()
my_turtle2.shape("turtle")
my_turtle2.color('green') #specify turtle color
(write 3 more times)
turtle.done
-Make the yellow turtles start at a different randomized location somewhere on the left side of the screen within the range of (-100,100) and (0, -100)
import turtle
#create first turtle
my_turtle1 = turtle. Turtle
my_turtle1.setposition(-100,100)
my_turtle1.shape("turtle")
my_turtle1.color('yellow')
my_turtle1.left(150)
#create second turtle
my_turtle2 = turtle. Turtle()
my_turtle2.setposition(0, -100)
my_turtle2.shape("turtle")
my_turtle2.color("yellow")
my_turtle2.left(150)
turtle.done()
-Make the green turtles start at a different randomized location somewhere on the right side of the screen within the range of (0,100) and (100, -100)
import turtle
#create first turtle
my_turtle1 = turtle. Turtle
my_turtle1.setposition(0,100)
my_turtle1.shape("turtle")
my_turtle1.color('green')
my_turtle1.right(150)
#create second turtle
my_turtle2 = turtle. Turtle()
my_turtle2.setposition(100, -100)
my_turtle2.shape("turtle")
my_turtle2.color("green")
my_turtle2.right(150)
turtle.done()
-Both the yellow and green turtles should be moving
randomly.
import turtle
import random
wn = turtle. Screen()
t = turtle. Turtle()
t.color("yellow")
for i in range (60)
t.pensize(random.randint(5,15))
t.goto(random.randint(-200,200), random.randint(-100,100))
if i = = 30:
t.color("green")
wn.mainloop()
-Create a boundary for the turtle game using a circle with a 500 unit radius using an invisible turtle- i.e.: it does not count as one of the turtles on either separately colored team.
import turtle
my_turtle = turtle. Turtle()
my_turtle.circle(500)
my_turtle.hideturtle() #hide the turtle
turtle.done()
-Every turtle should have a speed of 1.5
import turtle
turtle.speed(1.5)