[Tutor] Ongoing trouble with Turtle's end_fill() - caused by abug in turtle.py

Gregor Lingl gregor.lingl at aon.at
Fri Aug 8 20:47:11 CEST 2008


Hi Dick,

just to show you a bit of the versatility of the new turtle module
I've prepared to tiny rectangle-generator program examples.

They intentionally use different programming styles and also
turtle graphics techniques different from the ones you used
to accomplish something similar to what you did, of course
in a very simplified way and without your eleborated user
interface and colormanagement.

-----------
Version 1: procedural, using stamp()
-----------

from turtle import *
from random import random, randint
from time import sleep

MAXLEN = 30
MAXWID = 25

def randomcolor():
    return random(), random(), random()

reset()
title("Python turtle graphics: random rectangle generator")
hideturtle()
resizemode("user")
shape("square")
for cycle in range(randint(3, 5)):
    bgcolor(randomcolor())
    for rect in range(randint(5,10)):
        shapesize(3 + random()*MAXLEN, 3 + random()*MAXWID,
                   randint(3, 10))
        color(randomcolor(), randomcolor())
        stamp()
        sleep(0.5)
    sleep(1)
    clearstamps()


-----------
Version 2: object oriented, uses a list of turtles,
which change their properties (size, color, visibility)
-----------

from turtle import Screen, Turtle
from random import random, randint
from time import sleep

MAXLEN = 30
MAXWID = 25

def randomcolor():
    return random(), random(), random()

class RectGenerator(object):
    def __init__(self):
        self.s = s = Screen()
        s.reset()
        s.title("Python turtle graphics: random rectangle generator")
        self.rectturtles = []
        for n in range(10):
            t = Turtle(visible=False)
            t.hideturtle()
            t.resizemode("user")
            t.shape("square")
            self.rectturtles.append(t)
    def run(self):
        for cycle in range(randint(3, 5)):
            self.s.bgcolor(randomcolor())
            n  = randint(5,10)
            for index in range(n):
                t = self.rectturtles[index]
                t.shapesize(3 + random()*MAXLEN, 3 + random()*MAXWID,
                             randint(3, 10))
                t.color(randomcolor(), randomcolor())
                t.showturtle()
                sleep(0.5)
            sleep(1)
            for t in self.s.turtles():
                t.hideturtle()

if __name__ == "__main__":
    rg = RectGenerator()
    rg.run()


Hope you like it
Gregor


More information about the Tutor mailing list