-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake
199 lines (178 loc) · 7.99 KB
/
Snake
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Snake
{
class Snake
{
static void Main(string[] args)
{
byte right = 0;
byte left = 1;
byte down = 2;
byte up = 3;
int lastFoodTime = 0;
int foodDisappearTime = 8000;
int negativePoints = 0;
Position[] directions = new Position[]
{
new Position(0, 1), // right
new Position(0, -1), // left
new Position(1, 0), // down
new Position(-1, 0) // top
};
double sleepTime = 100;
int direction = right;
Random randomNumbersGenerator = new Random();
Console.BufferHeight = Console.WindowHeight;
lastFoodTime = Environment.TickCount;
List<Position> obstacles = new List<Position>()
{
new Position(12, 12),
new Position(14, 20),
new Position(7, 7)
};
foreach (Position obstacle in obstacles)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.SetCursorPosition(obstacle.col, obstacle.row);
Console.Write("=");
}
Queue<Position> snakeElements = new Queue<Position>();
for (int i = 0; i <= 5; i++)
{
snakeElements.Enqueue(new Position(0, i));
}
Position food;
do
{
food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight),
randomNumbersGenerator.Next(0, Console.WindowWidth));
} while (snakeElements.Contains(food) || obstacles.Contains(food));
Console.SetCursorPosition(food.col, food.row);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("@");
foreach (Position position in snakeElements)
{
Console.SetCursorPosition(position.col, position.row);
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write("*");
}
while (true)
{
negativePoints++;
if (Console.KeyAvailable)
{
ConsoleKeyInfo userInput = Console.ReadKey();
if (userInput.Key == ConsoleKey.LeftArrow)
{
if (direction != right) direction = left;
}
if (userInput.Key == ConsoleKey.RightArrow)
{
if (direction != left) direction = right;
}
if (userInput.Key == ConsoleKey.UpArrow)
{
if (direction != down) direction = up;
}
if (userInput.Key == ConsoleKey.DownArrow)
{
if (direction != up) direction = down;
}
}
Position snakeHead = snakeElements.Last();
Position nextDirection = directions[direction];
Position snakeNewHead = new Position(snakeHead.row + nextDirection.row,
snakeHead.col + nextDirection.col);
if (snakeNewHead.col < 0) snakeNewHead.col = Console.WindowWidth - 1;
if (snakeNewHead.row < 0) snakeNewHead.row = Console.WindowHeight - 1;
if (snakeNewHead.row >= Console.WindowHeight) snakeNewHead.row = 0;
if (snakeNewHead.col >= Console.WindowWidth) snakeNewHead.col = 0;
if (snakeElements.Contains(snakeNewHead) || obstacles.Contains(snakeNewHead))
{
Console.SetCursorPosition(0, 0);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Game over!");
int userPoints = (snakeElements.Count - 6) * 100 - negativePoints;
// if (userPoints < 0) userPoints = 0;
userPoints = Math.Max(userPoints, 0);
Console.WriteLine("Your points are: {0}", userPoints);
return;
}
Console.SetCursorPosition(snakeHead.col, snakeHead.row);
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write("*");
snakeElements.Enqueue(snakeNewHead);
Console.SetCursorPosition(snakeNewHead.col, snakeNewHead.row);
Console.ForegroundColor = ConsoleColor.Gray;
if (direction == right) Console.Write(">");
if (direction == left) Console.Write("<");
if (direction == up) Console.Write("^");
if (direction == down) Console.Write("v");
if (snakeNewHead.col == food.col && snakeNewHead.row == food.row)
{
// feeding the snake
do
{
food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight),
randomNumbersGenerator.Next(0, Console.WindowWidth));
} while (snakeElements.Contains(food) || obstacles.Contains(food));
lastFoodTime = Environment.TickCount;
Console.SetCursorPosition(food.col, food.row);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("@");
sleepTime--;
Position obstacle = new Position();
do
{
obstacle = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight),
randomNumbersGenerator.Next(0, Console.WindowWidth));
} while (snakeElements.Contains(obstacle) ||
obstacles.Contains(obstacle) ||
(food.row != obstacle.row && food.col != obstacle.col));
obstacles.Add(obstacle);
Console.SetCursorPosition(obstacle.col, obstacle.row);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("=");
}
else
{
// moving
Position last = snakeElements.Dequeue();
Console.SetCursorPosition(last.col, last.row);
Console.Write(" ");
}
if (Environment.TickCount - lastFoodTime >= foodDisappearTime)
{
negativePoints = negativePoints + 50;
Console.SetCursorPosition(food.col, food.row);
Console.Write(" ");
do
{
food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight),
randomNumbersGenerator.Next(0, Console.WindowWidth));
} while (snakeElements.Contains(food) || obstacles.Contains(food));
lastFoodTime = Environment.TickCount;
}
Console.SetCursorPosition(food.col, food.row);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("@");
// Console.Clear();
/*
foreach (Position position in snakeElements)
{
Console.SetCursorPosition(position.col, position.row);
Console.Write("*");
}
Console.SetCursorPosition(food.col, food.row);
Console.Write("@");
*/
sleepTime -= 0.01;
Thread.Sleep((int)sleepTime);
}
}
}
}