The game loop is what keeps your game running. In this tutorial, you learn the very basics of constructing a game loop. This is a very important tutorial as all games are built on the game loop.
PlayBasic Game Loop Tutorial
When PlayBasic runs out of code to execute, it naturally ends the game. A game loop (known as the main loop outside game programming) is the primary loop that keeps your game running. PlayBasic has a very convenient feature that makes constructing the game loop exceedingly easy — when the player hits ESCAPE, the game will automatically shut down. This means, at least in the beginning, all you have to do is worry about the loop itself.
All of the code below (except End) is necessary for a game loop to properly work, so here it is all at once :
Do Cls 0 Print "Barkin' Mad PlayBasic" Print "Press Escape" Sync Loop End
The game loop is nothing more than a loop that keeps repeating until the game is over. It is constructed using two keywords Do and Loop. Essentially, PlayBasic will repeat all the code between Do and Loop until the user hits ESCAPE. Normally, all of your main game code would go between Do and Loop, but for now we're just drawing a simple frame each loop.
Each loop draws a frame. The frame begins with Cls 0, which clears away any previous graphics displayed so new frame can be drawn. Cls requires you to tell it which color you want the screen to be. In this case, we've used , which is computerese for the color black.
- Put everything you want to be seen between Cls and Sync.
Anything before Cls will be erased by Cls and never seen. Anything after Sync will not be seen because it is never drawn in the first place.
Congratulations. You have mastered a very basic form of the game loop. You will be using this foundation for every game you make with PlayBasic.