• Aucun résultat trouvé

directions as shown in the following diagram: &

Dans le document Commodore And (Page 35-40)

Figure 1 -

' -y ■ ■

For example, if the joystick is pushed north (up or forward) as shown in the diagram, switch J3 will be closed and all other switches will be open. If the joystick is pushed on the diagonal, the two nearest switches will be closed simultaneously; e.g., Jl and J2 will both be closed if the joystick is pushed to the southwest. Notice that these switches imply only the direction of the joystick and that it is up to the program to keep track of any corresponding screen position or movement.

To see how to make the VIC read these switches, plug in your joystick and enter and run the following short program:

10 DD=37154:P1=37151:P2=37152

20 PRINT"{CLEAR}{DOWN} N E S W FB"

30 GOSUB 100:PRINT"{HOME}";J3;J0;J1;J2;FB 40 GOTO 30

100 REM SUBROUTINE TO READ JOY SWITCHES 110 POKE DD,127:P=PEEK(P2)AND128

CHAPTER ONE

120 J0=-(P=0)

130 POKE DD,255:P=PEEK(P1) 140 J1=-((P AND 8)=0)

150 J2=-((P AND 16)=0) 160 J3=-((P AND 4)=0) 170 FB=-((P AND 32)=0) 180 RETURN

As you run the program you will notice that, whenever you push thejoystick in a particular direction, the corresponding compass direction(s) will change from a zero to a one. Similarly, whenever you push the fire-button, it changes to a one - no matter which direction thejoystick is being pushed at the time.

The reason why and how this short program actually works is beyond both the scope of this article and the interest level of most readers. I will leave it to Commodore to explain more fully whenever they issue their documentation on the joystick. Suffice it to say, it does work!

Make A Sketch

Program 1 is a simple program that illustrates the use of the VIC joystick to draw either lines or graphic characters on the screen in

any color. While the program is running, you may control your artistic endeavors by:

• Hitting an "L" to change from graphics to lines

• Hitting a "C" to start all over again with new options

• Pressing the fire-button to clear the screen

• Hitting any color key to change color

• Hitting any other key to change to its graphic character

Let's review this program line by line to learn how to use the joystick effectively. Lines 140 and 150 define some of the important

VIC locations and parameters as variables. Not only does this save memory by requiring fewer bytes to reference these values later, but it is also faster because the VIC can look up a variable in its variable table much quicker than it can decode that same value expressed as a constant. C is the number of columns on the VIC screen; i.e., 22.R is the number of rows. S is the starting location of screen memory. A is the constant that must be added to a particular screen memory location to get its corresponding color matrix location; i.e., S+A is the start of the color matrix. DD, PI and P2 are the same values as used in the short program above to allow us to read thejoystick. SB

is the location that is POKEd to set the screen and border color combinations. V is the volume location for the sound and SI is one of the sound "voices."

The values in the matrix L%(IJ), as defined in lines 160 and 170, contain the values that, when POKEd to the screen, draw a line at a certain angle. Specifically, L%(I,J) contains these screen poke characters:

Figure 2.

0

1

2 0

\

/

11

2

/

\

These values will be used later when drawing lines on the screen.

Line 180 defines two very useful functions. FNA calculates the screen memory location corresponding to any row (Y-value) and any column (X-value). FNC is the corresponding color matrix location.

Lines 190 to 360 give instructions (if desired) and then ask for the initial values of the various user options, such as color, starting drawing location, character, speed, etc. Line 370 sets the screen and border colors to whatever combination has been specified and then also clears the screen.

Line 390 is the beginning of the main processing loop and GETs any key that is hit. If a key has been hit, lines 400 to 440 change to drawing lines, change color, clear the screen and start over, or change the graphic character being drawn - whatever is appropriate.

If no key were hit, the program would jump to line 450 which is a GOSUB680. This subroutine is essentially the same one used in the short program above to read thejoystick switches.

After returning from thejoystick subroutine, lines 460 to 510 determine the current relative directions, DX and DY, and the new screen coordinates, X and Y. DX is -1, 0, +1 if thejoystick is left, center, or right, respectively. Similarly, DY is -1, 0, or +1 if the joystick

CHAPTER ONE

