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:
!
) are bytes. A byte is a 8-bit unsigned integral type ranging from 0 to 255.%
) 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 thatThe 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.