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. |
| | | |
− | [[Image:TAMyblock.svg]]
| + | Examples: |
| | | |
− | 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. |
| + | # |
| + | ########################################################################### |
| | | |
− | def myblock(lc,x): | + | try: # make sure x is a number |
− | # draw a dotted line of length x
| + | x = float(x) |
− | # make sure x is a number
| + | except ValueError: |
− | if type(x) != int and type(x) != float:
| |
| return | | return |
− | dist = 0
| + | |
− | # save current turtle pen state | + | if lc.tw.canvas.pendown: |
− | pen = lc.tw.turtle.pendown
| + | dist = 0 |
− | # repeat drawing dots
| + | while dist+lc.tw.canvas.pensize < x: # repeat drawing dots |
− | while dist+lc.tw.turtle.pensize < x:
| + | lc.tw.canvas.setpen(True) |
− | setpen(lc.tw.turtle, True)
| + | lc.tw.canvas.forward(1) |
− | forward(lc.tw.turtle, 1)
| + | lc.tw.canvas.setpen(False) |
− | setpen(lc.tw.turtle, False)
| + | lc.tw.canvas.forward((lc.tw.canvas.pensize*2)-1) |
− | forward(lc.tw.turtle, (lc.tw.turtle.pensize*2)-1)
| + | dist += (lc.tw.canvas.pensize*2) |
− | dist += (lc.tw.turtle.pensize*2)
| + | lc.tw.canvas.forward(x-dist) # make sure we have moved exactly x |
− | # make sure we have moved exactly x
| + | lc.tw.canvas.setpen(True) |
− | forward(lc.tw.turtle, x-dist)
| + | else: |
− | # restore pen state | + | lc.tw.canvas.forward(x) |
− | setpen(lc.tw.turtle, pen)
| + | return |
− | 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 | + | ########################################################################### |
| + | # |
| + | # 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() |
− | # push result onto heap (use the pop block to use the new string)
| |
| lc.heap.append(X) | | lc.heap.append(X) |
− | return | + | 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. |
| + | # |
| + | ########################################################################### |
| | | |
− | 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_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 | + | return |
| + | |
| + | def myblock(lc, x): |
| + | ########################################################################### |
| + | # |
| + | # Add a third dimension (gray) to the color model. |
| + | # |
| + | ########################################################################### |
| | | |
− | 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] | | 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: | | if x != 100: |
| x = int(x)%100 | | x = int(x)%100 |
− | # mix in gray
| |
| 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(r<<8,g<<8,b<<8) | + | rgb = "#%02x%02x%02x" % (r,g,b) |
− | return
| + | lc.tw.fgcolor = lc.tw.cm.alloc_color(rgb) |
| + | return |
| + | |
| + | def myblock(lc, x): |
| + | ########################################################################### |
| + | # |
| + | # Save an image named x to the Sugar Journal. |
| + | # |
| + | ########################################################################### |
| | | |
− | def myblock(lc,x)
| + | lc.tw.save_as_image(str(x)) |
− | # save a screenshot in the journal
| + | return |
− | lc.tw.activity._do_saveimage_cb(lc.tw.activity) | |
− | return
| |
| | | |
| ===From the field=== | | ===From the field=== |