turtle dump

Peter Otten __peter__ at web.de
Fri Jul 17 08:28:46 CEST 2009


Terry Reedy wrote:

> Terry Reedy wrote:
> 
>>> $ file tmp.ps
>>> tmp.ps: PostScript document text conforming DSC level 3.0, type EPS
>>>
>>> Try changing the file extension from .ps to .eps.
>> 
>> I will. Thank you.
> 
> I tried it. Unfortunately, OOo does not open it correctly. It just
> displays the first three lines of metadate - Title, Creator, Date -- as
> image text. Photoshop does read the image, and does an ok job of
> conversion once anti-aliasing is turned off.

I snatched some code from the module and modified it to save a sample image:

from turtle import *

def switchpen():
    if isdown():
        pu()
    else:
        pd()

def demo2():
    """Demo of some new features."""
    speed(1)
    st()
    pensize(3)
    setheading(towards(0, 0))
    radius = distance(0, 0)/2.0
    rt(90)
    for _ in range(18):
        switchpen()
        circle(radius, 10)
    write("wait a moment...")
    while undobufferentries():
        undo()
    reset()
    lt(90)
    colormode(255)
    laenge = 10
    pencolor("green")
    pensize(3)
    lt(180)
    for i in range(-2, 16):
        if i > 0:
            begin_fill()
            fillcolor(255-15*i, 0, 15*i)
        for _ in range(3):
            fd(laenge)
            lt(120)
        laenge += 10
        lt(15)
        speed((speed()+1)%12)
    end_fill()

    lt(120)
    pu()
    fd(70)
    rt(30)
    pd()
    color("red","yellow")
    speed(0)
    fill(1)
    for _ in range(4):
        circle(50, 90)
        rt(90)
        fd(30)
        rt(90)
    fill(0)
    lt(90)
    pu()
    fd(30)
    pd()
    shape("turtle")

    tri = getturtle()
    tri.resizemode("auto")
    turtle = Turtle()
    turtle.resizemode("auto")
    turtle.shape("turtle")
    turtle.reset()
    turtle.left(90)
    turtle.speed(0)
    turtle.up()
    turtle.goto(280, 40)
    turtle.lt(30)
    turtle.down()
    turtle.speed(6)
    turtle.color("blue","orange")
    turtle.pensize(2)
    tri.speed(6)
    setheading(towards(turtle))
    count = 1
    while tri.distance(turtle) > 4:
        turtle.fd(3.5)
        turtle.lt(0.6)
        tri.setheading(tri.towards(turtle))
        tri.fd(4)
        if count % 20 == 0:
            turtle.stamp()
            tri.stamp()
            switchpen()
        count += 1
    tri.write("CAUGHT! ", font=("Arial", 16, "bold"), align="right")

if __name__ == "__main__":
    demo2()
    Screen().getcanvas().postscript(file="demo2.eps")

I could successfully insert the picture into Writer.

I'm on Kubuntu 9.04 with Python 2.6.2 and OpenOffice 3.0.1.
 
> Hmmm. Crazy idea... in
> 
http://us.pycon.org/media/2009/talkdata/PyCon2009/065/SevenWaysToUseTurtle-
PyCon2007.pdf
> Gregor Lingl says that turtle.py has been ported to pygame and jython as
> back ends. It should be possible to instead send output to a file
> instead of an on-screen canvas. Run a program to screen. When output
> looks right, return with a different parameter to send to file.
> 
> For instance, an OpenDocumentDrawing file (.odd) using odfpy.
> This assumes that there are elements corresponding to each turtle
> command. Or at least that the fit is close enough.

I can see nothing crazy about that.

But still, turtle is an educational tool, its main advantage is that it can 
show beginners how the image is generated.

If you want to generate high-quality graphics easily you need different 
primitives.

http://cairographics.org

has Python bindings, but I don't know if it's available on Windows.

Peter




More information about the Python-list mailing list