-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
173 lines (149 loc) · 3.98 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
using System;
Random random = new Random();
Console.CursorVisible = false;
int height = Console.WindowHeight - 1;
int width = Console.WindowWidth - 5;
bool shouldExit = false;
// Console position of the player
int playerX = 0;
int playerY = 0;
// Console position of the food
int foodX = 0;
int foodY = 0;
// Available player and food strings
string[] states = { "('-')", "(^-^)", "(X_X)" };
string[] foods = { "@@@@@", "$$$$$", "#####" };
// Current player string displayed in the Console
string player = states[0];
// Index of the current food
int food = 0;
InitializeGame();
while (!shouldExit)
{
if (TerminalResized())
{
Console.Clear();
Console.WriteLine("Console was resized. Program Exiting.");
shouldExit = true;
}
else
{
if (PlayerIsFaster())
{
Move(1, false); // if i increase the move speed then it isnt registered that the food is eaten
}
else if (PlayerIsSick())
{
FreezePlayer();
} else
{
Move(otherKeysExit: false);
}
if (NeedFood())
{
ChangePlayer();
ShowFood();
}
}
}
// Returns true if the Terminal was resized
bool TerminalResized()
{
return height != Console.WindowHeight - 1 || width != Console.WindowWidth - 5;
}
// 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]);
}
bool NeedFood()
{
return foodX == playerX && foodY == playerY;
}
bool PlayerIsSick()
{
return player.Equals(states[2]);
}
bool PlayerIsFaster()
{
return player.Equals(states[1]);
}
// Changes the player to match the food consumed
void ChangePlayer()
{
player = states[food];
Console.SetCursorPosition(playerX, playerY);
Console.Write(player);
}
// Temporarily stops the player from moving
void FreezePlayer()
{
System.Threading.Thread.Sleep(1000);
player = states[0];
}
// Reads directional input from the Console and moves the player
void Move(int speed = 1, bool otherKeysExit = false)
{
int lastX = playerX;
int lastY = playerY;
switch (Console.ReadKey(true).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;
if (shouldExit)
{
Console.Clear();
Console.WriteLine("You clicked the escape key. Program exiting.");
return;
}
break;
default:
shouldExit = otherKeysExit;
if (shouldExit)
{
Console.Clear();
Console.WriteLine("You clicked a nondirectional key. Program exiting.");
return;
}
break;
}
// Clear the characters at the previous position
Console.SetCursorPosition(lastX, lastY);
for (int i = 0; i < player.Length; i++)
{
Console.Write(" ");
}
// 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.Write(player);
}
// Clears the console, displays the food and player
void InitializeGame()
{
Console.Clear();
ShowFood();
Console.SetCursorPosition(0, 0);
Console.Write(player);
}