Warning

You're browsing the old v2.x documentation. This version is no longer maintained. Click here to go the v3.x documentation.

Step 3.1: Defining variables

Our game will store its state in a couple of variables. Defining variables in XC=BASIC can be a little confusing if you come from CBM BASIC, but don't worry, the differences are easy to understand.

The following commands can define a variable:

  • DIM
  • LET
  • FOR
  • DATA

(Note that the statement x = 1 is in fact a LET statement, although the LET keyword is omitted.)

Variables are defined in compile time. Once the compiler encounters a variable name in one of the above statements, it will check if the variable exists and will create it if not. This variable cannot change its type later.

Unlike in CBM BASIC, where DIM is used to define an array, XC=BASIC's DIM can define a single variable, too. And thus it is a good practice to pre-define variables in the beginning of the program using DIM to avoid confusions later. So let's define our variables like this:

REM -- The playfield, an array of 25 integers
REM -- Note this has already been added in step 2.4
DIM playfield[25]
REM -- Level (1-10)
DIM level!
REM -- Current score
DIM score%
REM -- Highest score of the day
DIM hiscore%
REM -- Game status: 0 = game on, 1 = game lost
DIM game_status!
REM -- How many rows have been cleared (reset above 100)
DIM ttl_rows_cleared!

REM -- Shape of current piece
DIM shape
REM -- Shape number of current piece
DIM shape_no!
REM -- Color of the current piece
DIM shape_color!
REM -- X, Y position and rotation of current piece
DIM piece_x!
DIM piece_y!
DIM piece_r!

REM -- Shape of next piece
DIM nxt_shape
REM -- Shape number of next piece
DIM nxt_shape_no!

Add the above statements to the program and consider a couple of things to note:

  • Variables without a sigil (that is, a symbol appended to the name) are integers. An integer is a 16-bit signed integral type that's value ranges from -32768 to +32767.
  • Variables appended with an exclamation mark (!) are bytes. A byte is a 8-bit unsigned integral type ranging from 0 to 255.
  • Variables appended with a percent sign (%) are of floating point type. This is exactly the same 40-bit floating point type that CBM BASIC uses, actually, floating point calculations are done by CBM BASIC, not XC=BASIC. This also means that they're very slow. The reasons why we use them to store the score are that
    • an integer would overflow at 32768,
    • the only time when the score needs to be updated is when rows are cleared in the playfield. Speed won't be crucial at this point, it will be okay to spend hundreds of CPU cycles just to add up two numbers.

The program will use more variables than those above. We will need temporary variables, loop counters, etc. I suggest we don't pre-define them now, they can get defined later where they're used.

Great! We've defined the variables that we'll need. Let's go ahead and start coding our routines.