-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
190 lines (168 loc) · 6.26 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
class Program {
static void Main(string[] args) {
// window width must be 2x the window height to make a square
Console.WindowHeight = 20;
Console.WindowWidth = 40;
Console.Clear();
renderWalls();
// delay between each frame in milliseconds
const int delay = 75;
Snake snake = new Snake('■', ConsoleColor.Green);
snake.xpos = 10;
snake.ypos = 4;
snake.direction = Direction.Down;
Apple apple = new Apple('■', ConsoleColor.Red);
apple.setPosition();
List<Body> snakeBody = new List<Body>();
while (true){
if (Console.KeyAvailable) {
ConsoleKeyInfo key = Console.ReadKey(true);
switch(key.Key) {
case ConsoleKey.W:
if (snake.direction != Direction.Down) {
snake.direction = Direction.Up;
}
break;
case ConsoleKey.S:
if (snake.direction != Direction.Up) {
snake.direction = Direction.Down;
}
break;
case ConsoleKey.D:
if (snake.direction != Direction.Left) {
snake.direction = Direction.Right;
}
break;
case ConsoleKey.A:
if (snake.direction != Direction.Right) {
snake.direction = Direction.Left;
}
break;
}
}
// derender everything so it can be re-rendered later
snake.derender();
apple.derender();
foreach (Body piece in snakeBody) {
piece.derender();
}
// add a new piece to the body
Body newPiece = new Body('■', ConsoleColor.Green);
newPiece.xpos = snake.xpos;
newPiece.ypos = snake.ypos;
snakeBody.Add(newPiece);
// Move the snake according to its direction
switch (snake.direction) {
case Direction.Up:
snake.ypos -= 1;
break;
case Direction.Down:
snake.ypos += 1;
break;
// snake must move twice as fast on the x-axis because characters written horizontally are 2x closer than vertically
case Direction.Right:
snake.xpos += 2;
break;
case Direction.Left:
snake.xpos -= 2;
break;
}
// remove the last piece of the body, unless the snake touched the apple
if (snake.collision(apple)) {
apple.setPosition();
} else {
snakeBody.RemoveAt(0);
}
// check if the snake ran into any walls
if (snake.xpos <= 1 || snake.xpos >= (Console.WindowWidth) || snake.ypos <= 0 || snake.ypos >= (Console.WindowHeight)) {
break;
}
// render all objects
snake.render();
apple.render();
// render each body piece AND check if the head collided with any of the body parts
foreach (Body piece in snakeBody) {
if (snake.collision(piece)) {
// the notorious goto
// this just jumps out of the while loop
goto End;
}
piece.render();
}
// keep the cursor up in the corner
Console.SetCursorPosition(0, 0);
System.Threading.Thread.Sleep(delay);
}
End:
Console.Clear();
Console.WriteLine("Final Score: " + snakeBody.Count);
}
public static void renderWalls() {
for (int i = 0; i < Console.WindowWidth; i++) {
Console.ForegroundColor = ConsoleColor.Cyan;
Console.SetCursorPosition(i, 0);
Console.Write('I');
Console.SetCursorPosition(i, Console.WindowHeight);
Console.Write('I');
}
for (int i = 0; i < Console.WindowHeight; i++) {
Console.ForegroundColor = ConsoleColor.Cyan;
Console.SetCursorPosition(0, i);
Console.Write('I');
Console.SetCursorPosition(Console.WindowWidth, i);
Console.Write('I');
}
}
}
enum Direction {
Up,
Down,
Left,
Right
}
// Base class that handles rendering/derendering of all on-screen objects, among other things
class Object {
private char character;
private ConsoleColor color;
public Object(char c, ConsoleColor clr) {
character = c;
color = clr;
}
public int xpos { get; set; }
public int ypos { get; set; }
public void derender() {
Console.SetCursorPosition(xpos, ypos);
Console.Write(" ");
}
public void render() {
Console.ForegroundColor = color;
Console.SetCursorPosition(xpos, ypos);
Console.Write(character);
Console.ForegroundColor = ConsoleColor.Black;
}
public bool collision(Object other) {
return xpos == other.xpos && ypos == other.ypos;
}
}
class Snake : Object {
public Snake (char c, ConsoleColor clr) : base(c, clr) {}
public Direction direction { get; set; }
}
class Apple : Object {
public Apple (char c, ConsoleColor clr) : base(c, clr) {}
public void setPosition() {
Random random = new Random();
// ensure apple lands on an even-number x position, since the snake only exists on even-numbered x coordinates
xpos = 2 * random.Next(2, Console.WindowWidth/2);
ypos = random.Next(1, Console.WindowHeight);
}
}
class Body : Object {
public Body (char c, ConsoleColor clr) : base(c, clr) {}
}