-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cs
99 lines (87 loc) · 3.09 KB
/
Game.cs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
namespace scroller_game;
public class Game
{
private const int HEIGHT = 15;
private const int WIDTH = 7;
private const int REFRESH_RATE = 16; // ~60fps
private const int LEVEL_LENGTH = 240;
private const int PLAYER_STARTING_POSITION = 3;
char[,] m_gameWorld = new char[HEIGHT,WIDTH]
{
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '}
};
char[] m_playersArea = new char[WIDTH] {' ', ' ', ' ', ' ', ' ', ' ', ' '};
bool m_gameEnded;
ConsoleKeyInfo m_playersKeyPressedInfo;
public void Run()
{
int currentGameTick = 0;
EntityManager entityManager = new(
levelLength: LEVEL_LENGTH,
levelWidth: WIDTH,
playerStartingPosition: PLAYER_STARTING_POSITION);
Drawer drawer = new(HEIGHT, WIDTH);
Thread watchKeyThread = new(WatchKeys);
Thread gameThread = new(GameLoop);
Console.Clear();
watchKeyThread.Start();
gameThread.Start();
void WatchKeys()
{
while (m_playersKeyPressedInfo.Key != Config.QuitKey && !m_gameEnded)
{
m_playersKeyPressedInfo = Console.ReadKey(true);
}
}
void GameLoop()
{
while (m_playersKeyPressedInfo.Key != Config.QuitKey && !m_gameEnded)
{
entityManager.UpdatePlayerPosition(m_playersArea, m_playersKeyPressedInfo.Key);
m_playersKeyPressedInfo = new ConsoleKeyInfo();
if (ShouldUpdateGameWorld(currentGameTick))
{
entityManager.AddNextLineOfObstacles();
entityManager.UpdateObstacles(m_gameWorld, HEIGHT);
}
drawer.Draw(m_gameWorld, m_playersArea);
if (m_gameEnded)
{
break;
}
if (entityManager.PlayerObstacleCollision(HEIGHT))
{
Console.WriteLine("Game Over! You Lose!");
break;
}
if (entityManager.LevelWon())
{
Console.WriteLine("Game Over! You Win!");
break;
}
currentGameTick++;
Thread.Sleep(REFRESH_RATE);
Console.Clear();
}
m_gameEnded = true;
}
bool ShouldUpdateGameWorld(int currentTick)
{
return currentTick % 5 == 0;
}
}
}