Game Programming
The Game Loop
Most console games follow the same flow in Main:
- Setup: initialize global variables and draw anything that is only shown once
- Loop until the game ends:
- Input: read any keys that were pressed
- Update: change the game state (positions, score, enemies)
- Draw: redraw only what changed
- Wait: pause briefly so the loop runs at a steady frame rate
- Cleanup: restore the console before exiting
Global Variables
Without object-oriented programming (OOP), the simplest way to share state between methods are static fields on the Program class.
They behave like global variables:
class Program
{
static bool gameOver = false;
// ...
}
Reading Keys
Normally a program doesn't receive input until Enter is pressed, BUT that doesn't work for games.
Console.ReadKey()
ReadKey is "blocking" which means the program pauses until a key is pressed, then immediately returns a ConsoleKeyInfo.
Pass true so the key isn't echoed (printed) to the screen:
bool running = true;
while (running)
{
ConsoleKeyInfo key = Console.ReadKey(true); // true = don't print the key
// ... more stuff ...
}
key isn't just a character. It's a ConsoleKeyInfo struct that jumbles information about the key press:
| Property | Type | What it tells you | Press Shift + A | Press ↑ |
|---|---|---|---|---|
Key | [ConsoleKey | Which physical key was pressed | ConsoleKey.A | ConsoleKey.UpArrow |
KeyChar | char | Which character the key produced | 'A' | '\0' (none) |
Which one should you use?
Key, for game controls 👉qandQboth giveConsoleKey.Q, (keys that don't type a character have their own values)KeyChar, for text 👉 use it when you care about what was typed (e.g. player entering their name)
Console.KeyAvailable
KeyAvailable is "non-blocking" which means when you press a key, the OS stores it in a "buffer". KeyAvailable checks that buffer and returns right away:
true👉 a key is waiting, soReadKeywill return immediatelyfalse👉 no key yet, so skip reading and keep the game running
int counter = 0;
bool running = true;
while (running)
{
counter++;
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey(true);
Console.WriteLine($"counter={counter}, key={key.Key}");
if (key.Key == ConsoleKey.Q) running = false;
}
}
The counter keeps growing between key presses, so the program never stops to wait.
Keys to Movement
Decide what each key does, then translate keys to movement with a switch:
static void ProcessKeyStrokes()
{
while (Console.KeyAvailable) // empty the whole buffer
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Q:
gameOver = true;
break;
case ConsoleKey.UpArrow:
Move(-1, 0);
break;
case ConsoleKey.DownArrow:
Move( 1, 0);
break;
case ConsoleKey.LeftArrow:
Move( 0, -1);
break;
case ConsoleKey.RightArrow:
Move( 0, 1);
break;
}
}
}
static void Move(int dRow, int dCol)
{
// TODO - implement Move logic
}
Example
Putting the game loop all together:
using System;
using System.Threading;
class Program
{
// ===============================================================
// Game state (static fields, shared by all methods in Program)
// ===============================================================
static bool gameOver = false;
static int row, col; // current position
static int prevRow, prevCol; // position at the last draw
const int FrameMs = 50; // ~20 frames per second
// ===============================================================
// Entry point
// ===============================================================
static void Main()
{
// Setup Game
Console.Clear();
Console.CursorVisible = false;
row = prevRow = Console.WindowHeight / 2;
col = prevCol = Console.WindowWidth / 2;
Console.SetCursorPosition(col, row);
Console.Write('@');
// Game Loop
while (!gameOver)
{
ProcessKeyStrokes(); // TODO implement
Draw(); // TODO implement
Thread.Sleep(FrameMs);
}
// Cleanup after Game Over
Console.CursorVisible = true;
Console.Clear();
}
}