如何在执行时将python turtle命令输出到终端?

时间:2014-11-13 20:54:51

标签: python-3.x turtle-graphics

请原谅我是这样的菜鸟。我有一个课程作业,我需要在执行时将代码打印到终端。这超出了我目前的范围,我无法找到一种方法来找到一个简单的答案。任何事都会非常感激。再次感谢再次抱歉。

from turtle import *
import random
s1=Screen()
s1.colormode(255)
t1=Turtle()
t1.ht()
t1.speed(0)


def jumpto():
    x=random.randint(-300,300)
    y=random.randint(-300,300)
    t1.penup()
    t1.goto(x,y)
    t1.pendown()

def drawshape():

    shape=random.choice(["circle","shape","star"])

    if shape=="circle":
        t1.begin_fill()
        x=random.randint(25,200)
        t1.circle(x)
        t1.end_fill()

    if shape== "shape":
        x=random.randint(25,200)
        y=random.randint(3,8)
        t1.begin_fill()
        for n in range (y):

            t1.fd(x)
            t1.rt(360/y)
        t1.end_fill()

    if shape=="star":
         x=random.randint(25,200)

        t1.begin_fill()
         for n in range (5):

            t1.fd(x)
            t1.rt(720/5)
        t1.end_fill()


def randomcolor():
    r=random.randint(0,255)
    g=random.randint(0,255)
    b=random.randint(0,255)
    t1.color(r,g,b)
    r=random.randint(0,255)
    g=random.randint(0,255)
    b=random.randint(0,255)
    t1.fillcolor(r,g,b)
def pensize():
    x=random.randint(1,20)
    t1.pensize(x)

for n in range (1000):

    randomcolor()
    pensize()
    drawshape()
    jumpto()

1 个答案:

答案 0 :(得分:0)

您可以将装饰器附加到Turtle的所有公共成员函数,如this post中所示:

import inspect
import types

def decorator(func):
    def inner(*args, **kwargs):
        print('function: ' + func.__name__)
        print('args: ' + repr(args))
        print('kwargs: ' + repr(kwargs) + '\n')
        func(*args, **kwargs)
    return inner

for name, fn in inspect.getmembers(Turtle):
    if isinstance(fn, types.FunctionType):
        if(name[0] != '_'):
            setattr(Turtle, name, decorator(fn))