Simple Plotter

View all posts by: Vectronic | View all posts in category: Apple II Software

Listed below is the code for a simple paint program I put together using Applesoft BASIC and the low-resolution graphics built into the Apple IIe. The paint program allows you to draw low-resolution pictures on the screen in 14 different colors. It uses the joystick and keyboard for its inputs. The program could easily be modified to take input from a mouse. I originally designed this program to help me build Apple II video games in low-resolution mode. It is much easier to design a game when you know exactly what the pixels will look like. The video game helper is a much more complex program than this simple paint program. It holds the screen picture in arrays and has a print subroutine to print out the plot points and color numbers to an ImageWriter II printer. These plot points can then be used to build the game without the need to work out on paper how the pixels should be mapped.

The paint program has been scaled down considerably. It has no print subroutine and does not keep track of the pixel plot points and colors. It also lacks a screen reset. You can customize the program anyway you want once you get the logic down. It would even be possible to save the screen to a file if you so desired. I have heard of people who have made text generators using low-resolution graphics. Because the Apple II uses a composite signal, a text generator could be used in conjunction with video production. Of course, this would be silly nowadays considering all the other tools available but back in the 80's, it was a viable option. A text generator could be an extension of this program's basic engine. It would require you to predefine the text and allow alphanumeric inputs to generate them on the screen.

Low-resolution graphics is relatively easy to program compared to the Apple II's high-resolution mode. The Apple II series computers are justly famous for their high-resolution graphics but it is considerably more difficult to use than low-resolution. Techniques used to build low-resolution programs are very similar to programming in high-resolution mode. High-resolution requires a great deal more planning and skill. The Apple II has an odd way of rendering high-resolution while low-resolution is relatively straightforward.

In Applesoft BASIC, the two-letter instruction GR tells the computer to change from TEXT mode to GRaphic mode. The instruction TEXT does the opposite: it tells the computer to leave GRaphic mode and return to TEXT mode. In TEXT mode, the screen displays 24 lines (or rows) of alphanumeric characters in 40 columns across the screen. For my simple paint program, the rows represent the Y-axis and the columns represent the X-axis. In GRaphic mode, the display prints a colored dot instead of a character.

This dot is actually a small square or block, the size of the top half of the famous Apple II flashing cursor. The top 20 rows or lines of the alphanumeric TEXT mode display are converted into 40 rows in GRaphic mode. The smallest point or block you can control in low-resolution mode is half the size of an alphanumeric character. Woz came up with this scheme to give the first Apple II the capability to support his Breakout game. It is an eloquent and simple method to do some graphical programming.

In GR mode, the columns across the screen are numbered from 0 to 39, starting at the left edge of the display. The 40 rows are also numbered from 0 to 39, with the top row being row 0 and bottom being row 39. As mentioned earlier, the top 20 (out of 24) display lines in TEXT mode are converted into the 40 numbered rows on the graphics grid. The bottom 4 lines of the 24-line text display remain in TEXT mode. This scheme allows you to combine text with graphics. This bottom text rows could be used as a scoreboard for games or a selection menu like in my paint program.

Low-resolution mode has 14 colors (16 if you include black, and two grays). These colors are assigned to numbers that the BASIC instruction COLOR= understands. The numbers are assigned as follows:

0 - black
1 - purple/red or magneto
2 - dark
3 - purple
4 - dark green
5 - gray
6- medium blue
7 - light blue
8 - brown
9 - orange
10 - gray
11 - pink
12 - green
13 - yellow
14 - aqua or blue/green
15 - white

My paint program uses all these colors.

If you are familiar with Applesoft BASIC, you would probably agree that it is easy to program with but very frustrating in structure. Each line of the program must be numbered in succession. This simplifies matters for novices but for programmers used to C++ or any other object-oriented language, it is very limiting.

Take a look at the program below. A few things applicable to the Apple II computer need to be clarified.

The program uses a joystick as an input device to move the cursor, which is an orange dot, one pixel in size. Line 150 initializes the variables used to capture the movement of the joystick. PDL returns the current value of the game control specified. PDL is a BASIC function unique to Applesoft BASIC. PDL(0) returns the value of the X-axis (left and right). PDL(1) returns the value of the Y-axis (up and down). My program has a counter FOR-NEXT statement placed between the PDL returns. This slows the computer down to "human speed" and results in a smoother interface. The value returned PDL is an integer between 0 and 255, which can differ based on the joystick you are using. The IF statements in lines 170-200 are calibrated to my joystick. Your joystick may differ and some fine-tuning may be necessary.

