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:

(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:

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.