Thursday, February 16, 2017

Retro Programming in Commodore BASIC 2.0 - Part 2

Okay.  Since the last post, I've added code to process movement.  It's very basic and doesn't handle obstacles yet, but one step at a time.

The lines of code are still there from the last post.  What's been added are:
Lines 40-90: Read the cursor keys, set the direction (DI) and call the movement subroutine.  Pressing 'space' will end the program.
Line 26: Line 26 provides initialization of the movement routines (the variables are explained below), and creates a ball (81) character at (3,3).

Lines 10020 - 10110 make up the movement subroutine.  This subroutine will likely change quite a bit as time goes on.
Line 10020 erases the current ball at the current player position.
Lines 10040 - 10100 set the values of SX and SY for plotting the character, and line 10105 jumps to the display subroutine.

Within the movement handler, the following variables have the following meaning:
UX (Upper X): The highest value for SX; this is the rightmost boundary of player movement.
UY (Upper Y): The highest value for SY; this is the bottommost boundary of player movement.
LX (Lower X): The lowest value for SX; this is the leftmost boundary of player movement.
LY (Lower Y): The lowest value for SY; this is the uppermost boundary of player movement.
DI: This is the movement direction:

  • DI = 1 : Left movement.
  • DI = 2 : Right movement.
  • DI = 3 : Movement up.
  • DI = 4 : Movement down.
The next step will be setting up a maze and configuring non-traversable walls.

Wednesday, February 15, 2017

Retro Programming in Commodore BASIC 2.0 - Part 1

Overview
It's amazing how much rust your programming skills can get for a specific machine and a specific language.  I've been programming Java professionally for about 10 years now, and am going back to my roots to rediscover the simplicity of programming for a machine with only 64k of RAM and direct access to the hardware ... no BIOS, no heavy GUI, just me and the machine.

And it's awesome!

The Program
A game.  That's about as far as I've gotten.  I'm writing this by the seat of my pants, so I'm just going to code.  I know I need screen routines and movement routines, but beyond that, I'm hashing out the details as I go.

Here's what I have so far:

It's not much.  Here's the breakdown:

Lines 10-30 are test code for the subroutines starting at line 10000:
Line 10 clears the screen, sets the cursor color to cyan, and sets the background color and border to blue.
Line 20 calls the subroutine at 10010 to put a ball character at (15, 5) on the screen in white.
Line 25 calls the subroutine at 10010 to put a ball character at (0,0) on the screen in yellow.
Line 30 is a keypress loop to hold up execution.

Line 9999 ends the program before the subroutines can be reached.  This prevents a program that doesn't exit before then from accidentally slipping into the first subroutine.

Line 10000 is just a comment.

Line 10010 contains the first subroutine.  Using 1024 as the start of screen RAM and 55296 as the start of screen color RAM, it uses the values in SX and SY to calculate the positions in screen and screen color RAM for the requested item to be displayed, and POKEs the character code and color code (CH and CO) into screen and screen color RAM.  There is no error checking; values outside of the valid ranges of 0<=SX<=39, 0<=SY<=24, 0<=CH<=255, and 0<=CO<=255 will result in an error or in the case of SX and SY, possibly lock up the computer or corrupt the program.

Next Step
Next, I'll be adding movement routines, first based on cursor keys and then based on joystick.