-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakeGame.cs
332 lines (266 loc) · 9.52 KB
/
SnakeGame.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using PixelEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GameEngine.Snack.Extentions;
namespace GameEngine.Snack
{
public class SnakeGame : Game
{
private List<SnakeSegment> snake; // Store all segments of snake
// Coordinates of the food
private int foodX;
private int foodY;
private int score; // Player's score
private int dir; // Direction of snake
private bool dead; // Is the snake dead?
private bool started; // Has the game been started?
private ISnake snakeCharacter;
static void Main(string[] args)
{
// Create an instance
SnakeGame s = new SnakeGame();
// Construct the game
s.Construct(50, 50, 10, 10, 30);
// Start the game
s.Start();
}
// A part of the snake
private struct SnakeSegment
{
public SnakeSegment(int x, int y) : this()
{
this.X = x;
this.Y = y;
}
public int X { get; private set; } // X location
public int Y { get; private set; } // Y location
}
// Set the title of the window
public SnakeGame()
{
AppName = "SNAKE!";
snakeCharacter = new ExampleSnake();
}
// Start the game
public override void OnCreate()
{
// Uncomment to make the game fullscreen
//Enable(Subsystem.Fullscreen);
Enable(Subsystem.HrText);
Reset();
}
// Reset all fields
private void Reset()
{
// Init and make the snake
snake = new List<SnakeSegment>();
for (int i = 0; i < 9; i++)
snake.Add(new SnakeSegment(i + 20, 15));
// Set the variables to default values
foodX = 30;
foodY = 15;
//score = 0;
dir = 3;
dead = false;
Seed();
}
public override void OnUpdate(float elapsed)
{
snakeCharacter.UpdateMap(GetMap());
CheckStart();
UpdateSnake();
DrawGame();
//GetMap();
Thread.Sleep(100);
}
// Draw the game
private void DrawGame()
{
// Clear the screen
Clear(Pixel.Presets.Black);
if (started) // Inform the player of their score
DrawTextHr(new Point(15, 15), snakeCharacter.Name + " score: " + score, Pixel.Presets.Green, 2);
else // Inform the player to start by pressing enter
DrawTextHr(new Point(15, 15), "Press Enter To Start", Pixel.Presets.Green, 2);
// Draw the border
DrawRect(new Point(0, 0), ScreenWidth - 1, ScreenHeight - 1, Pixel.Presets.Grey);
// Render snake
for (int i = 1; i < snake.Count; i++)
Draw(snake[i].X, snake[i].Y, dead ? Pixel.Presets.Blue : Pixel.Presets.Yellow);
// Draw snake head
Draw(snake[0].X, snake[0].Y, dead ? Pixel.Presets.Green : Pixel.Presets.Magenta);
// Draw food
Draw(foodX, foodY, Pixel.Presets.Green);
}
// Update the snake's position
private void UpdateSnake()
{
// End game if snake is dead
if (dead)
started = false;
var direction = (SnakeDirection)dir;
var action = snakeCharacter.GetNextDirection(direction);
if ( (action == SnakeDirection.Down && direction == SnakeDirection.Up)
|| (action == SnakeDirection.Up && direction == SnakeDirection.Down)
|| (action == SnakeDirection.Left && direction == SnakeDirection.Right)
|| (action == SnakeDirection.Right && direction == SnakeDirection.Left))
{
//can't go to opposite direction
}
else
{
dir = (int) action;
}
int x = snake[0].X, y = snake[0].Y;
bool u = true, d = true, r = true, l = true;
for (int i = 1; i < snake.Count; i++)
{
if ((snake[i].X == x + 1 && snake[i].Y == y) || x + 1 == ScreenWidth)
r = false;
if ((snake[i].X == x - 1 && snake[i].Y == y) || x - 1 == 0)
l = false;
if ((snake[i].Y == y + 1 && snake[i].X == x) || y + 1 == ScreenHeight - 1)
d = false;
if ((snake[i].Y == y - 1 && snake[i].X == x) || y - 1 == 0)
u = false;
}
//// Turn right
/* if (GetKey(Key.Right).Pressed)
{
dir++;
if (dir == 4)
dir = 0;
}
//// Turn left
if (GetKey(Key.Left).Pressed)
{
dir--;
if (dir == -1)
dir = 3;
}
*/
if (snake[0].X > foodX && l)
dir = 3;
else if (snake[0].X < foodX && r)
dir = 1;
else if (snake[0].Y > foodY && u)
dir = 0;
else if (snake[0].Y < foodY && d)
dir = 2;
else if(l){
dir = 3;
}
else if(r){
dir = 1;
}
else if(u)
{
dir = 0;
}
else if(d)
{
dir = 2;
}
switch (dir)
{
case 0: // UP
y--;
break;
case 1: // RIGHT
x++;
break;
case 2: // DOWN
y++;
break;
case 3: // LEFT
x--;
break;
}
if (started)
{
// Move in the direction
snake.Insert(0, new SnakeSegment(x, y));
// Pop the tail
snake.RemoveAt(snake.Count - 1);
CheckCollision();
}
}
// Check for snake's collision
private void CheckCollision()
{
// Check collision with food
if (snake[0].X == foodX && snake[0].Y == foodY)
{
score+= Math.Max(1, snake.Count / 3);
RandomizeFood();
snake.Add(new SnakeSegment(snake[snake.Count - 1].X, snake[snake.Count - 1].Y));
}
// Check wall collision
if (snake[0].X <= 0 || snake[0].X >= ScreenWidth || snake[0].Y <= 0 || snake[0].Y >= ScreenHeight - 1)
dead = true;
// Check self collision
for (int i = 1; i < snake.Count; i++)
if (snake[i].X == snake[0].X && snake[i].Y == snake[0].Y)
dead = true;
}
// Check if the game is started
private void CheckStart()
{
if (!started)
{
// Check if game has to be started
//if (GetKey(Key.Enter).Pressed)
{
Reset();
started = true;
}
}
}
// Set random location for food
private void RandomizeFood()
{
// Loop while the food is not on empty cell
while (GetScreenPixel(foodX, foodY) != Pixel.Presets.Black)
{
// Set food to random point
foodX = Random(ScreenWidth);
foodY = Random(ScreenHeight);
}
}
/// <summary>
/// Get map
/// </summary>
/// <returns></returns>
private string GetMap()
{
var screen = this.GetScreen();
var mapBuilder = new StringBuilder();
for (int i = 0; i < screen.GetLength(0); i++)
{
Console.WriteLine("");
for (int j = 0; j < screen.GetLength(1); j++)
{
var pixel = screen[j, i];
if (pixel.isWall())
mapBuilder.Append("x");
else if (pixel.isEmpty())
mapBuilder.Append(" ");
else if (pixel.isHead())
mapBuilder.Append("*");
else if (pixel.isBody())
mapBuilder.Append("1");
else if (pixel.isFood())
mapBuilder.Append("7");
else if (pixel.isTrap())
mapBuilder.Append("6");
//Console.Write(mapBuilder[mapBuilder.Length - 1]);
}
}
return mapBuilder.ToString();
}
}
}