XC=BASIC is a procedural language which means that you can define routines that have their own variable scope and therefore they don't mess up the rest of the program (although they can access global variables as previously seen). These routines can be called by the main program flow, by another routine, or by the routine itself as well. The latter is called recursion. There are two types of routines in XC=BASIC:
PROC ... ENDPROC
keywords. Procedures can have arguments but they don't return anything.FUN ... ENDFUN
keywords. Functions are similar to procedures, except that they must return a value and this value must be always of the same type.
Although the good old GOSUB ... RETURN
statements are there for your convenience, you can write cleaner, stable and more readable code using procedures and functions.
For our game we will need the following routines:
clear_playfield
to clear the playfield before each game. We have already completed this.get_shape()
to find a shape in the array of shapes.overlaps()
to check if a shape overlaps with the playfield. This will let us know if the piece can be moved or rotated.draw_shape
to draw a shape on the playfield at a given position.draw_preview
to draw the next shape in the preview window. This will make use of the previous procedure.lock_piece
to lock a piece, ie. to make it permanent when it has hit to the bottom.clear_row
to clear a row and cascade everything from above when there's a full row.These are 7 routines in total out of which one is already done. We'll start coding the rest from the next page and on.