From 09da0eafffdbbe790de5c2f0c0d6dd8d944a8ff2 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Sat, 06 Jul 2013 14:33:49 +0000 Subject: Merge branch 'turtle-centric-2' into merge-work --- (limited to 'pysamples') diff --git a/pysamples/dotted_line.py b/pysamples/dotted_line.py index febd409..c581f83 100644 --- a/pysamples/dotted_line.py +++ b/pysamples/dotted_line.py @@ -1,4 +1,4 @@ -#Copyright (c) 2009-11, Walter Bender +#Copyright (c) 2009-13, Walter Bender # This procedure is invoked when the user-definable block on the "extras" # palette is selected. Some examples of how to use this block are included @@ -52,43 +52,57 @@ # clearscreen(self) tw.canvas.clearscreen() # Note: Clears the screen and resets all turtle and # pen attributes to default values -# setpen(self, flag) tw.canvas.setpen(True) +# +# +# Class Turtles -- useful properties and methods (from within +# tamyblock.py, tw.turtles is the class instance) +# +# self.set_turtle(name) tw.turltes.set_turtle(1) +# Note: Set the current turtle to turtle '1' +# self.get_active_turtle() tw.turltes.get_active_turtle() +# Note: Returns active turtle (See Class Turtle below) +# +# Class Turtle -- useful properties and methods (from within +# tamyblock.py, tw.turtles.get_active_turtle() is the class instance) +# +# set_pen_state(self, flag) +# tw.turtles.get_active_turtle().set_pen_state(True) # Note: True will set the pen "down", enabling drawing; # False will set the pen "up" -# forward(self, n) tw.canvas.forward(100) +# forward(self, n) tw.turtles.get_active_turtle().forward(100) # Note: Move the turtle forward 100 units -# arc(self, a, r) tw.canvas.arc(120, 50) +# arc(self, a, r) tw.turtles.get_active_turtle().arc(120, 50) # Note: Move the turtle along an arc of 120 degrees # (clockwise) and radius of 50 units -# setheading(self, a) tw.canvas.setheading(180) +# set_heading(self, a) tw.turtles.get_active_turtle().set_heading(180) # Note: Set the turtle heading to 180 # (towards the bottom of the screen) -# self.heading tw.canvas.heading +# self.get_heading() tw.turtles.get_active_turtle().get_heading() # Note: The current heading -# setpensize(self, n) tw.canvas.setpensize(25) +# set_pen_size(self, n) tw.turtles.get_active_turtle().set_pen_size(25) # Note: Set the turtle pensize to 25 units -# self.pensize tw.canvas.pensize -# Note: The current pensize -# setcolor(self, c) tw.canvas.color(70) +# self.get_pen_size() tw.turtles.get_active_turtle().get_pen_size() +# Note: The current pen size +# self.set_color(self, c) tw.turtles.get_active_turtle().set_color(70) # Note: Set the pen color to 70 (blue) -# self.color tw.canvas.color +# self.color() tw.turtles.get_active_turtle().get_color() # Note: The current pen color -# setshade(self, s) tw.canvas.shade(50) +# self.set_shade(self, s) tw.turtles.get_active_turtle().set_shade(50) # Note: Set the pen shade to 50 -# self.shade tw.canvas.shade +# self.get_shade() tw.turtles.get_active_turtle().get_shade() # Note: The current pen shade -# fillscreen(self, c, s) tw.canvas.fillscreen(70, 90) +# fillscreen(self, c, s) +# tw.turtles.get_active_turtle().fillscreen(70, 90) # Note: Fill the screen with color 70, shade 90 (light blue) -# setxy(self, x, y) tw.canvas.setxy(100,100) +# self.set_xy(self, (x, y)) +# tw.turtles.get_active_turtle().set_xy((100,100)) # Note: Move the turtle to position (100, 100) -# self.xcor tw.canvas.xcor +# self.get_xy tw.turtles.get_active_turtle().get_xy()[0] # Note: The current x coordinate of the turtle # (scaled to current units) -# self.ycor tw.canvas.ycor +# self.get_xy tw.turtles.get_active_turtle().get_xy()[1] # Note: The current y coordinate of the turtle # (scaled to current units) -# self.set_turtle(name) tw.canvas.set_turtle(1) -# Note: Set the current turtle to turtle '1' # # # Other useful Python functions @@ -117,17 +131,18 @@ def myblock(tw, line_length): line_length = float(line_length) except ValueError: return - if tw.canvas.pendown: + if tw.turtles.get_active_turtle().get_pen_state(): dist = 0 - while dist + tw.canvas.pensize < line_length: # repeat drawing dots - tw.canvas.setpen(True) - tw.canvas.forward(1) - tw.canvas.setpen(False) - tw.canvas.forward((tw.canvas.pensize * 2) - 1) - dist += (tw.canvas.pensize * 2) + pen_size = tw.turtles.get_active_turtle().get_pen_size() + while dist + pen_size < line_length: # repeat drawing dots + tw.turtles.get_active_turtle().set_pen_state(True) + tw.turtles.get_active_turtle().forward(1) + tw.turtles.get_active_turtle().set_pen_state(False) + tw.turtles.get_active_turtle().forward(pen_size * 2 - 1) + dist += pen_size * 2 # make sure we have moved exactly line_length - tw.canvas.forward(line_length - dist) - tw.canvas.setpen(True) + tw.turtles.get_active_turtle().forward(line_length - dist) + tw.turtles.get_active_turtle().set_pen_state(True) else: - tw.canvas.forward(line_length) + tw.turtles.get_active_turtle().forward(line_length) return 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/load_block.py b/pysamples/load_block.py deleted file mode 100644 index a4f680d..0000000 --- a/pysamples/load_block.py +++ /dev/null @@ -1,90 +0,0 @@ -#Copyright (c) 2011,2012 Walter Bender - -# DEPRECATED by load block on extras palette. - -# This procedure is invoked when the user-definable block on the -# "extras" palette is selected. - -# Usage: Import this code into a Python (user-definable) block; when -# this code is run, the turtle will create a block at the current -# location of the turtle. The first argument to the Python block -# should be a string containing the name of the desired -# block. Arguments to the block can be passed by expanding the Python -# block to include up to two additional arguments. Note that the -# turtle is moved to the bottom of the block after it is loaded in -# order position another block to the bottom of the stack. - -# The status of these blocks is set to 'load block' - -# For example, try the following to place forward 100, right 90 on the canvas: -# start -# Python(forward, 100) <-- Python load_block.py expanded to two arguments -# Python(right, 90) <-- Python load_block.py expanded to two arguments - - -def myblock(tw, blkname): - ''' Load a block on to the canvas ''' - - from TurtleArt.tapalette import block_names, block_primitives, \ - special_names, content_blocks - from TurtleArt.tautils import find_group - - def make_block(tw, name, x, y, defaults): - tw._new_block(name, x, y, defaults) - - # Find the block we just created and attach it to a stack. - tw.drag_group = None - spr = tw.sprite_list.find_sprite((x, y)) - if spr is not None: - blk = tw.block_list.spr_to_block(spr) - if blk is not None: - tw.drag_group = find_group(blk) - for b in tw.drag_group: - b.status = 'load block' - tw._snap_to_dock() - - # Disassociate new block from mouse. - tw.drag_group = None - return blk.docks[-1][3] - - def find_block(tw, blkname, x, y, defaults=None): - """ Create a new block. It is a bit more work than just calling - _new_block(). We need to: - (1) translate the label name into the internal block name; - (2) 'dock' the block onto a stack where appropriate; and - (3) disassociate the new block from the mouse. """ - - for name in block_names: - # Translate label name into block/prim name. - if blkname in block_names[name]: - if name in block_primitives and \ - block_primitives[name] == name: - return make_block(tw, name, x, y, defaults) - elif name in content_blocks: - return make_block(tw, name, x, y, defaults) - for name in special_names: - # Translate label name into block/prim name. - if blkname in special_names[name]: - return make_block(tw, name, x, y, defaults) - return -1 - - # Place the block at the active turtle (x, y) and move the turtle - # into position to place the next block in the stack. - x, y = tw.active_turtle.get_xy() - if isinstance(blkname, list): - name = blkname[0] - value = blkname[1:] - dy = int(find_block(tw, name, x, y, value)) - else: - name = blkname - if name == 'delete': - for blk in tw.just_blocks(): - if blk.status == 'load block': - blk.type = 'trash' - blk.spr.hide() - dy = 0 - else: - dy = int(find_block(tw, name, x, y)) - - tw.canvas.ypos -= dy - tw.canvas.move_turtle() 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