Options Library

This is just a short BASIC library I wrote as part of the larger project but since pretty much every game requires some kind of menu at the beginning to choose different options I decided to separate it and document it for the future reuse and I hope you find it useful.

The goal is not to provide a very rich and error proof library but to reduce the amount of mundane and repetitive work so more time can be dedicated to actual game design and game play.

What it does

The library allows for displaying and navigating through multiple options on the screen. It requires the options to be stored in a specific data structure and then it does the rest.



Each option can be either one or multiple choice. For example option Quit just has two possible actions it is either chosen or not. On the other hand we can have multiple choice Options where we can choose between different levels, difficulties etc. On and Off options behave the same way but obviously can only have two options.

We also have full control of the colors, we can define three Foreground/Background sets for

  • Default Foreground/Background
  • Selected Foreground/Background to highlight currently active choice
  • Active Foreground/Background to highlight current position that we are positioned at

Controls

To navigate the Options arrow keys are used and Enter or Space buttons are used to choose the option.

Left and Right Arrow to choose between multiple choice options allowing to wrap around

Up and Down arrow to move between options and also allows to wrap around at the top and the bottom.

Enter or Space to pick the option

Data Structures

With proper initializing data structures we define the appearance and behavior of the Options menu. Before we configure all the variables let’s sketch what we want to achieve. Let’s go for four the following menu:

  1. Choose difficulty between three choices: Easy, Medium and hard
  2. Turn Music On or Off
  3. Jump to Game Instructions
  4. Quit the game

Set sizes

As we see we have four sets of options with various number of choices. We set the Option Counter variable for above example as follows:

OC=4       We have 4 Menu options
OC(1)=3    First one has 3 choices
OC(2)=2    Second one has two choices
OC(3)=1        Third one has only on choice
OC(4)=1    Fourth one also has only one choice

Set Options values

Each option can have 5 attributes set:

OT$    Text to be displayed
OX     X position on the screen
OY     Y position on the screen
OR     Return value that if that particular option will be chosen
OS     Currently selected Choice (for multiple choice options).

Because the system is two dimensional all those attributes are set in the variables in two dimensional arrays. Let’s just look at the implementation of our concrete example: 

OX(1,1)=3:OY(1,1)=10:OT$(1,1)=" EASY ":RV(1,1)=1:OS(1,1)=1
OX(1,2)=15:OY(1,2)=10:OT$(1,2)=" MEDIUM ":RV(1,2)=2
OX(1,3)=27:OY(1,3)=10:OT$(1,3)=" HARD ":RV(1,2)=3
OX(2,1)=14:OY(2,1)=14:OT$(2,1)=" ON ":RV(2,1)=4
OX(2,2)=21:OY(2,2)=14:OT$(2,2)=" OFF ":RV(2,2)=5:OS(2,2)=1
OX(3,1)=13:OY(3,1)=17:OT$(3,1)=" INSTRUCTIONS ":RV(3,1)=6
OX(4,1)=17:OY(4,1)=20:OT$(4,1)=" QUIT ":RV(4,1)=7

In our example we draw choices for each setting horizontally but of course we have full freedom to draw them anywhere on the screen. Note that I only set selected or default values with one and did not set unselected ones with 0 because that is the default value at initialization but it might be a good practice to set them to 0 anyway.

Return value

Since Commander X16 Basic does not support Functions, return values or local/private variables all variables are global, great care has to be taken to make sure that each variable used is unique or at least reused within a narrow scope and not expected to have save value. Therefore I chose RET to contain return value from our options handling procedure and we need to examine after calling it. Of course Commander X16 only distinguishes between the first two characters so RET is internally really only identified as RE but like with every other variable if RE is already used for some other use feel free to change it to something else.

Color settings

As mentioned in the beginning we can set colors for three different states each option might be:

DC     Default Color (Foreground)
DB     Default Background
SC     Selected Color (Foreground)
SB     Selected Background
AC     Active Color (Foreground)
AB     Active Background

Usage

We can use this little library with some minimal preparation. We can of course move things around in any way that fit your design but in principle following sequence is expected:

  • Allocate arrays for attributes
  • Set variables to configure options
  • Run a loop calling the Options display and Processing and check the return value RET

The main program in this demo will of course be just a small part in the beginning of the game but could look like this or could use ON RET GOTO statement or something similar:

30 CLS:C=0
40 GOSUB 10000
50 IF RET=7 THEN END
60 IF RET=4 THEN PRINT"\X13MUSIC IS ON "
70 IF RET=5 THEN PRINT"\X13MUSIC IS OFF"
80 GOTO 40

Obviously the main procedure is located at line 10000 and we simply check the Return value with IF statements. The only new pieces of information are variables C and CM which are used to track our current position within the options system. CM tracks the first dimension so in our case it would go between 1 and 4 and C tracks the second dimension and can its value depend on the size we defined. So in Option 1 we have 3 choices so C can be between 1 and 3.

In our program we started with 0 which tells our program to find the currently selected option and use it as active.


The complete example source code is below:



Link to Video of the above code in action:



Comments

Popular posts from this blog

Commander X16 Premium Keyboard