is up, center, or down. Lines 530 to 560 check and restrict the values of X and Y to values within the screen border.

If the program is drawing lines (i.e., the line flag L has been set to 1), line 570 sets the current poke character, CR, to the appropriate matrix value of L%(DX + 1 ,DY + 1). For example, if thejoystick is being pushed northeast, DX will be +1, and DY will be -1, and the value of L%(2,0) is a poke character of a line going up and to the right.

Lines 580 and 590 POKE the reverse of the current character (CR + 128) at the current screen coordinates, POKE the current color into the corresponding color matrix location, and sound a hi-note for DLjiffies (i.e., a 60th of a second). Lines 610 to 630 POKE the current character (not reversed) at the current screen coordinates, sound a lo-note for another DLjiffies, then turn off the sound. Line 640 loops back to the beginning of the whole process at line 390.

Spacewar

Programs 2 and 3 contain a version of the classic arcade game

"Spacewar" which absolutely requires a joystick in order to maintain both the speed and sense of excitement of the original game. The game is in "hi-res" using the VIC's custom character feature [Home and Educational COMPUTING!, Summer 1981) and is a reasonably close copy of the original game.

In the game, you command a single spaceship (using the joystick, of course) against a kill-crazed, kamikaze Mingon. There are

nine levels of play, ranging from trivial to impossible. You must shoot him (using the fire-button) before he gets you. There are asteroids, black holes, and the sun's gravity to add complications. If you get desperate, you can try a hyperspacejump - but there is no guarantee you will survive.

I will not attempt to describe "Spacewar" on a line-by-line basis. But, as you key it in, you will recognize most of the joystick techniques used in "make a sketch." The code is difficult to follow because of the short variable names and the dearth of REMs - all necessary to enable it to fit in the VIC's 3.5K of memory.

I used one other memory-saving trick that might be of interest.

The program is actually written in two separate parts. Program 2 gives instructions, asks for the various game options, loads the custom character set into hi-memory, and then resets the memory pointers to protect the special character set. Then this part of the program automatically loads and begins execution of the second part, Program 3, which is the actual game. This trick is done in line

2190 of Program 2 which POKEs the keyboard buffer with the commands NEW, LOAD and RUN. When the VIC encounters the END at the end of line 2190, it quits executing Program 2 and then it checks the keyboard buffer for additional commands, which it executes as if they were keyed in directly by you. This enables 7K worth of programming to appear to fit into a 3.5K VIC. You should keep this trick in mind whenever you have a program thatjust will not fit in the VIC's limited memory.

After studying these two programs you should be a joystick master and be able to easily incorporate thejoystick into your own VIC programs. This will make your programs both more interesting and more exciting.

Program 1. Make a Sketch.

100 REM VIC MAKE-A-SKETCH USING JOYSTICK 110 REM BY DAVID MALMBERG

120 REM 43064 VIA MORAGA

130 REM FREMONT, CALIFORNIA 94538

140 C=22 : R=23 : S=7680 : A =30720 : DD=

37154 : Pl=37151 : P2=37152

150 SB=36879 : V=36878 : Sl=V-2 : DIML%(2 ,2)

160 FOR I=0TO2 : FOR J=0TO2 : READ L%(I,J

) : NEXT J,I

170 DATA 77,64,78,93,46,93,78,64,77

180 DEF FNA(Z) = S+(X-l)+C*(Y-l) : DEF FN C(Z) = FNA(Z) + A

190 PRINT"{CLEAR} {REV}MAKE-A-SKETCH{O OFF}" : INPUT"{02 DOWN}INSTRUCTI

ONS N{03 LEFT}";A$

200 IF A$="Y" THEN GOSUB720

210 POKESB,27 : PRINT"{CLEAR}{REV}MAKE-A-SKETCH OPTIONS{OFF}"

220 PRINT"{02 DOWNjENTER BORDER COLOR" :

GOSUB650

230 BR = VAL(A$)-1 : IF BR<0 OR BR>8 THEN 220

240 PRINT"{DOWN}ENTER SCREEN COLOR" : GOS UB650

250 BC = VAL(A$)-1 : IF BC<0 OR BC>8 THEN

CHAPTER ONE , o

Dans le document Commodore And (Page 35-40)