Activities/Turtle Art/Under the hood: Difference between revisions
→Modifying Turtle Art: updated instructions for modifying TA |
|||
| Line 40: | Line 40: | ||
'''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.) | '''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.) | ||
The tabasics.py file 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, you could simply add a block of code to that file or to turtle_block_plugin.py, | |||
which contains additional blocks. (Even better, write your own plugin!!) | |||
( | Adding a new palette is simply a matter of: | ||
palette = make_palette('mypalette', # the name of your palette | |||
colors=["#00FF00", "#00A000"], | |||
help_string=_('Palette of my custom commands')) | |||
For example, if we want to add a new turtle command, 'uturn', we'd use the | |||
add_block method in the Palette class. | |||
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')) | |||
# Next, you need to define what your block will do: | |||
# def_prim takes 3 arguments: the primitive name, the number of | |||
# of arguments, 0 in this case, and the function to call, in this | |||
# case, the canvas function to set the heading. | |||
self.tw.lc.def_prim('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 'mypalette' palette. | |||
You will have to create icons for the palette-selector buttons. These | |||
are kept in the icons subdirectory. You need two icons: | |||
mypaletteoff.svg and mypaletteon.svg, where 'mypalette' is the same | |||
string as the entry you used in instantiating the Palette class. Note | |||
that the icons should be the same size (55x55) as the others. (This is | |||
the default icon size for Sugar toolbars.) | |||