The playfield is where the gameplay happens. In the TETRIS world, it is often called the Tetrion. It is a 10×20 block surrounded with a frame. The playfield also has an invisible part above its ceiling the top where the pieces start falling.
Since we have encoded the shapes in 16-bit integers it would be useful if we could encode the playfield similarly. Here is how I suggest it:
Each row will have a corresponding 16-bit value whereas each column will be one bit within this value. If we allocate 25 integers, that is 25 rows, we will have space to store the visible and invisible areas as well. Note that the ceiling (painted in grey above) will only be drawn but will not be part of the playfield to allow pieces to fall through it.
Let's define the playfield using the DIM
statement:
REM -- The playfield, an array of 25 integers DIM playfield[25]
Note that unlike CBM BASIC, XC=BASIC uses square brackets for array subscripts.
We will have to initialize the playfield every time when we start a new game so why don't we write a routine for this purpose:
REM -- Initialize the playfield REM -- Called when starting a new game PROC clear_playfield REM -- initialize the playfield FOR i! = 0 TO 23 \playfield[i!] = %0010000000000100 NEXT \playfield[24] = %0011111111111100 REM -- empty the playfield on screen FOR i! = 4 TO 23 TEXTAT 15, i!, " " : REM 10 spaces NEXT ENDPROC
A code block between the PROC
and ENDPROC
keywords is called a procedure. It is a routine with its own varible scope, although global variables can also be referenced using the \
operator. Notice that the variable playfield
is prepended with the \
character, which tells the compiler “this not a new local variable but the same that was defined outside of this procedure!” On the other hand, i!
is a local variable here and will not interfere with another i!
that is defined outside of this procedure.
It is also worth noting that array indices in XC=BASIC are zero-based! If the array length is 25, like in the above case, the valid array indices start at 0 and end at 24! Another important rule is that you must always pre-define arrays, even if they contain less than ten elements.
The procedure above does two things: initializes the playfield
array by setting the initial values for each of its members in a FOR … NEXT
loop. The next loop clears the playfield in the Screen RAM. We will call this procedure later in our program using the CALL
command.
Head to the next page where we'll plan the program flow.