PEEK (-16384) reads the keyboard. Line 160 uses a keyboard PEEK. Each time the computer runs this line of instruction, it checks the keyboard for an input. Each key on the Apple II keyboard is assigned a number equal to the ASCII code plus 128. Subroutine 1000 assigns the variable C the color chosen by the user depending on the key pressed. The value of the key pressed is stored in variable K in line 160. If you are interesting in using keyboard inputs in your Apple II programs, find a standard ASCII code chart and add 128 to each value. For example, in ASCII, the number 1 is 49. 49 + 128 = 177. If you notice below, line 1100 basically states that if the user presses the 1 button (which generates a value of 177) then assign C a value of 1.

Instead of looking up the ASCII code and adding 128 to it, you can run a simple program that will print out the number of the key you press. This program will constantly loop and the number printed will change to reflect the key you press. Press Control-C to interrupt the loop and stop the program.

Keyboard Number Checker Program:

100 K = PEEK(-16384)
120 GOTO 100

The keyboard inputs for the paint program are as follows:

1 - purple/red or magneto
2 - dark
3 - purple
4 - dark green
5 - gray
6- medium blue
7 - light blue
8 - brown
9 - orange
Q - pink
W - green
E - yellow
R - aqua or blue/green
T - white
Spacebar - black
Esc - Quit

All the colors available in low-resolution mode (the cursor is the orange dot)

The program will drag any color you choose. To go back and erase or move the cursor without dragging color, press the spacebar. It is a funny way to do it, but it works once you get the hang of it.

The movement of the cursor is controlled in lines 300-340, 400-440, 500-540, and 600-640. The logic is fairly straightforward. It is controlled in the main loop (150-220) with the joystick reads in the IF statements in lines 170-200. The program breaks this loop when the Escape key is pressed (key number 155) and exits to subroutine 3000. The user is given the chance to quit completely or reenter the loop.

The program is easy to put into your Apple II computer and simple to understand. If you have any questions, please feel free to email me.

The Paint Program:

100 HOME : GR
110 X = 20 : Y = 20 : C = 9 : K = 160
120 COLOR = C: PLOT X, Y
130 GOSUB 2000
150 XP%=PDL(0): FOR PX = 1 TO 10 : NEXT: YP%=PDL(1): FOR PY =1 TO 10: NEXT
160 K = PEEK(-16384)
170 IF XP% > 130 THEN 300
180 IF XP% < 120 THEN 400
190 IF YP% > 130 THEN 500
200 IF YP% < 120 THEN 600
210 IF K = 155 THEN GOSUB 3000
220 GOTO 150

300 GOSUB 1000
310 IF X =39 THEN 150
320 X = X + 1: COLOR = C: PLOT X - 1, Y
330 COLOR = 9: PLOT X, Y
340 GOTO 150

400 GOSUB 1000
410 IF X = 0 THEN 150
420 X = X -1: COLOR = C: PLOT X + 1, Y
430 COLOR = 9: PLOT X, Y
440 GOTO 150

500 GOSUB 1000
510 IF Y = 39 THEN 150
520 Y = Y+ 1: COLOR = C: PLOT X, Y-1
530 COLOR = 9: PLOT X, Y
540 GOTO 150

600 GOSUB 1000
610 IF Y = 0 THEN 150
620 Y = Y - 1: COLOR = C: PLOT X, Y + 1
630 COLOR = 9: PLOT X,Y
640 GOTO 150

999 END

1100 IF K = 177 THEN C = 1
1110 IF K = 178 THEN C = 2
1120 IF K = 179 THEN C = 3
1130 IF K = 180 THEN C = 4
1140 IF K = 181 THEN C = 5
1150 IF K = 182 THEN C = 6
1160 IF K = 183 THEN C = 7
1170 IF K = 184 THEN C = 8
1180 IF K = 185 THEN C = 9
1200 IF K = 209 THEN C = 11
1210 IF K = 215 THEN C = 12
1220 IF K = 197 THEN C = 13
1230 IF K = 210 THEN C = 14
1240 IF K = 212 THEN C = 15
1250 IF K = 160 THEN C = 0

2100 PRINT "1=PRD 2=DBL 3=PRL 4=DGR 5=GR 6=MBL
2200 K= PEEK (-16368)

3110 PRINT "TYPE 'Y' OR 'N'"
3200 INPUT N$
3300 IF N$= "Y" THEN 3700
3400 IF N$= "N" THEN 3800
3600 GOTO 3100
3700 TEXT : HOME : GOTO 999
3800 GOSUB 2000

My masterpiece - a Mac Plus drawn on an Apple IIe