Classic Computer Magazine Archive ANTIC VOL. 2, NO. 3 / JUNE 1983

STARTING LINE

Dancin' Man

by Steve Englehart and Dave Menconi

You certainly don't need to know anything about computer science to play games on your ATARI, nor to use the many programs already in existence-but eventually you'll want to begin programming your computer. Perhaps there's a special application you need (a pigeon-feeding scheduler, for example), or you have an idea for a great video game.

To make these things happen you'll need to know how to write your own programs. Programs are step-by-step instructions for the computer to follow. And if you're going to write a program, you need a language to write it in. By far the most common language-and probably the easiest -is BASIC (Beginners All-purpose Symbolic Instruction Code). ATARI BASIC comes ready-to-use inside its own cartridge, which you insert in the computer before you begin your programming.

This article will show you a short BASIC program that exercises some of the graphics power of the ATARI, so even if you've used BASIC on other machines you'll still benefit from this example.

SIMPLE ANIMATION

The easiest way to learn anything is to play with it and have fun. We'll start with something simple: making a little stick figure dance around on the screen. The animation is admittedly crude, but we can work on improving it after we understand it.

BASIC programs are written in statements. You can have more than one statement on a line, but each line is given a number so the computer knows the order to process them, and can easily refer to the program's parts.

To make this little man dance, we have to define four parts of the figure's body; the head, the shoulders, the body and the legs. In BASIC we call each of these definitions a string variable, and name them like this: HEAD$, SHLDR$, BODY$, and LEG$. (We could spell out SHOULDER if we wanted to, but abbreviation saves typing.) The "$" is always used at the end of the word to indicate that it is a string variable.

One thing that's not immediately evident on your computer is that the "letter" keys can also be used to put graphics characters on your screen, when the [CTRL] (control) key is held down. These characters, and the keys which control them, are shown on the back cover of the BASIC Reference Manual. See our character table (page 18, this issue) for guidance on typing these characters.

Each of the four body parts is constructed from three graphics characters. The head and body have only one possible shape while the legs and shoulders have four and six different shapes respectively. To make the figure appear to move we draw the head g a randomly selected shoulder, the body, and a randomly selected set of legs-then clear the screen and draw the figure again at a different place.

Be very careful about spaces inside the quotes when you type this program. Everything else is typed just as you see it.

A detailed explanation of the program follows.

10: The REM statement is not processed by BASIC. It is used to put REMARKS in the program that explain how the program works, record the name of the program (as here) or whatever else the programmer wants.

Programmers often number their lines by tens or twenties, so that they can insert lines later, if necessary.

20: A DIM statement tells BASIC to reserve space (a dimension) for the string variables. We measure space in keystrokes-that is, we use three keystrokes to make a head, eighteen to make six different kinds of shoulders, and so on. If we reserve more space than we need, that's fine. If we don't reserve enough, the computer will give us an error message.

40: The GRAPHICS statement selects the way that the picture on your screen is presented. The number that we put after GRAPHICS determines the mode of the picture.

60: This statement makes the cursor invisible. The POKE statement allows us to put (to POKE) a number into a particular memory address. Some addresses are located in the computer's built-in Operating System; they have pre-set numbers which give the computer pre-set orders, but you can change the orders with a POKE. The first number after the POKE is the address (in this case, 752) while the second number is the number we want to put there (1).

80-140: These lines define the possible positions of our dancing man. He has only one head but there are six possible shoulders and four possible legs. Note that each part of the body--the head, the shoulders, the body, and the legs--is three characters long. This insures that our figure's various pieces will line up properly.

160: The numeric variable P determines how far from the left we will draw the figure. By changing P we will make the figure appear to move back and forth.

180: RND(0) is a standard BASIC command. It tells the computer to pick a random number between zero and one. We call that random number R. Then we add one to P if R is less than .5, or subtract one from P if R is greater than .5. In the unlikely event that R is equal to .5, we leave P alone. As P increases and decreases, the figure moves one space to the right or left.

200: Here we select one of the six shoulder combinations from the string in line 100. The first shoulder group begins with the first character and ends with the third. The second group begins with the fourth character, the third with the seventh character, etc. Therefore, we need to randomly select among the numbers 1, 4, 7, 10, 13 or 16 as a starting point for the shoulder group. To do this, we generate a random number between zero and one, and multiply it by six. This gives us a decimal fraction between zero and six. Then we use another standard command, INT, which takes the integer part of that number. This gives us a random whole number between zero and five (do you see why it can't be six?). We multiply this whole number by three, giving 0, 3, 6, 9, 12 or 15, and add one to get the desired number. This whole process sets S randomly to equal 1, 4, 7, 10, 13, or 16. That number is then used to select which of six shoulder shapes to use.

220: In this line L is generated the same way S was, to select one of the four leg shapes (1, 4, 7, or 10).

240: This is another standard command which clears the screen. We ask the computer to PRINT this string so that any figure already drawn will be erased before we draw another one. If you removed this line you would see pieces of the figure slowly scatter across the screen.

250: Lines 250 & 255 check to see if the image's position is exceeding the screen bounds. If this is the case, the image is repositioned at the opposite edge to create "wrap-around" effect.

260: Lines 260, 300, 340, and 380 position the (invisible) cursor for the head, shoulders, body and legs, respectively. P is the always-changing horizontal position, as determined in line 180, while the vertical position is simply increased by one in each of these four lines. Since P only changes by one each time as well, all four parts move together.

280: Lines 280, 320, and 400 cause the head, shoulders, body and legs to be put on the screen where they belong. In the case of the shoulders, the actual shape to be used is part of the SHLDR$ beginning with that random value of S and ending with S + 2 (the end of a three-character set: S, S + 1, S + 2). LEG$ works the same way.

420: This causes another figure to be drawn. All the steps between this line 180 and 420 will be executed over and over until you stop it by pressing the BREAK key.

This seems like a lot of things to take in at once, doesn't it? But the longer you use this program, and watch your little man dance, the more natural these concepts will become-and the sooner you'll be able to use all of your computer.

[code]