-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
72 lines (59 loc) · 2.42 KB
/
Copy pathProgram.cs
File metadata and controls
72 lines (59 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class Program
{
// Save console colours, to restore state after the game ends.
private static ConsoleColor originalBackgroundColor;
private static ConsoleColor originalForegroundColor;
public static void Main()
{
Console.CursorVisible = false;
Console.CancelKeyPress += new ConsoleCancelEventHandler(CleanupHandler);
originalBackgroundColor = Console.BackgroundColor;
originalForegroundColor = Console.ForegroundColor;
var maxWidth = Console.WindowWidth > 50 ? 50 : Console.WindowWidth-1;
var maxHeight = Console.WindowHeight > 24 ? 24: Console.WindowHeight-2;
var game = new MonsterMazeGame(maxWidth, maxHeight);
bool quitGame = false;
while(!quitGame)
{
ShowTitleScreen();
if(GameUtils.WaitForEscapeOrSpace() != true)
{
bool gameOver = false;
for(int levelNumber = 1; levelNumber <= MonsterMazeGame.MaxLevel && !gameOver; levelNumber++)
{
gameOver = game.PlayLevel(levelNumber);
}
}
else
{
// Player wants to quit the game
quitGame = true;
}
}
CleanupHandler(null, null);
}
protected static void ShowTitleScreen()
{
Console.Clear();
Console.SetCursorPosition(Console.WindowWidth/2-20, 5);
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("### ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("Monster Maze");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" ###");
Console.SetCursorPosition(0, 10);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("You are trapped in a maze with monsters. Your goal is to escape.");
Console.WriteLine("Use the arrow keys to move, avoid the monsters.");
Console.WriteLine();
Console.WriteLine("Press space to start, or escape to quit.");
}
// If "escape" or "control-c" is pressed, try to get the console window back into a clean state.
protected static void CleanupHandler(object? sender, ConsoleCancelEventArgs? args)
{
Console.ForegroundColor = originalForegroundColor;
Console.BackgroundColor = originalBackgroundColor;
Console.Clear();
}
}