-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
172 lines (152 loc) · 4.22 KB
/
Copy pathProgram.cs
File metadata and controls
172 lines (152 loc) · 4.22 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
using System;
using System.Collections.Generic;
using System.Threading;
Random random = new Random();
Console.CursorVisible = false;
int height = Console.WindowHeight - 1;
int width = Console.WindowWidth - 5;
bool shouldExit = false;
// Player position in the Console
int playerX = 0;
int playerY = 0;
// Food position in the Console
int foodX = 0;
int foodY = 0;
// food states and player states
string[] states = { "('-')", "(^-^)", "(X_X)", "(O-O)", "(>_<)" };
string[] foods = { "@@@@@", "$$$$$", "#####", "OOOO", "90210" };
// player 1
string player = states[0];
// array index of the current food
int food = 0;
// game starts
InitializeGame();
while (!shouldExit)
{
Move();
AdditionalMoves();
if (TerminalResized())
{
TerminalResized();
Console.Clear();
Console.WriteLine("The Console was resized. The Game is now exiting");
shouldExit = true;
}
}
// 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
// player.Length is used to prevent the food from being displayed outside the bounds of the Console
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]);
}
void IncorrectMove()
{
Console.Clear();
Console.WriteLine("Invalid move. Please use the arrow keys to move the player.");
shouldExit = true;
}
// Reads directional input from the Console and moves the player
void Move()
{
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--; break;
case ConsoleKey.RightArrow:
playerX++; break;
case ConsoleKey.Escape:
shouldExit = true;
Console.Clear();
Console.WriteLine("The game is exiting"); break;
default:
IncorrectMove(); 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);
void ChangeFoodLocation()
{
if (lastX == foodX && lastY == foodY)
{
ShowFood();
}
}
void ChangePlayer()
{
if (lastX == foodX && lastY == foodY)
{
ShowNewPlayer();
}
}
ChangePlayer();
ChangeFoodLocation();
}
void ShowNewPlayer()
{
player = states[food];
Console.SetCursorPosition(playerX, playerY);
Console.Write(player);
}
void AdditionalMoves()
{
var key = Console.ReadKey(true).Key;
if (player == states[2])
{
Thread.Sleep(2000);
if (key == ConsoleKey.RightArrow || key == ConsoleKey.LeftArrow)
{
playerX =+ 2;
}
else if (key == ConsoleKey.UpArrow || key == ConsoleKey.DownArrow)
{
playerY =+ 2;
}
}
else if (player == states[1] && key == ConsoleKey.LeftArrow)
{
playerX =+ 4;
}
else if (player == states[3]) { }
else if (player == states[4]) { }
else { }
}
// Clears the console, displays the food and player
void InitializeGame()
{
Console.Clear();
Console.WriteLine("Welcome to the Snake Game I built");
System.Console.WriteLine("To exit press the 'esc' button");
System.Console.WriteLine("The game will start shorlty");
Thread.Sleep(5000);
Console.Clear();
ShowFood();
Console.SetCursorPosition(0, 0);
Console.Write(player);
}