text
stringlengths 0
99.6k
|
|---|
Don't be misled by the apparent simplicity of turtle graphics. It is extremely powerful, and allows many remarkable pictures to be drawn, as well as many interesting mathematical explorations to be carried out. It's also a *lot* of fun. As a simple example, here is a word that takes one value from the stack, and draws a square with sides of that length:
|
: SQUARE ( SIDE -- )
|
4 0 DO DUP FD 90 RT LOOP DROP ;
|
To try out this word, type DRAW and then 100 SQUARE. A square of 100 turtle units per side will be drawn.
|
This is obviously a simple example, but the power of turtle graphics, combined with the speed and power of Forth, allow amazingly rich and complex graphics displays to be drawn with words that are barely more complex than our SQUARE definition. As another example, here is a word that trues a truly remarkable curve:
|
: C-CURVE ( SIDE LEVEL )
|
?DUP 0= IF FD EXIT THEN
|
2DUP 1- RECURSE
|
90 RT
|
2DUP 1- RECURSE
|
90 LT 2DROP ;
|
This innocent looking definition draws a real whopper of a curve. To try it out, type DRAW, and then issue the following sequence of commands to orient the turtle:
|
70 BACK CLEARSCREEN FULLSCREEN 90 LEFT 3 10 C-CURVE
|
This type of curve is known as a fractal, which have received a lot of attention recently in the popular press. They have the reputation of being *very* hairy. Note the simplicity of the definition, and note also that Turtle Graphics and recursion are naturals when used together. I hope this brief introduction will encourage you to explore turtle graphics further on your own. There are some other programing examples included on the source disk. Aside from the windowing demo mentioned earlier, there are:
|
Another fractal, the DRAGON curve. This program draws a whole family of Dragons. Access this by typing 137 LOAD.
|
Space-filling curves. The famous HILBERT curve is here, as is the less well know but very pretty and interesting SIERPINKSI curve. To view these curves, type 135 136 THRU. The dragon curve, and both of these curves, are further examples of recursive programming in Forth.
|
There is a simple example of the use of DCOLOR mode. Access this by typing 131 133 THRU.
|
All of the above will run when they are loaded. In addition, there are some examples included which you may play around with interactively. Block 131 contains some words which draw circles. Screen 134 contains some words which use these words to build up more complex designs. First type 131 LOAD, and then 134 LOAD. The words available are:
|
SPINSHRINK Type DRAW SPINSHRINK to see this design.
|
The next two words require you to place a parameter on the stack.
|
SLINKY requires a value on the stack, which determines the radius of the circles drawn.
|
SPINSLINKY requires the same value. (This is a particularly pretty display.)
|
Finally, screen 138 contains three examples which deal with polygons. These are quite famous among turtle graphics users, and they are presented here to show you one way to implement them in Forth, or to introduce you to them, if you haven't met them before. Typing 138 LOAD will compile the following words:
|
POLY This word requires two values on the stack, which specify a side length and an angle. POLY draws closed polygons, and any polygon may be drawn by POLY. For example DRAW 100 90 POLY will draw a square of 100 turtle units.
|
POLYSPI This word requires the same two parameters on the stack as POLY, but POLYSPI draws spiraling polygons. This is easier to see than it is to describe. For example, DRAW 1 90 POLYSPI will draw a spiraling square (or SQUIRAL).
|
INSPI This word also requires two values on the stack, but where POLYSPI increased the side each time, INSPI increases the angle. An incredible variety of shapes can be drawn with both INSPI and POLYSPI. One of my favorite INSPI designs is DRAW 10 1 INSPI . You will no doubt discover others you like, as you try out the effects of different stack values on each of these words.
|
Blazin' Forth also contains words that allow you to manipulate sprites. Blazin' Forth will take care of most of the hardware details for you, but some caution must be exercised. Since you must setup your own sprite data areas, it is possible to garbage the disk buffers with your sprite data. However, with a small amount of care, this should not occur, since there is enough room for 45 sprites without stealing any room from Forth. (A total of 109 sprite images are available.)
|
You should first enter a sprite definition into the Forth dictionary. The method you use to do this is up to you. One possibility is to use C, as follows:
|
CREATE SPRITE1 0 C, 2 C, etc.
|
Or you can use the " word, if the string handlers are loaded into the system:
|
: SPRITE1 "ANCND" etc. ;
|
Note that if you use this second way, then you must add 1 to the address left by " , since the address left by " is the address of the strings count byte, and SETSHAPE , which is the word used to transfer the data to the sprite areas, expects the address of the start of the data on the stack.
|
However you decide to do it, your sprite definitions must be 64 bytes long. There are 63 bytes of spirte display data, while the last byte is used by the system to set the sprites color mode. If the last byte is 0 , then the sprite will be a hires (single color) sprite. If the last byte is anything else, then the sprite will be a multicolored one.
|
Each sprite-shape is assigned a sprite number by the hardware. You must use a slight amount of care in setting your shapes, since it is possible to garbage your disk buffers, or to select an area which the system is already using for something else, such as the color storage for the graphics screen.
|
There are 45 shape numbers which are available to you and won't cause any conflicts anywhere. Shape-numbers from 64 to 94 or perfectly safe, as are shape numbers from 112 to 126. Sprite numbers greater than 126, or between 95 and 111 are not available at anytime.
|
In addition, the sprite shapes from 0 to 64 may be used, but this will cause garbage to be written to the disk buffers. There are two solutions to this problem. One is to make sure you always type EMPTY-BUFFERS after using the sprite words. The other way (and it's probably the easiest in the long run) is to lower the top of memory to hex $C000. (For information on how to do this, please see CONFIGURE.) Note that you may have to pair down the dictionary if you take this second route. In any case, I find it hardly likely you will need more than the 45 sprites already available to you - but if you do need the full 109 shapes, they are available to you for very little extra work.
|
Once you have your shape data safely installed in the dictionary, you are ready to go:
|
SETSHAPE ( addr shape# -- )
|
SETSHAPE takes the address of a sprite image, and a shape# and moves the sprite data to the proper area for the display of that sprite. Note that DRAW must have been executed at least once prior to using this word!
|
S1 S2 S3 S4 S5 S6 S7 S8
|
These words set the current sprite. Executing these words will determine which sprite is affected by other sprite words.
|
SPRITEON ( SHAPE# -- )
|
Takes a shape number from the stack, and sets the current sprite to that shape. It then turns on the sprite. Note that the sprite must be on screen to be seen.Example:
|
S1 65 SPRITEON ( set sprite1 to shape# 65, and turn it on.)
|
One way of using this word for effective animation effects is to use it to shift between sprite images for the same sprite:
|
: SWITCH S1 40 0 DO 65 SPRITE ON 70 SPRITEON LOOP ;
|
(Note that the above example will probably go much to fast for effective animation. It's just an illustration.)
|
SPR-DCOLOR ( c1 c2 -- )
|
Takes two color codes from the stack, and sets the multi-color sprite color registers to these values. Note that all multi colored sprites will share these colors. Sprites not in multicolor mode are unaffected by this command.
|
SPRITECOLOR ( c -- )
|
Takes a color code from the stack, and sets the the current sprite to that color. Example:
|
S2 0 SPRITECOLOR ( set sprite 2 to black)
|
HIDESPRITE
|
Hides the current sprite. Example:
|
S1 HIDESPRITE ( hide sprite 1)
|
SHOWSPRITE
|
Shows the current sprite, if it is on screen. Example:
|
S1 SHOWSPRITE
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.