turtles slowing down

EricC ec97005 at gmail.com
Tue Jul 19 18:02:35 EDT 2011


Hi,

I am a newbie - I have been teaching myself Python 3 since a few
months ago. My “self assignments” include some purely for fun. Among
them was using the turtle module to have multiple turtles running
around on the screen.

Recently one such fun turtle “project” showed strange behavior that I
cannot understand – while I achieved my goals (multiple turtles
oscillate along random straight lines while changing colors), there
was a steady slowing down of turtles as time goes. Actually they
became so slow it is not even fun to look at.

To trouble shoot the problem by myself, I striped down the codes to
the minimal that reproduces the problem. In appearance as long as it
involves “fd()”, it steadily slows down.

The follows is the striped down version (no multiple turtles, no color
changes):

from turtle import *
import time

class Ball(Turtle):
    def __init__(self):
        Turtle.__init__(self)
        self.stepsize = 1
        self.steps = 100        # number of steps before turning 180
deg
        self.counter = 0        # index for counting steps

    def move(self):
        self.fd(self.stepsize)
        if self.counter >= self.steps:
            self.rt(180)
            self.counter = 0
        else:
            self.counter += 1

def main():
    s = Screen()
    s.clear()
    s.tracer(16, 0)
    b1 = Ball()

    i = 0
    t0 = time.time()
    while True:
        b1.move()
        i += 1
        if i > 2000:
            print(time.time() - t0)     # reports time span of the
interval
            i = 0
            t0 = time.time()

if __name__ == "__main__":
    main()
    mainloop()

A sample output to show the lengthening of the interval:

0.5490000247955322
0.625999927520752
0.7360000610351562
0.8619999885559082
0.9549999237060547
1.0649998188018799
1.2220001220703125
1.4100000858306885
1.3619999885559082
1.56600022315979
1.942000150680542
1.816999912261963
1.8640000820159912
2.113999843597412
2.0209999084472656
2.2710001468658447
2.254999876022339
2.3500001430511475
2.3959999084472656
2.50600004196167
2.5850000381469727
2.7260000705718994
2.803999900817871
2.9600000381469727
3.055000066757202
3.0859999656677246
3.2890000343322754
3.3519999980926514
3.4609999656677246
3.6029999256134033
3.634000062942505
3.758999824523926
3.883999824523926
4.134999990463257
...

Did I do something wrong?

Thank you very much for your time.

Best Regards,

EC






More information about the Python-list mailing list