Sunday, April 9, 2017

Turtle in Circle


I use previous code and improve it so the turtle could only move at certain circle area.

import turtle
from random import uniform
import numpy as np

turtle.shape("turtle")
#turtle.speed(1)
x = 0
y = 0

for i in range (1,1000):
    a = uniform (-90,90) #angle
    turtle.left(a)
    d = uniform (-75,75) #distance
    x += d*np.cos(np.pi*a/180)
    y += d*np.sin(np.pi*a/180)
    r = np.sqrt(x*x+y*y)
    if r>40:
        turtle.setx(0)
        turtle.sety(0)
        x = 0
        y = 0
    turtle.forward(d)

turtle.exitonclick()



 

.