Activities/Turtle Art: Difference between revisions
| Line 553: | Line 553: | ||
To use the customized block, select the "view source" block from the Extras palette. | To use the customized block, select the "view source" block from the Extras palette. | ||
Examples: | |||
def myblock(lc, x): | |||
########################################################################### | |||
# | |||
# Set rgb color | |||
# | |||
########################################################################### | |||
r = int(x[0]) | |||
while r < 0: | |||
r += 256 | |||
while r > 255: | |||
r -= 256 | |||
g = int(x[1]) | |||
while g < 0: | |||
g += 256 | |||
while g > 255: | |||
g -= 256 | |||
b = int(x[0]) | |||
while b < 0: | |||
b += 256 | |||
while b > 255: | |||
b -= 256 | |||
rgb = "#%02x%02x%02x" % (r,g,b) | |||
lc.tw.fgcolor = lc.tw.cm.alloc_color(rgb) | |||
return | |||
def myblock(lc, x): | |||
########################################################################### | |||
# | |||
# Draw a dotted line of length x. | |||
# | |||
########################################################################### | |||
try: # make sure x is a number | |||
x = float(x) | |||
except ValueError: | |||
return | return | ||
if lc.tw.canvas.pendown: | |||
dist = 0 | |||
while dist+lc.tw.canvas.pensize < x: # repeat drawing dots | |||
lc.tw.canvas.setpen(True) | |||
lc.tw.canvas.forward(1) | |||
lc.tw.canvas.setpen(False) | |||
lc.tw.canvas.forward((lc.tw.canvas.pensize*2)-1) | |||
dist += (lc.tw.canvas.pensize*2) | |||
lc.tw.canvas.forward(x-dist) # make sure we have moved exactly x | |||
lc.tw.canvas.setpen(True) | |||
else: | |||
lc.tw.canvas.forward(x) | |||
return | |||
[[Image:TA-dotted-line.png]] | [[Image:TA-dotted-line.png]] | ||
def myblock(lc,x): | def myblock(lc, x): | ||
# | ########################################################################### | ||
# | |||
# Push an uppercase version of a string onto the heap. | |||
# Use a 'pop' block to use the new string. | |||
# | |||
########################################################################### | |||
if type(x) != str: | if type(x) != str: | ||
X = str(x).upper() | X = str(x).upper() | ||
else: | else: | ||
X = x.upper() | X = x.upper() | ||
lc.heap.append(X) | lc.heap.append(X) | ||
return | |||
def myblock(lc, x): | |||
########################################################################### | |||
# | |||
# Push hours, minutes, seconds onto the FILO. | |||
# Use three 'pop' blocks to retrieve these values. | |||
# Note: because we use a FILO (first in, last out heap), | |||
# the first value you will pop will be seconds. | |||
# | |||
########################################################################### | |||
lc.heap.append(localtime().tm_hour) | lc.heap.append(localtime().tm_hour) | ||
lc.heap.append(localtime().tm_min) | lc.heap.append(localtime().tm_min) | ||
lc.heap.append(localtime().tm_sec) | lc.heap.append(localtime().tm_sec) | ||
return | |||
def myblock(lc, x): | |||
########################################################################### | |||
# | |||
# Add a third dimension (gray) to the color model. | |||
# | |||
########################################################################### | |||
val = 0.3 * lc.tw.rgb[0] + 0.6 * lc.tw.rgb[1] + 0.1 * lc.tw.rgb[2] | val = 0.3 * lc.tw.rgb[0] + 0.6 * lc.tw.rgb[1] + 0.1 * lc.tw.rgb[2] | ||
if x != 100: | if x != 100: | ||
x = int(x)%100 | x = int(x)%100 | ||
r = int((val*(100-x) + lc.tw.rgb[0]*x)/100) | r = int((val*(100-x) + lc.tw.rgb[0]*x)/100) | ||
g = int((val*(100-x) + lc.tw.rgb[1]*x)/100) | g = int((val*(100-x) + lc.tw.rgb[1]*x)/100) | ||
b = int((val*(100-x) + lc.tw.rgb[2]*x)/100) | b = int((val*(100-x) + lc.tw.rgb[2]*x)/100) | ||
# reallocate current color | # reallocate current color | ||
lc.tw.fgcolor = lc.tw.cm.alloc_color( | rgb = "#%02x%02x%02x" % (r,g,b) | ||
lc.tw.fgcolor = lc.tw.cm.alloc_color(rgb) | |||
return | |||
def myblock(lc, x): | |||
########################################################################### | |||
# | |||
# Save an image named x to the Sugar Journal. | |||
# | |||
########################################################################### | |||
lc.tw.save_as_image(str(x)) | |||
return | |||
lc.tw. | |||
===From the field=== | ===From the field=== | ||