The Fluid Brush
Al Baker
This month I got carried away. I had so much fun changing and improving this program that it incorporates several hints on how to use the Atari plus another way to use the joysticks. Before digging into the program, though, let us see what it does.
Type in the program and run it. All but the bottom four lines of the screen turn black. Near the center of the black area is a white dot. Move joystick 0 and you can paint with the dot, just as if it was a brush dipped in white paint. The motion of the dot on the screen is quite slow. This is intentional. If the dot moved too fast, it would be hard to control. As your skill increases, you can speed the dot up.
Hold down the joystick button and move the joystick. Now the dot moves much faster, but it doesn't paint. It erases. You have a paint brush which moves quickly from one area of the screen to another, and yet paints slowly enough to give you complete control!
Unless you are Tom Sawyer, painting with a white brush can be quite boring. Let's change colors. As you probably know, you have three color registers available in graphics mode 7. When the program starts, you are painting with register 1 set to white. To pick another register, press either the 1,2, or 3 key. You are now using this register. But before you can paint, you must choose a color. Type in a color number between 0, for white, and 15, for light gold. Press RETURN. Table 1 lists the 16 color possibilities. Now as you move the joystick, you are painting with this new color.
What's Going On
The program is initialized between lines 1000 and 2030. The beginning location of the dot is position (X = 90, Y = 48); its color, C, is 0; its color register, R, is 1; and its brightness, L, is 10.
Line 1090 opens the keyboard for input. This statement is necessary if you want to read single Ascii characters from the keyboard without using the INPUT statement. The number 1 is my choice for the file number. I could have chosen anything between 1 and 5. The 4 means input, the 0 is required, and the "K:" means the input is from the keyboard.
Look at Diagram 1. The joystick returns numbers between 5 and 15 depending on its position. The actual number is used as the subscript of arrays XD and YD to determine how the X and Y positions of the dot on the screen are to be changed. For example, if the joystick is pushed away from the user and to the right, the number is 6. XD(6) is equal to 1 and YD(6) equals -1. The dot would move one position right ( + 1) and one position up (-1) on the screen. The arrays XD and YD are initialized in lines 1110 to 2030.
The main program loop starts at line 150. Look closely at line 160. This statement determines the speed of the dot on the screen. If the button on joystick zero is pushed, STRIG(0) = 0 and S will be 0. If the button isn't pressed, STRIG = 1 and S = 100. Line 170 uses the variable S as the alarm on the delay timer. Finally, lines 150 and 180 cause the dot to blink. Line 200 makes sure that if the button is pressed, then the dot erases, or leaves a black spot, when the joystick is moved.
The rest of the program loop is between lines 250 and 310. Line 250 gets the value of the joystick. This value is used to modify the X and Y positions of the dot as previously discussed. Once these values are computed, line 290 places the dot in its new location.
The statement on line 280 keeps the dot from running off the TV screen. If the PLOT statement tries to put the dot off the screen, an error results. The trap on line 280 branches the computer to the routine at line 3000. That routine adds one to Y if Y is above the screen (Y < 0) or subtracts one from Y if Y is below the screen (Y > 79). Likewise, it adjusts X if X is to the left (X < 0) or right (X > 159) of the screen. Finally, the routine jumps back to line 280 to set the trap again and plot.
The user can type in a new register number and color. Line 300 scans the keyboard each time the program loops to see if the artist is ready to change colors. If location 764 isn't equal to 255, then a key has been pressed. The routine beginning on line 4000 is called on to respond to the artist's request.
The first thing done by the keyboard routine is get the Ascii value of the key pressed. The GET statement must use the same number as the open statement on line 1090 and it puts the value of the key in the variable R. The Ascii value for a one is 49 and for a three is 51. If the key is not between these two, it is ignored and another is required. Once a proper key has been pressed, line 4020 converts it into the numbers 1, 2, or 3 and line 4030 sets plotting to that color register.
The POSITION statement does not control the location of print statements in the text window when graphics modes 1 thorugh 8 are chosen. This is done by poking values into memory locations 656 and 657. Poking a number between 0 and 3 into location 656 will position a statement vertically on the bottom four text lines. Poking a number between 0 and 39 into location 657 will position a print statement between columns 0 and 39 horizontally on the screen. Line 4040 positions the next print statement on the third line of the text area at the bottom of the screen.
Since the print statement on line 4050 is always printed in the same location, it is necessary to erase any previous answers. This is done by including four spaces followed by four back-arrows after the word COLOR. To insert a back-arrow, or any arrow, in a print statement, press the ESC key before typing the arrow key.
Conclusion
I would like to thank Dick Ainsworth for his idea about using two different speeds on the joystick to control different functions, and I'd also like to thank William Bailey for his idea on using arrays to simplify the conversion of joystick values into directions. If you would like to share your ideas with other readers, send them in. If I use them, you will also be acknowledged.
Al, Baker, Programming Director, The Image Producers, Inc.
Table 1: The Atari Colors | |
NUMBER | COLOR |
0 | Gray |
1 | Gold |
2 | Orange |
3 | Red |
4 | Pink |
5 | Violet |
6 | Purple |
7 | Light blue |
8 | Blue |
9 | Gray blue |
10 | Turquoise |
11 | Olive green |
12 | Green |
13 | Yellow green |
14 | Brown |
15 | Light gold |
Diagram 1: The Joystick Control Arrays
CHANGE IN X -- XD XD(10) =-1 XD(14) = 0 XD(6) =1 XD(11) =-1 XD(15) = 0 XD(7) =1 XD(9) =-1 XD(13) = 0 XD(5) =1
CHANGE IN Y -- YD YD(10) =-1 YD(14) =-1 YD(6) =-1 YD(11) =0 YD(15) =0 YD(7) =0 YD(9) = 1 YD(13) =1 YD(5) =1
10 REM THE FLUID BRUSH 20 REM 30 REM 40 REM GO SET UP CONDITIONS 50 REM 60 GOSUB 1000 120 REM 130 REM GET BUTTON FOR SPEED 140 REM 150 COLOR 4: PLOT X, Y 160 S = 100*STRIG(0) 170 FOR I=1 TO S:NEXT I 180 COLOR R: PLOT X, Y 190 REM 200 IF S = 0 THEN COLOR 4: PLOT X, Y: COLOR R 220 REM 230 REM MOVE DOT IF JOYSTICK MOVED 240 REM 250 J = STICK (0) 260 Y = Y+YD(J) 270 X = X+XD(J) 280 TRAP 3000 290 PLOT X, Y 300 IF PEEK(764)<>255 THEN GOSUB 4000 310 GOTO 150 970 REM 980 REM SET UP CONDITIONS 990 REM 1000 GRAPHICS 7 1010 X = 90 1020 Y = 48 1030 C = 0 1040 L = 10 1050 R = 1 1060 SETCOLOR R-1, C, L 1070 COLOR 1 1080 PLOT X, Y 1090 OPEN #1, 4, 0, "K:" 1100 DIM XD(15), YD(15) 1110 FOR I=1 TO 15 1120 READ N:XD(I) = N 1130 READ N:YD(I) = N 1140 NEXT I 1150 RETURN 2000 DATA 0, 0, 0, 0, 0, 0, 0, 0 2010 DATA 1, 1, 1, -1, 1, 0, 0, 0 2020 DATA -1, 1, -1, -1, -1, 0, 0, 0 2030 DATA 0, 1, 0, -1, 0, 0 2970 REM 2980 REM CATCH MOTION OFF THE SCREEN 2990 REM 3000 Y=Y+(Y<0)-(Y>79) 3010 X=X+(X<0)-(X>159) 3020 GOTO 280 3970 REM 3980 REM CHANGE COLOR 3990 REM 4000 GET #1, R 4010 IF (R<49) + (R>51) THEN 4000 4020 R=R-48 4030 COLOR R 4040 POKE 656, 2 4050 PRINT "COLOR ←←←←"; 4060 INPUT C 4070 SETCOLOR R-1, C, L 4080 RETURN