-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
251 lines (215 loc) · 5.86 KB
/
Program.cs
File metadata and controls
251 lines (215 loc) · 5.86 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
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
251
using System;
using System.Net.NetworkInformation;
Random random = new Random();
Console.CursorVisible = false;
int height = Console.WindowHeight - 1;
int width = Console.WindowWidth - 5;
bool shouldExit = false;
int score = 0;
int lastScore = -1;
int moveDelay = 50;
int timeLeft = 60;
int endTime = -1;
DateTime timeCheck = DateTime.Now;
DateTime boostEndTime = DateTime.MinValue;
// Console position of the player
int playerX = 0;
int playerY = 1;
// Console position of the food
int foodX = 0;
int foodY = 0;
// Available player and food strings
string[] states = { "(o_o)", "(^_^)", "(x_x)" };
string[] foods = { "🍕", "💎", "💣" };
// Current player string displayed in the Console
string player = states[0];
// Index of the current food
int food = 0;
Console.BackgroundColor = ConsoleColor.Black;
Console.Clear();
InitializeGame();
while (!shouldExit)
{
UpdateTime();
ScoreBoard();
if (TerminalResized())
{
Console.SetCursorPosition(1, height);
Console.Write("Console was resized. Exiting...");
shouldExit = true;
}
if (PlayerSick())
{
FreezePlayer();
}
else
{
bool fast = FastSpeed();
Move(speed: fast ? 2 : 1);
}
if (FoodConsumed())
{
score++;
ChangePlayer();
ShowFood();
}
if (DateTime.Now > boostEndTime && moveDelay < 30)
{
moveDelay = 50;
}
System.Threading.Thread.Sleep(moveDelay);
}
// Returns true if the Terminal was resized
bool TerminalResized()
{
return height != Console.WindowHeight - 1 || width != Console.WindowWidth - 5;
}
void UpdateTime()
{
if ((DateTime.Now - timeCheck).TotalSeconds >= 1)
{
timeLeft--;
timeCheck = DateTime.Now;
if (timeLeft <= 0)
{
Console.Clear();
Console.WriteLine("⏱️ Time's up!");
Console.WriteLine($"🏆 Final Score: {score}");
shouldExit = true;
}
}
}
void ScoreBoard()
{
if (timeLeft != endTime || score != lastScore)
{
Console.SetCursorPosition(0, 0);
if (timeLeft <= 10)
{
Console.ForegroundColor = ConsoleColor.Red;
}
else
{
Console.ForegroundColor = ConsoleColor.Cyan;
}
Console.Write($"Score: {score} Time left: {timeLeft}s ");
Console.ResetColor();
endTime = timeLeft;
lastScore = score;
}
}
// Displays random food at a random location
void ShowFood()
{
// Update food to a random index
food = random.Next(0, foods.Length);
// Update food position to a random location
foodX = random.Next(0, width - player.Length);
foodY = random.Next(0, height - 1);
// Display the food at the location
Console.SetCursorPosition(foodX, foodY);
Console.Write(foods[food]);
}
// Changes the player to match the food consumed
void ChangePlayer()
{
player = states[food];
switch (player)
{
case "(o_o)":
Console.ForegroundColor = ConsoleColor.Yellow;
timeLeft += 3;
moveDelay = 25;
break;
case "(^_^)":
Console.ForegroundColor = ConsoleColor.Blue;
moveDelay = 5;
timeLeft += 10;
boostEndTime = DateTime.Now.AddSeconds(4);
break;
case "(x_x)":
Console.ForegroundColor = ConsoleColor.Red;
moveDelay = 50;
timeLeft -= 10;
break;
}
//To avoid negative or extreme time and score
if (score < 0) score = 0;
if (timeLeft < 0) timeLeft = 0;
if (timeLeft > 999) timeLeft = 999;
Console.SetCursorPosition(playerX, playerY);
Console.Write(player);
}
// Temporarily stops the player from moving
void FreezePlayer()
{
System.Threading.Thread.Sleep(1500);
score -= 2;
player = states[0];
}
// Clears the console, displays the food and player
void InitializeGame()
{
Console.Clear();
ShowFood();
Console.SetCursorPosition(0, 0);
Console.Write(player);
}
// checks if player has consumed food
bool FoodConsumed()
{
int range = player.Length - 1;
return Math.Abs(playerX - foodX) <= range && playerY == foodY;
}
// checks for current player, then freezes player
bool PlayerSick() => player == states[2];
// checks for player state, then determine the move speed
bool FastSpeed() => player.Equals(states[1]);
// Reads directional input from the Console and moves the player
void Move(bool game = false, int speed = 1)
{
int lastX = playerX;
int lastY = playerY;
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey(true);
switch (key.Key)
{
case ConsoleKey.UpArrow:
playerY--;
break;
case ConsoleKey.DownArrow:
playerY++;
break;
case ConsoleKey.LeftArrow:
playerX -= speed;
break;
case ConsoleKey.RightArrow:
playerX += speed;
break;
case ConsoleKey.Escape:
shouldExit = true;
break;
default:
shouldExit = game;
break;
}
}
// Clear the characters at the previous position
Console.SetCursorPosition(lastX, lastY);
Console.Write(new string(' ', player.Length));
// Keep player position within the bounds of the Terminal window
playerX = (playerX < 0) ? 0 : (playerX >= width ? width : playerX);
playerY = (playerY < 0) ? 0 : (playerY >= height ? height : playerY);
// Draw the player at the new location
Console.SetCursorPosition(playerX, playerY);
Console.ForegroundColor = player switch
{
"(^_^)" => ConsoleColor.Blue,
"(x_x)" => ConsoleColor.Red,
"(o_o)" => ConsoleColor.Yellow,
};
Console.Write(player);
Console.ResetColor();
Thread.Sleep(50);
}