Programming The Commodore Joystick
I own a Commodore VIC-20. I have learned how to program games, but I have not learned how to program the games to respond to a joystick.
I have looked in the VIC-20 Programmer's Reference Guide, but I did not understand the information. I would like to know how to program games to respond to a joystick. Would you please explain this to me?
In both the VIC-20 and the Commodore 64, the joystick is programmed by PEEKing two bytes.
The VIC is designed to handle only one joystick, and it takes two bytes to control that joystick. In the VIC, location 37137 is PEEKed to read the joystick for the up, down, left, and fire button movements. Location 37152 is PEEKed to detect movements to the right.
The 64 is designed to handle two joysticks, and unlike the VIC, each joystick is controlled with one byte. To detect the position of a joystick plugged into port A, PEEK 56320, and for port B, PEEK 56321 is used.
To see just how easy it is to detect movement on the joystick, plug in your joystick, type in one of the following short programs, and then RUN. The programs simply PEEK the joystick control bytes, and then PRINT that reading to the screen.
For the VIC-20:
10 PRINT PEEK(37137), PEEK(37152) : GOTO 10
For the Commodore 64:
10 PRINT PEEK(56320), PEEK(56321) : GOTO 10
As you run the programs and play with the joystick, you'll see the values printed to the screen change as the values in the joystick ports change. You can program the joystick into your games by PEEKing these locations, and using IF... THEN statements in your program to process the information.
A quick word about programming techniques here. Because the joystick control bytes are often shared (that is, they do other things besides read the joysticks), it is more efficient to PEEK only the affected bits in the joystick control bytes. This will filter out other information not connected to the joystick. This is done by AND-ing your PEEKs. For more information on which bit is used for each joystick application, consult the Programmer's Refeference Guide for your specific computer.