2. The "game loop"
The main part of a game is a loop, which repeats a series of actions, which tipically are:
- Drawing the elements on screen
- Check if the user pressed a key (or moved the joystick/gamepad, or the mouse, or any other input device)
- Move the enemies, the environment and any other part of the game which should be able to move on its own.
- Check collisions, to see if we have collected any item, been hit by an enemy, if a shot has hit something, etc. Then, we can update the points, the lives and the rest of the current game state.
- If we want the speed of the game to be "stable" (not extremely fast on newer computers, which would unplayable), we may need to pause after each "frame".
In the first approach, we will make our game is repeated indefinitely, with a condition like "while (1==1)".
Most of the tasks of this "game loop" will be completed later, and now they will be commented out, so that the source "remember" what will end up being.
The result could be something like:
/* First mini-console-game skeleton Version C: "game loop" */ using System; public class Game01c { public static void Main() { int x=40, y=12; // Game Loop while( 1 == 1 ) { // Draw Console.Clear(); Console.SetCursorPosition(x, y); Console.Write("C"); // Read keys and calculate new position // TO DO // Move enemies and environment // TO DO // Collisions, lose energy or lives, etc // TO DO // Pause till next fotogram // TO DO } } }
Suggested exercise: Use a "bool" variable to control the game loop (right now, this variable will have "true" as a statring value, which will not change).