Step 3.2: Writing routines

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:

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:

  1. clear_playfield to clear the playfield before each game. We have already completed this.
  2. get_shape() to find a shape in the array of shapes.
  3. The function overlaps() to check if a shape overlaps with the playfield. This will let us know if the piece can be moved or rotated.
  4. draw_shape to draw a shape on the playfield at a given position.
  5. draw_preview to draw the next shape in the preview window. This will make use of the previous procedure.
  6. lock_piece to lock a piece, ie. to make it permanent when it has hit to the bottom.
  7. 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.