-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.cs
231 lines (221 loc) · 6.36 KB
/
Snake.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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
public class Snake : Game
{
public List<Point> body;//the body of the snake
public ConsoleColor defaultbg = ConsoleColor.Black;//default console background
public ConsoleColor snakeclr = ConsoleColor.DarkGreen;//snake color
public ConsoleColor foodclr = ConsoleColor.Red;//food color
public Point currentFood;//current food object
private Random rand;//rng
private System.Timers.Timer timer;
private Direction dir;
private bool stopped = false;
public Menu menu;
private Menu gameOver;
private Thread keys;
private bool canChange = true;
public Snake(Menu _menu)
{
//initialize the body and rng
rand = new Random();
body = new List<Point>();
timer = new System.Timers.Timer();
timer.Interval = 60;
timer.AutoReset = true;
timer.Elapsed += timerElapsed;
dir = Direction.Up;
name = "Snake";
//put the snake in the center
body.Add(new Point(60, 17));
body.Add(new Point(60, 16));
body.Add(new Point(60, 15));
//draw the snake and food
drawSnake();
generateFood();
menu = _menu;
keys = new Thread(Run);
keys.Start();
gameOver = new Menu("gameOver.txt");
timer.Enabled = true;
}
private void timerElapsed(object sender, ElapsedEventArgs e)
{
draw(dir);
}
public void generateFood()
{
//generate food at a random point
currentFood = new Point(rand.Next(0, Console.WindowWidth / 2) * 2, rand.Next(Console.WindowHeight));
//loop through the snake body
foreach (Point p in body)
{
//if the food spawns on the snake, respawn it
if (currentFood == p)
{
generateFood();
return;
}
}
//draw the food once it's in an acceptable position
drawFood();
}
//draw the snake
public void drawSnake()
{
foreach (Point p in body)
{
Console.SetCursorPosition(p.x, p.y);
Console.BackgroundColor = snakeclr;
//a square is two spaces
Console.Write(" ");
}
}
//move the snake
public void draw(Direction dir)
{
canChange = true;
//new front of the snake
Point p = null;
//set the new position based on the current direction
switch (dir)
{
case Direction.Up:
p = new Point(body[body.Count - 1].x, body[body.Count - 1].y - 1);
break;
case Direction.Down:
p = new Point(body[body.Count - 1].x, body[body.Count - 1].y + 1);
break;
case Direction.Left:
p = new Point(body[body.Count - 1].x - 2, body[body.Count - 1].y);
break;
case Direction.Right:
p = new Point(body[body.Count - 1].x + 2, body[body.Count - 1].y);
break;
}
//if it's not on the food, cover the back with the default bg
if (p != currentFood)
{
Console.SetCursorPosition(body[0].x, body[0].y);
Console.BackgroundColor = defaultbg;
Console.Write(" ");
try {
body.RemoveAt(0);
}
catch(Exception e)
{
}
}
else//if it is, generate a new food
{
score++;
generateFood();
}
//if you go outside the window, you lose
if (p.x >= Console.WindowWidth || p.x < 0 || p.y >= Console.WindowHeight || p.y < 0)
{
Console.SetCursorPosition(0, 0);
Stop();
return;
}
//draw the new front snake
Console.SetCursorPosition(p.x, p.y);
Console.BackgroundColor = snakeclr;
Console.Write(" ");
Console.BackgroundColor = defaultbg;
body.Add(p);
//if you hit the body, you lose
foreach (Point b in body)
{
if (!(body[body.Count - 1].Equals(b)))//check that the references aren't equal
{
if (p == b)//if they are the same, you lose
{
Console.SetCursorPosition(0, 0);
Stop();
}
}
}
}
public override void Resume()//draw the snake and the food again
{
drawSnake();
drawFood();
timer.Start();
}
private void drawFood()//draw the food
{
Console.SetCursorPosition(currentFood.x, currentFood.y);
Console.BackgroundColor = foodclr;
Console.Write(" ");
}
public override void HandleKey(ConsoleKeyInfo? cki2) {
if (cki2 == null)
{
Run();
return;
}
ConsoleKeyInfo cki = (ConsoleKeyInfo)cki2;
if (cki.Key == ConsoleKey.Escape)
{
if (timer.Enabled)
{
Pause();
}
else
{
Resume();
}
}
if (canChange)
{
if ((cki.Key == ConsoleKey.W || cki.Key == ConsoleKey.UpArrow) && dir != Direction.Down)
{
dir = Direction.Up;
}
else if ((cki.Key == ConsoleKey.A || cki.Key == ConsoleKey.LeftArrow) && dir != Direction.Right)
{
dir = Direction.Left;
}
else if ((cki.Key == ConsoleKey.S || cki.Key == ConsoleKey.DownArrow) && dir != Direction.Up)
{
dir = Direction.Down;
}
else if ((cki.Key == ConsoleKey.D || cki.Key == ConsoleKey.RightArrow) && dir != Direction.Left)
{
dir = Direction.Right;
}
canChange = false;
}
}
public override void Run()
{
while (!stopped)
{
ReadKey();
}
gameOver.Hide();
menu.Show();
}
public override void Pause()
{
timer.Stop();
}
public override void Stop()
{
timer.Stop();
Console.Clear();
gameOver.Show();
stopped = true;
}
public enum Direction
{
Left,
Right,
Up,
Down
}
}