diff options
author | Walter Bender <walter.bender@gmail.com> | 2012-09-02 03:12:09 (GMT) |
---|---|---|
committer | Walter Bender <walter.bender@gmail.com> | 2012-09-02 03:12:09 (GMT) |
commit | 97349bd34552054406d3c788ce6ecb8144b4a6ee (patch) | |
tree | 58daefeb00df9f5bdc79afb5912daa1c2c407c8d /pysamples | |
parent | 5bc53fb5a2ee97961ade0fcf5fa02c5b485142b7 (diff) |
useful gaming block example
Diffstat (limited to 'pysamples')
-rw-r--r-- | pysamples/forward_push.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/pysamples/forward_push.py b/pysamples/forward_push.py new file mode 100644 index 0000000..f0083b2 --- /dev/null +++ b/pysamples/forward_push.py @@ -0,0 +1,53 @@ +#Copyright (c) 2012, Walter Bender + +# Usage: Import this code into a Python (user-definable) block; when +# this code is run, the turtle will draw a line of the length of the +# numeric argument block docked to the Python block. But before +# drawing the line, it pushes the rgb values of the destination to the +# FILO. + + +def myblock(tw, name): + ''' ''' + + def _prim_forward_push(tw, line_length): + try: # make sure line_length is a number + line_length = float(line_length) + except ValueError: + return + penstatus = tw.canvas.pendown + tw.canvas.setpen(False) + tw.canvas.forward(line_length) + r, g, b, a = tw.canvas.get_pixel() + tw.lc.heap.append(b) + tw.lc.heap.append(g) + tw.lc.heap.append(r) + tw.canvas.forward(-line_length) + tw.canvas.setpen(penstatus) + tw.canvas.forward(line_length) + return + + from TurtleArt.tapalette import make_palette, palette_name_to_index + from TurtleArt.talogo import primitive_dictionary + from gettext import gettext as _ + + # Choose a palette for the new block. + palette = make_palette('turtle') + + primitive_dictionary['forwardpush'] = _prim_forward_push + + # Create a new block prototype. + palette.add_block('forwardpush', + style='basic-style-1arg', + label=name, + default=100, + prim_name='forwardpush', + help_string=_('push destination rgb value to heap')) + + # Add its primitive to the LogoCode dictionary. + tw.lc.def_prim('forwardpush', 1, + lambda self, x: primitive_dictionary['forwardpush'](tw, x)) + + # Regenerate the palette, which will now include the new block. + tw.show_toolbar_palette(palette_name_to_index('turtle'), + regenerate=True) |