0

Please forgive me for being such a noob. I have a class assignment where I need to have my code printed to the terminal as it is executed. This is beyond my current scope, and I can't see a way to find an easy answer. Anything would be greatly appreciated. Thanks again and sorry again.

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()
twasbrillig
  • 17,084
  • 9
  • 43
  • 67
  • Is your program throwing an error, or doing something unexpected? – twasbrillig Nov 13 '14 at 20:57
  • Sounds like his program is running as expected, but he needs additional functionality. In particular, every call to a `turtle` method needs to be printed to the console. I don't think there's an easy way to do this, other than replacing all instances of `t1.fd(x)` with `t1.fd(x); print "t1.fd({})".format(x)` everywhere in the code. – Kevin Nov 13 '14 at 21:07
  • Yes, the code is running fine. I was just wondering if there was a simple command that I was missing to output the code to console as well when they are called by the program. So far I haven't seen anything and it seems I just have a lot more typing ahead of me. Thanks a bunch. – Joshua Baker Nov 14 '14 at 21:11

1 Answers1

0

You can attach a decorator to all public member functions of Turtle, as seen in 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))
Community
  • 1
  • 1
greschd
  • 606
  • 8
  • 19