Line 186: |
Line 186: |
| * [http://tonyforster.blogspot.com/2010/10/turtle-arduino-digital-write.html Arduino write output] [[File:Turtle Art Activity ardout.ta]] [[File:Tamyblock mouse.odt]] | | * [http://tonyforster.blogspot.com/2010/10/turtle-arduino-digital-write.html Arduino write output] [[File:Turtle Art Activity ardout.ta]] [[File:Tamyblock mouse.odt]] |
| | | |
− | == Modifying Turtle Art ==
| + | {{:Activities/TurtleArt/Under_the_hood}} |
− | | |
− | Turtle Art is under the MIT license. You are free to use it and learn with it. You are also encourage to modify it to suit your needs or just for a further opportunity to learn.
| |
− | | |
− | Much of the motivation behind the Version 83 refactoring of the code was to make it easier for you to make changes. Most changes can be confined to two modules: taconstants.py and talogo.py. The former defines the blocks and palettes; the latter defines what code is executed by a block.
| |
− | | |
− | '''Note:''' As of Version 106, there is also support for plugins. If you can use the plugin mechanism to add support for additional devices, e.g., Arduino, or for making modifications such as are described below without making changes to the standard code base. (The advantage to the latter is that your changes will remain intact even after you upgrade to a newer version.)
| |
− | | |
− | taconstants.py contains the constants that by-in-large determine the behavior of Turtle Art. Notably, the block palettes are defined below. If you want to add a new block to Turtle Art, it is generally a matter of modifying some tables below and then adding the primitive to talogo.py. For example, if we want to add a new turtle command, 'uturn', we'd make the following changes:
| |
− | | |
− | (1) We'd add 'uturn' to the PALETTES list of lists:
| |
− | | |
− | PALETTES = [['forward', 'back', 'clean', 'left', 'right', 'show',
| |
− | 'seth', 'setxy', 'heading', 'xcor', 'ycor', 'setscale',
| |
− | 'arc', 'scale', 'left', 'top', 'right', 'bottom', 'uturn'],
| |
− | ['penup','pendown', 'setpensize', 'fillscreen', 'pensize',...
| |
− | | |
− | (2) Then we'd add it to one of the block-style definitions. Since it takes
| |
− | no arguments, we'd add it here:
| |
− | | |
− | BASIC_STYLE = ['clean', 'penup', 'pendown', 'stack1', 'stack2', 'vspace',
| |
− | 'hideblocks', 'showblocks', 'clearheap', 'printheap', 'kbinput', 'uturn']
| |
− | | |
− | (3) Then we give it a name (Note the syntax _('string to be
| |
− | translated') used by the language-internationalization system; also
| |
− | note that the name is an array, as some blocks contain multiple
| |
− | strings.):
| |
− | | |
− | BLOCK_NAMES = {
| |
− | ...
| |
− | 'uturn':[_('u-turn')],
| |
− | ...
| |
− | }
| |
− | | |
− | (4) and a help-menu entry:
| |
− | | |
− | HELP_STRINGS = {
| |
− | ...
| |
− | 'uturn':_('change the heading of the turtle 180 degrees'),
| |
− | ...
| |
− | }
| |
− | | |
− | (5) Next, we need to define it as a primitive for the Logo command
| |
− | parser (generally just the same name):
| |
− | | |
− | PRIMITIVES = {
| |
− | ...
| |
− | 'uturn':'uturn',
| |
− | ...
| |
− | }
| |
− | | |
− | (6) Since there are no default arguments, we don't need to do anything
| |
− | else here. But we do need to define the actual function in talogo.py
| |
− | | |
− | DEFPRIM = {
| |
− | ...
| |
− | 'uturn':[0, lambda self: self.tw.canvas.seth(self.tw.canvas.heading+180)],
| |
− | ...
| |
− | }
| |
− | | |
− | That's it. When you next run Turtle Art, you will have a 'uturn' block
| |
− | on the Turtle Palette.
| |
− | | |
− | [[Image:TAuturn.png|300px]]
| |
− | | |
− | Adding a new palette is simply a matter of: (1) adding an additional entry to PALETTE_NAMES; (2) new list of blocks to PALETTES; and (3) an additional entry in COLORS. However you will have to: (4) create icons for the palette-selector buttons. These are kept in the icons subdirectory. You need two icons: yourpalettenameoff.svg and yourpalettenameon.svg, where yourpalettename is the same string as the entry you added to the PALETTE_NAMES list. Note that the icons should be the same size (55x55) as the others. This is the default icon size for Sugar toolbars.
| |