Save turtle state?

Peter Otten __peter__ at web.de
Wed Jun 18 09:21:38 CEST 2008


Allen wrote:

> I'm using the turtle module in Python.  Is there a way to save the turle
> state at any moment for recursive algorithms to easily return the turtle
> to an earlier point for another branch/etc?

Just copying the turtle seems to work:

import turtle
from copy import copy
def rec(t, n):
    t.forward(n)
    n = (n*4)/5
    u = copy(t)
    u.color("red")
    u.right(45)
    if n > 10:
        rec(u, n)

def main():
    t = turtle.Turtle()
    t.color("green")
    for i in range(12):
        t.left(30)
        rec(t, 50)

main()
raw_input()

Peter



More information about the Python-list mailing list