From c7fa5dca9957c90d122b4d28e1cc0e9b1472c761 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Tue, 25 Jun 2013 19:29:51 +0000 Subject: turtle-centric --- diff --git a/pysamples/forward_push.py b/pysamples/forward_push.py index b98c726..ca527db 100644 --- a/pysamples/forward_push.py +++ b/pysamples/forward_push.py @@ -1,10 +1,13 @@ -#Copyright (c) 2012, Walter Bender +#Copyright (c) 2012-2013, 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. +# this code is run, a new block will be added to the Turtle Palette. +# This block will be named 'name', the value of the argument block +# docked to the Python block. +# +# The new block will cause the turtle to draw a line of the +# length. But before drawing the line, it pushes the rgb values of the +# destination to the FILO. def myblock(tw, name): @@ -15,16 +18,16 @@ def myblock(tw, name): 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() + penstatus = tw.turtles.get_active_turtle().get_pen_status() + tw.turtles.get_active_turtle().set_pen_state(False) + tw.turtles.get_active_turtle().forward(line_length) + r, g, b, a = tw.turtles.get_active_turtle().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) + tw.turtles.get_active_turtle().forward(-line_length) + tw.turtles.get_active_turtle().set_pen_state(penstatus) + tw.turtles.get_active_turtle().forward(line_length) return from TurtleArt.tapalette import make_palette, palette_name_to_index diff --git a/pysamples/uturn.py b/pysamples/uturn.py index cdeb96d..fee9a29 100644 --- a/pysamples/uturn.py +++ b/pysamples/uturn.py @@ -1,4 +1,4 @@ -#Copyright (c) 2011, Walter Bender +#Copyright (c) 2011-2013, Walter Bender # This procedure is invoked when the user-definable block on the # "extras" palette is selected. @@ -11,6 +11,17 @@ def myblock(tw, arg): ''' Add a uturn block to the 'turtle' palette ''' + # def_prim takes 3 arguments: the primitive name, the number of + # arguments -- 0 in this case -- and the function to call -- in this + # case, we define the _prim_uturn function to set heading += 180. + def _prim_uturn(tw): + value = tw.turtles.get_active_turtle().get_heading() + 180 + tw.turtles.get_active_turtle().set_heading(value) + # We also update the label on the heading block to indicate + # the current heading value + if tw.lc.update_values: + tw.lc.update_label_value('heading', value) + from TurtleArt.tapalette import make_palette, palette_name_to_index from TurtleArt.talogo import primitive_dictionary from gettext import gettext as _ @@ -26,10 +37,14 @@ def myblock(tw, arg): help_string=_('make a uturn')) # Add its primitive to the LogoCode dictionary. - tw.lc.def_prim('uturn', 0, - lambda self: primitive_dictionary['set'] - ('heading', tw.canvas.seth, tw.canvas.heading + 180)) + tw.lc.def_prim('uturn', 0, lambda self: _prim_uturn(tw)) # Regenerate the palette, which will now include the new block. tw.show_toolbar_palette(palette_name_to_index('turtle'), regenerate=True) + + palette.add_block('uturn', # the name of your block + style='basic-style', # the block style + label=_('u turn'), # the label for the block + prim_name='uturn', # code reference (see below) + help_string=_('turns the turtle 180 degrees')) -- cgit v0.9.1