Activities/Turtle Art-0.86: Difference between revisions

Line 459: Line 459:
  [[0,"repeat",331,158,[null,1,2,null]],[1,["number","4"],417,167,[0,null]],[2,"forward",426,207,[0,3,4]],[3,["number","100"],500,216,[2,null]],[4,"right",426,246,[2,5,null]],[5,["number","90"],500,255,[4,null]],[-1,"turtle",0,0,0,0,50,5]]
  [[0,"repeat",331,158,[null,1,2,null]],[1,["number","4"],417,167,[0,null]],[2,"forward",426,207,[0,3,4]],[3,["number","100"],500,216,[2,null]],[4,"right",426,246,[2,5,null]],[5,["number","90"],500,255,[4,null]],[-1,"turtle",0,0,0,0,50,5]]


==From the field==
==Programmable Brick==
 
The following feature is only available in the [[Activity/TAPortfolio|experimental fork]] of Turtle Art.
 
=== myblock.py ===
 
And a block that can be programmed by the Pippy interface:
 
A copy of the tamyblock.py module is stored in the Journal when you first launch TurtleArt. You can edit the module in Pippy and then import your custom code into Turtle Art using the Pippy button.
 
[[Image:TAPippyButton.svg]]
 
[[Image:TA-pippy.png|300px]]
 
To use the customized block, select the "view source" block from the Sensors palette.
 
[[Image:TAMyblock.svg]]
 
Examples:
 
from taturtle import *
def myblock(lc,x):
# draw a dotted line of length x
    # make sure x is a number
    if type(x) != int and type(x) != float:
        return
    dist = 0
    # save current turtle pen state
    pen = lc.tw.turtle.pendown
    # repeat drawing dots
    while dist+lc.tw.turtle.pensize < x:
        setpen(lc.tw.turtle, True)
        forward(lc.tw.turtle, 1)
        setpen(lc.tw.turtle, False)
        forward(lc.tw.turtle, (lc.tw.turtle.pensize*2)-1)
        dist += (lc.tw.turtle.pensize*2)
    # make sure we have moved exactly x
    forward(lc.tw.turtle, x-dist)
    # restore pen state
    setpen(lc.tw.turtle, pen)
return
 
[[Image:TA-dotted-line.png]]
 
from taturtle import *
def myblock(lc,x):
# push an uppercase version of a string onto the heap
    if type(x) != str:
        X = str(x).upper()
    else:
        X = x.upper()
    # push result onto heap (use the pop block to use the new string)
    lc.heap.append(X)
return
 
from taturtle import *
def myblock(lc,x):
# push hours, minutes, seconds onto the heap
# use three pop blocks to retrieve the values
# remember: the heap is a FILO (first in, last out)
# the first value you will pop will be seconds
    lc.heap.append(localtime().tm_hour)
    lc.heap.append(localtime().tm_min)
    lc.heap.append(localtime().tm_sec)
return
 
from taturtle import *
def myblock(lc,x):
# add a third dimension (gray) to the color model
    # calculate the value (brightness) of the current color
    val = 0.3 * lc.tw.rgb[0] + 0.6 * lc.tw.rgb[1] + 0.1 * lc.tw.rgb[2]
    # make sure gray is in range from 0 to 100
    if x != 100:
        x = int(x)%100
    # mix in gray
    r = int((val*(100-x) + lc.tw.rgb[0]*x)/100)
    g = int((val*(100-x) + lc.tw.rgb[1]*x)/100)
    b = int((val*(100-x) + lc.tw.rgb[2]*x)/100)
    # reallocate current color
    lc.tw.fgcolor = lc.tw.cm.alloc_color(r<<8,g<<8,b<<8)
return
 
 
===From the field===


Tony Forster has written a number of blog posts about his experiments with Turtle Art: [http://tonyforster.blogspot.com/2009/02/using-python-blocks-in-turtleart.html Using Python blocks in TurtleArt] [http://tonyforster.blogspot.com/2009/02/turtle-lander.html Turtle Lander] [http://tonyforster.blogspot.com/2009/01/reprogramming-sugar.html Reprogramming Sugar]
Tony Forster has written a number of blog posts about his experiments with Turtle Art: [http://tonyforster.blogspot.com/2009/02/using-python-blocks-in-turtleart.html Using Python blocks in TurtleArt] [http://tonyforster.blogspot.com/2009/02/turtle-lander.html Turtle Lander] [http://tonyforster.blogspot.com/2009/01/reprogramming-sugar.html Reprogramming Sugar]


[[Category:Activity]]
[[Category:Activity]]