Most real-time games repeat the same structure every frame:
Do
HandleInput()
UpdateGame()
RenderGame()
Flip()
Loop Until gameOver
Input should be read every frame and translated into game actions.
KeyDown for held movement.KeyHit for one-frame actions (confirm, pause, menu select).MouseX, MouseY, MouseButton for pointer interactions.StartTextInput and GetTextInput$ for typed text entry.Update is where game rules happen:
Keep update logic independent from drawing whenever possible.
Render draws the current state without changing game rules.
Cls.Load resources before the main loop. Reuse handles during the loop. Free resources after the loop.
Dim fontRef As Integer
Dim playerImage As Integer
fontRef = LoadFont("", 18)
playerImage = LoadImage("assets/player.png")
Do
Cls()
DrawImage(playerImage, playerX, playerY)
DrawText(fontRef, 20, 20, "Score: " + Str(score))
Flip()
Loop Until finished
FreeImage(playerImage)
FreeFont(fontRef)
Most games switch between states such as menu, gameplay, and pause. Use a state variable.
Const STATE_MENU As Integer = 1
Const STATE_PLAY As Integer = 2
Const STATE_PAUSE As Integer = 3
Dim state As Integer
state = STATE_MENU
For smoother motion, many games use FrameDelta() to scale movement by elapsed time.
playerX = playerX + Round(playerSpeed * FrameDelta())
Navigation: Previous: Programming Fundamentals | Outline | Next: Debugging and Problem Solving