Chapter 1 of 9
Give Mars a key
Somebody has to write the part of a game that waits at the title screen. Our whole first game is that part. Give the CoCo a 1 and it greets Earth. Give it a 2 and it greets Mars. By the time it answers, one byte will have crossed from your keyboard into a decision you can find in the source.
Make the first run
- Begin with the Disk BASIC system profile, running at its OK prompt. Save any program you care about before continuing. Direct Run uses the CoCo’s program memory.
- On the Mac choose Develop → C Development…. Click New, choose Hello World, give the new folder a name such as TwoWorlds, and select Create Project. The editor opens main.c. The bundled starter and the copy in your folder have the same starting text. You can also save the complete planet source from this book.
- Leave the configuration on Debug and click Build. Look for Build succeeded. You now have two things you can keep on the Mac. The source file is for a person to read and change. The resulting main.bin contains instructions for the CoCo processor. The CoCo is still at its prompt.
- Click Run at the top of the C window. Confirm Run when the app explains that it will replace program memory. When the question appears on the CoCo, click inside its display and press 1. A single key is enough. Look for Hello Nice Blue Dot World! and then OK.
- Return to the C Development window. Click Run again, confirm and choose 2 at the CoCo. You used the same compiled program twice. Which part did the key change?
If the guest sits at the question, first check where the keyboard is going. Choose Input → Focus CoCo Display and press 1 or 2. If you see a flashing caret in main.c, you are typing into the Mac editor. A freshly created starter has no source breakpoints. You can leave the Run to Breakpoint button at the bottom alone for now.
Read the keystroke in C
int main(void)
{
byte choice;
width(40);
printf("What planet do you live on?\n1) Earth\n2) Mars\n");
do
{
choice = inkey(); /* No Enter needed. Ignore other keys. */
} while (choice != '1' && choice != '2');
main is the function where the program starts its own work. Its braces enclose that work. The parentheses in main(void) tell CMOC the function takes no arguments. The local variable choice holds one byte. The call to width(40) asks the CoCo 3 to switch to a 40-column text display. The double quotes around the question enclose a string, and each \n in that string moves printing to a new line.
Start at do and read downward as the processor would. The call to inkey() checks the keyboard. With no key pressed it returns zero. Otherwise it supplies a character code. The single = stores that result in choice. At while, != means “different from,” and && means both tests must hold. If the value differs from both '1' and '2', execution goes around again. Imagine pressing M. Both comparisons are true, so the loop checks the keyboard again.
The single quotes matter. '1' is the code for the character printed on the key. The number 1 is an integer with a different value. Comparing the returned character to another character lets the program recognize exactly what was typed.
Who chooses the greeting?
Find if (choice == '1') in your copy of main.c. The double equals sign tests whether two values match. Trace the first branch as far as its closing brace. Now put your finger on else and follow the other branch. The earlier loop guaranteed that reaching this second branch means the player chose Mars. Both paths meet again at the final text-attribute call, then return 0; ends the function and control returns to BASIC.
The two lines at the very top beginning with #include bring in declarations that CMOC uses while compiling. The header cmoc.h declares printf, which produced the question and greeting. The header coco.h declares width and inkey, and defines the name byte. These names give you a way to look up what a call expects before you change it. You'll try that on the colors next. If you've already written the same decision in BASIC, trace the two choices in Planet Explorer and compare the moment each program accepts its answer. To follow the key all the way down to the processor, catch the shared greeting in assembly.