Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to speed up python's 'turtle' function and stop it freezing at the end

I have written a turtle program in python, but there are two problems.

  1. It goes way too slow for larger numbers, I was wonder how I can speed up turtle.
  2. It freezes after it finishes and when clicked on, says 'not responding'

This is my code so far:

import turtle

#Takes user input to decide how many squares are needed
f=int(input("How many squares do you want?"))
c=int(input("What colour would you like? red = 1, blue = 2 and green =3"))
n=int(input("What background colour would you like? red = 1, blue = 2 and green =3"))

i=1

x=65

#Draws the desired number of squares.
while i < f:
    i=i+1
    x=x*1.05
    print ("minimise this window ASAP")
    if c==1:
        turtle.pencolor("red")
    elif c==2:
        turtle.pencolor("blue")
    elif c==3:
        turtle.pencolor("green")
    else:
        turtle.pencolor("black")
    if n==1:
        turtle.fillcolor("red")
    elif n==2:
        turtle.fillcolor("blue")
    elif n==3:
        turtle.fillcolor("green")
    else:
        turtle.fillcolor("white")
    turtle.bk(x)
    turtle.rt(90)
    turtle.bk(x)
    turtle.rt(90)
    turtle.bk(x)
    turtle.rt(90)
    turtle.bk(x)
    turtle.rt(90)
    turtle.up()
    turtle.rt(9)
    turtle.down()

By the way: I am on version 3.2!

like image 772
clickonMe Avatar asked Apr 20 '13 11:04

clickonMe


People also ask

How do you speed up the turtle in Python turtle?

You can speed up or slow down the turtle's animation speed. (Animation controls how quickly the turtle turns and moves forward). Speed settings can be set between 1 (slowest) to 10 (fastest). But if you set the speed to 0, it has a special meaning — turn off animation and go as fast as possible.

How do you finish a turtle Python?

turtle. bye() , aka turtle. Screen(). bye() , closes a turtle graphics window.

Why does turtle crash Python?

It's probably a problem with the graphics backend. The turtle module's docs says it uses the tkinter module and you need a version of Python installed with Tk support to make it work.


3 Answers

  1. Set turtle.speed() to fastest.
  2. Use the turtle.mainloop() functionality to do work without screen refreshes.
  3. Disable screen refreshing with turtle.tracer(0, 0) then at the end do turtle.update()
like image 177
kalgasnik Avatar answered Oct 16 '22 02:10

kalgasnik


Python turtle goes very slowly because screen refreshes are performed after every modification is made to a turtle.

You can disable screen refreshing until all the work is done, then paint the screen, it will eliminate the millisecond delays as the screen furiously tries to update the screen from every turtle change.

For example:

import turtle
import random
import time
screen = turtle.Screen()

turtlepower = []

turtle.tracer(0, 0)
for i in range(1000):
    t = turtle.Turtle()
    t.goto(random.random()*500, random.random()*1000)
    turtlepower.append(t)

for i in range(1000):
    turtle.stamp()

turtle.update()

time.sleep(3)

This code makes a thousand turtles at random locations, and displays the picture in about 200 milliseconds.

Had you not disabled screen refreshing with turtle.tracer(0, 0) command, it would have taken several minutes as it tries to refresh the screen 3000 times.

https://docs.python.org/2/library/turtle.html#turtle.delay

like image 28
Eric Leschinski Avatar answered Oct 16 '22 03:10

Eric Leschinski


For reference, turtle being slow is an existing problem. Even with speed set to max, turtle can take quite a long time on things like fractals. Nick ODell reimplemented turtle for speed here: Hide Turtle Window?

import math

class UndrawnTurtle():
def __init__(self):
    self.x, self.y, self.angle = 0.0, 0.0, 0.0
    self.pointsVisited = []
    self._visit()

def position(self):
    return self.x, self.y

def xcor(self):
    return self.x

def ycor(self):
    return self.y

def forward(self, distance):
    angle_radians = math.radians(self.angle)

    self.x += math.cos(angle_radians) * distance
    self.y += math.sin(angle_radians) * distance

    self._visit()

def backward(self, distance):
    self.forward(-distance)

def right(self, angle):
    self.angle -= angle

def left(self, angle):
    self.angle += angle

def setpos(self, x, y = None):
    """Can be passed either a tuple or two numbers."""
    if y == None:
        self.x = x[0]
        self.y = x[1]
    else:
        self.x = x
        self.y = y
    self._visit()

def _visit(self):
    """Add point to the list of points gone to by the turtle."""
    self.pointsVisited.append(self.position())

# Now for some aliases. Everything that's implemented in this class
# should be aliased the same way as the actual api.
fd = forward
bk = backward
back = backward
rt = right
lt = left
setposition = setpos
goto = setpos
pos = position

ut = UndrawnTurtle()
like image 36
c0d3rman Avatar answered Oct 16 '22 01:10

c0d3rman