How do I make the Turtle drawing speed faster?

Question:

How can I make the Python library Turtle, run and draw a square in 1 screen tick?

import turtle as t
import math

def square():
  t.penup()
  t.goto(cord_x_1, cord_y_1)
  t.pendown()
  t.goto(cord_x_2, cord_y_2)
  t.goto(cord_x_3, cord_y_3)
  t.goto(cord_x_4, cord_y_4)
  t.goto(cord_x_1, cord_y_1)
  t.penup()
 
t.speed(0)
theta = 0

print("What do you want for angle Θ?")
radians_theta = math.fmod(float(theta), 360)
sqrt =  math.sqrt((50)**2+(50)**2)

while 2 > 1: 
  radians_theta = math.fmod(float(theta), 360)

  #cord setup
  cord_x_1 = sqrt * math.cos(radians_theta)
  cord_y_1 = sqrt * math.sin(radians_theta)

  cord_x_2 = sqrt * math.cos(radians_theta + math.radians(90))
  cord_y_2 = sqrt * math.sin(radians_theta + math.radians(90))

  cord_x_3 = sqrt * math.cos(radians_theta + math.radians(180))
  cord_y_3 = sqrt * math.sin(radians_theta + math.radians(180))

  cord_x_4 = sqrt * math.cos(radians_theta + math.radians(270))
  cord_y_4 = sqrt * math.sin(radians_theta + math.radians(270))

  #repeat
  t.clear()
  square()
  theta += 30
  theta = theta % 360
  print(theta)

What I’m wanting is to have the turtle draw the rotated square very fast. I tried setting the speed to 0, but that is not fast enough. Does anyone know how I could speed turtle up?

Asked By: Python User

||

Answers:

Certainly. Sometimes the turtle speed is not fast enough and so there is a way to go even faster. The way would be to disable the animation entirely.
t.tracer(0) will disable animation.So put this somewhere near the top of your code. Now at the end of your code you will need t.update() to force an update once the turtle is finished and show it on screen.

Answered By: pmxi

We can probably beat simply adding tracer() and update() by also precomputing the fixed set of images that get drawn:

from turtle import Screen, Turtle, Vec2D
from math import sin, cos, radians, sqrt
from itertools import cycle

def square(cord_1, cord_2, cord_3, cord_4):
    turtle.goto(cord_1)

    turtle.pendown()
    turtle.goto(cord_2)
    turtle.goto(cord_3)
    turtle.goto(cord_4)
    turtle.goto(cord_1)
    turtle.penup()

def draw():
    theta, cord_1, cord_2, cord_3, cord_4 = next(endless_coordinates)
    turtle.clear()
    square(cord_1, cord_2, cord_3, cord_4)
    screen.update()
    print(theta)
    screen.ontimer(draw)

theta = 0
delta = 30
diagonal = sqrt(2 * 50**2)
coordinates = []

for theta in range(0, 360, delta):
    theta_radians = radians(theta)

    cord_1 = diagonal * Vec2D(cos(theta_radians), sin(theta_radians))
    cord_2 = diagonal * Vec2D(cos(theta_radians + radians(90)), sin(theta_radians + radians(90)))
    cord_3 = diagonal * Vec2D(cos(theta_radians + radians(180)), sin(theta_radians + radians(180)))
    cord_4 = diagonal * Vec2D(cos(theta_radians + radians(270)), sin(theta_radians + radians(270)))

    coordinates.append((theta, cord_1, cord_2, cord_3, cord_4))

endless_coordinates = cycle(coordinates)

screen = Screen()
screen.tracer(False)

turtle = Turtle()
turtle.hideturtle()
turtle.penup()

draw()

screen.exitonclick()

We can also eliminate turtle-level drawing by spinning the turtle itself, pushing all the drawing down to the C-level:

from turtle import Screen, Turtle

SQUARE_SIZE = 100
DELTA = 30

CURSOR_SIZE = 20

theta = 0

def draw():
    global theta

    turtle.left(DELTA)
    screen.update()

    theta = (theta + DELTA) % 360
    print(theta)

    screen.ontimer(draw)

screen = Screen()
screen.tracer(False)

turtle = Turtle()
turtle.shape('square')
turtle.fillcolor('white')
turtle.shapesize(SQUARE_SIZE / CURSOR_SIZE)
turtle.penup()

draw()

screen.exitonclick()
Answered By: cdlane