forked from dotnet/dotnet-console-games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
250 lines (239 loc) · 4.82 KB
/
Program.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using System.Text;
using System.Threading;
int width = 50;
int height = 30;
int windowWidth;
int windowHeight;
Random random = new();
char[,] scene;
int score = 0;
int carPosition;
int carVelocity;
bool gameRunning;
bool keepPlaying = true;
bool consoleSizeError = false;
int previousRoadUpdate = 0;
Console.CursorVisible = false;
try
{
Initialize();
LaunchScreen();
while (keepPlaying)
{
InitializeScene();
while (gameRunning)
{
if (Console.WindowHeight < height || Console.WindowWidth < width)
{
consoleSizeError = true;
keepPlaying = false;
break;
}
HandleInput();
Update();
Render();
if (gameRunning)
{
Thread.Sleep(TimeSpan.FromMilliseconds(33));
}
}
if (keepPlaying)
{
GameOverScreen();
}
}
Console.Clear();
if (consoleSizeError)
{
Console.WriteLine("Console/Terminal window is too small.");
Console.WriteLine($"Minimum size is {width} width x {height} height.");
Console.WriteLine("Increase the size of the console window.");
}
Console.WriteLine("Drive was closed.");
}
finally
{
Console.CursorVisible = true;
}
void Initialize()
{
windowWidth = Console.WindowWidth;
windowHeight = Console.WindowHeight;
if (OperatingSystem.IsWindows())
{
if (windowWidth < width && OperatingSystem.IsWindows())
{
windowWidth = Console.WindowWidth = width + 1;
}
if (windowHeight < height && OperatingSystem.IsWindows())
{
windowHeight = Console.WindowHeight = height + 1;
}
Console.BufferWidth = windowWidth;
Console.BufferHeight = windowHeight;
}
}
void LaunchScreen()
{
Console.Clear();
Console.WriteLine("This is a driving game.");
Console.WriteLine();
Console.WriteLine("Stay on the road!");
Console.WriteLine();
Console.WriteLine("Use A, W, and D to control your velocity.");
Console.WriteLine();
Console.Write("Press [enter] to start...");
PressEnterToContinue();
}
void InitializeScene()
{
const int roadWidth = 10;
gameRunning = true;
carPosition = width / 2;
carVelocity = 0;
int leftEdge = (width - roadWidth) / 2;
int rightEdge = leftEdge + roadWidth + 1;
scene = new char[height, width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j < leftEdge || j > rightEdge)
{
scene[i, j] = '.';
}
else
{
scene[i, j] = ' ';
}
}
}
}
void Render()
{
StringBuilder stringBuilder = new(width * height);
for (int i = height - 1; i >= 0; i--)
{
for (int j = 0; j < width; j++)
{
if (i == 1 && j == carPosition)
{
stringBuilder.Append(
!gameRunning ? 'X' :
carVelocity < 0 ? '<' :
carVelocity > 0 ? '>' :
'^');
}
else
{
stringBuilder.Append(scene[i, j]);
}
}
if (i > 0)
{
stringBuilder.AppendLine();
}
}
Console.SetCursorPosition(0, 0);
Console.Write(stringBuilder);
}
void HandleInput()
{
while (Console.KeyAvailable)
{
ConsoleKey key = Console.ReadKey(true).Key;
switch (key)
{
case ConsoleKey.A or ConsoleKey.LeftArrow:
carVelocity = -1;
break;
case ConsoleKey.D or ConsoleKey.RightArrow:
carVelocity = +1;
break;
case ConsoleKey.W or ConsoleKey.UpArrow or ConsoleKey.S or ConsoleKey.DownArrow:
carVelocity = 0;
break;
case ConsoleKey.Escape:
gameRunning = false;
keepPlaying = false;
break;
case ConsoleKey.Enter:
Console.ReadLine();
break;
}
}
}
void GameOverScreen()
{
Console.SetCursorPosition(0, 0);
Console.WriteLine("Game Over");
Console.WriteLine($"Score: {score}");
Console.WriteLine($"Play Again (Y/N)?");
GetInput:
ConsoleKey key = Console.ReadKey(true).Key;
switch (key)
{
case ConsoleKey.Y:
keepPlaying = true;
break;
case ConsoleKey.N or ConsoleKey.Escape:
keepPlaying = false;
break;
default:
goto GetInput;
}
}
void Update()
{
for (int i = 0; i < height - 1; i++)
{
for (int j = 0; j < width; j++)
{
scene[i, j] = scene[i + 1, j];
}
}
int roadUpdate =
random.Next(5) < 4 ? previousRoadUpdate :
random.Next(3) - 1;
if (roadUpdate is -1 && scene[height - 1, 0] is ' ') roadUpdate = 1;
if (roadUpdate is 1 && scene[height - 1, width - 1] is ' ') roadUpdate = -1;
switch (roadUpdate)
{
case -1: // left
for (int i = 0; i < width - 1; i++)
{
scene[height - 1, i] = scene[height - 1, i + 1];
}
scene[height - 1, width - 1] = '.';
break;
case 1: // right
for (int i = width - 1; i > 0; i--)
{
scene[height - 1, i] = scene[height - 1, i - 1];
}
scene[height - 1, 0] = '.';
break;
}
previousRoadUpdate = roadUpdate;
carPosition += carVelocity;
if (carPosition < 0 || carPosition >= width || scene[1, carPosition] is not ' ')
{
gameRunning = false;
}
score++;
}
void PressEnterToContinue()
{
GetInput:
ConsoleKey key = Console.ReadKey(true).Key;
switch (key)
{
case ConsoleKey.Enter:
break;
case ConsoleKey.Escape:
keepPlaying = false;
break;
default: goto GetInput;
}
}