Chapter 23 of Getting Started with Extended Color BASIC for the Color Computer introduces us to some cool low resolution commands, namely, SET, RESET, and POINT, that, along with how to read the joysticks and their fire buttons.
The SET command will let you place pixels on a low resolution text screen using a resolution of 64 pixels wide, by 32 pixels tall, exactly twice the resolution of the text characters available on the same text screen.
In the example above you see the 32*16 text screen takes each text cell, in breaks into a grid of 4 pixels. Within each of these cells, you can have one of the 8 foreground colors and black, but you can not put two different colored pixels next to each other in the same cell. So for example pixel 0 and pixel 1 could not be red and blue, they both occupy the same cell, so they could be red or black, or blue and black but not red and blue.
The command to SET a pixel is SET (X,Y,C), where X is the horizontal axis from 0-63, Y is the vertical axis from 0-32, and C is the color from 1-8.
RESET (X,Y) will set the pixel to the default black or “off” color (value 0), and POINT (X,Y) will tell you the current color of a given pixel on the screen by it’s X and Y coordinate. This is useful if you want to enable collision detection in a game, by determining if a pixel is on, and what color it is.
A demo showing how to randomly plot X and Y coordinates with a random color was created to illustrate the concepts called “RNDSET.BAS” and will be on the setjoystk disk image available from this blog post.
From learning how to SET, RESET and get the color via POINT, we move on the the JOYSTK command which reads the X and Y coordinates of both joysticks, each of which range from 0-63. Reading a joystick is pretty straight forward using these commands, yet determining when a joystick button is pressed requires us to use the PEEK command to read a value from memory.
To read the Right Joystick, you say X=JOYSTK(0) to get the horizontal axis value, and Y=JOYSTK(1) to get the vertical axis. To read the left joystick, it’s H=JOYSTIK(2) and V=JOYSTK(3).
Reading the left and right fire buttons requires us to PEEK into memory location 65280. If both buttons are idle, the value should read either 255 or 127.
If we press the right fire button, a PEEK into 65280 should reveal a 126 or 254, and the left fire button should reveal a 125 or 253.
An example of the code would be:
10 P=PEEK(65280) 20 IF P=255 OR P=127 THEN PRINT "NOTHING IS BEING PRESSED" 30 IF P=126 OR P=254 THEN PRINT "THE RIGHT BUTTON IS BEING PRESSED" 40 IF P=125 OR P=253 THEN PRINT "THE LEFT BUTTON IS BEING PRESSED" 50 GOTO 10
Running that sample program will let you know when a button is pressed or not, if you hold the button down, you’ll keep getting the same message.
I created a custom demo called JOYPRINT which allows you to move the joystick around, and then prints on the screen the X and Y coordinates, my version was a slight improvement over the book’s version, because the location where the numbers get printed moves to match the joystick coordinates, so it’s a little more visual and responsive. I also check for the left and right buttons, and if you press either, it will print “LB” or “RB” in reverse text and make a brief sound. This program was just updated, called “JOYPRNT2” to monitor and display both joysticks in the same fashion.
The book also showed us how to create a very simple paint demo, where you can move the joystick around and color the screen depending on where the the coordinates were. I took the opportunity to make a slightly more fancy version of this demo where you could actually change the color you wanted to paint with, by pressing numbers 1-8 to chose one of the available 8 colors, and if you pressed 9, it would change the color back to zero, and allow you to move to a new location without painting, effectively “picking up” your brush and moving it somewhere else on the canvas.
The first version of this program called “JOYPAINT” employed a crude method of detecting the current screen color with POINT, capturing that, setting a pixel to show your location, and then setting back the original color captured by the POINT command, in an attempt to create a “flashing cursor” to show your current screen location.
This was flawed due to the fact that each pixel location is part of a 4 pixel grid or text cell, and trying to set on pixel for the “flashy cursor” and even resetting the color back to the original often disrupted the neighboring pixels within the cell.
Not happy with this, I updated the program logic to not use SET/POINT or RESET to “flash my cursor” I used PEEK and POKE, took the X/Y coordinates of the screen, translated them to their TEXT coordinates, and offset that by location 1024 which is where the text screen resides in the CoCo’s RAM, and used PEEK to capture what complete cell object was on that screen, POKED in a reverse asterisks as my cursor, paused briefly, then POKED back the original background object, and viola! Flashy cursor achieved! The revised joystick demo is called “JOYPNT2”
All of these demos are on my latest disk image which you can download by clicking setjoystk disk image extracting the ZIP file, and mounting the DSK image in a real CoCo or emulator.