We have reached the final step in our game development, the outermost loop! This loop is responsible for starting the game loops endlessly. The algorithm is super simple:
Here is the code:
REM -- REM -- The program loop REM -- DISABLEIRQ WHILE 1 = 1 TEXTAT 27, 6, "press fire" REM -- Wait for fire button REPEAT UNTIL joy_1_fire!() = 1 REM -- Clear messages TEXTAT 27, 4, " " TEXTAT 27, 5, " " TEXTAT 27, 6, " " REM -- Start new game. Initialize variables level! = 1 delay! = 10 TEXTAT 26, 11, " " TEXTAT 26, 11, level! CALL clear_playfield game_status! = 0 ttl_rows_cleared! = 0 score% = 0.0 REM -- Get next shape and display preview nxt_shape_no! = CAST!(RND%() * 7.0) nxt_shape = get_shape(nxt_shape_no!, 0) CALL draw_preview REM -- REM -- Paste the game loop here REM -- REM -- Game over. Update hiscore and messages TEXTAT 27, 4, "game over" IF score% > hiscore% THEN hiscore% = score% TEXTAT 27, 5, "new hiscore" TEXTAT 27, 17, hiscore% ENDIF ENDWHILE REM -- Program loop ends here
I have started with a command that may be unknown for you, namely DISABLEIRQ
. It is identical to the assembly language instruction SEI
. It tells the processor to not accept any intterrupt requests. The reason why we use this command here is to make the gameplay a little smoother by turning out the default interrupt service of the C64, that is responsible for flashing the cursor, updating the system clock and watching the keyboard. Since we don't need any of the them in our game, it is safe to turn it off, saving a little CPU time every 1/50 of a second.
This loop is a WHILE ... ENDWHILE
loop, which is a pre-test loop. Since its entry condition always evaluates to true, the loop will run endlessly. This is the same as
REPEAT <statements> UNTIL 1 = 0
but I wanted to show you that XC=BASIC features both kinds of loops.
The rest should be really straightforward. If there is a mistake and your program can't be compiled, compare it to my version: here is the full code after all steps completed.
Congratulations, you have just coded a Tetris game in XC=BASIC! Compile, run and enjoy!
If you have questions, something doesn't work or you have improvement ideas, please write to me to feketecsaba [at] gmail [dot] com or mention @news_xc in your tweet!
Thanks for reading!