This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.ts
216 lines (159 loc) · 5.75 KB
/
Snake.ts
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
class Snake {
private direction: Vector;
private parts: BiDirectionalLinkedList<SnakePart> = new BiDirectionalLinkedList([]);
private score: number = 0;
get head() {
return this.parts.head;
}
get tail() {
return this.parts.tail;
}
constructor(parts?: BiDirectionalLinkedList<SnakePart>, direction?: Vector) {
this.parts = parts;
if (!this.parts) {
this.parts = Snake.generateRandomParts(startingSnakeLength);
}
this.direction = direction;
if (!this.direction) {
const directions = cloneArray(Vector.cardinalDirections);
for (let i = 0; i < directions.length; i++) {
const clone = this.head.value.clone().addVector(directions[i]);
if (this.isOnPosition(clone)) {
directions.splice(i, 1);
i--;
}
}
this.direction = chooseRandomly(directions);
}
loops.everyInterval(snakeMoveIntervalMilliseconds, () => this.move())
input.onButtonPressed(Button.A, () => snake.direction.rotate(-90 * degreesToRadians).round());
input.onButtonPressed(Button.B, () => snake.direction.rotate(90 * degreesToRadians).round());
}
private move() {
if (this.hasCollectedCollectible()) {
this.onCollectedCollectible();
}
for (let i = this.parts.length - 1; i >= 0; i--) {
const part = this.parts.elements[i];
const isFirstPart = part === this.head;
const isLastPart = part === this.tail;
if (isLastPart) {
led.unplot(part.value.x, part.value.y);
}
if (isFirstPart) {
part.value.addVector(this.direction);
ledSquare.wrapAround(part.value);
} else {
part.value = part.previous.value.clone();
}
}
if (this.isTouchingSelf()) {
this.gameOver();
return;
}
if (this.hasCollectedCollectible()) {
this.onCollectedCollectible();
}
this.draw();
}
private isTouchingSelf() {
for (const part of this.parts.elements) {
const partsTouchingPosition = this.getPartsTouchingPosition(part.value);
for (const partTouchingPosition of partsTouchingPosition) {
if (partTouchingPosition === part) {
continue;
}
return true;
}
}
return false;
}
private draw() {
for (const part of this.parts.elements) {
led.plot(part.value.x, part.value.y);
}
}
private onCollectedCollectible() {
this.score++;
Collectible.instance.collect();
this.grow();
}
private grow() {
const tail = this.tail;
let directions = cloneArray(Vector.cardinalDirections);
for (let i = 0; i < directions.length; i++) {
const direction = directions[i];
const cloned = ledSquare.wrapAround(this.tail.value.clone().addVector(direction));
const isInvalidPosition = this.isOnPosition(cloned);
if (isInvalidPosition) {
directions.splice(i, 1);
i--;
}
}
if (directions.length === 0) {
this.gameOver();
return;
}
let selectedDirection = chooseRandomly(directions);
for (const direction of directions) {
if (this.isOnPosition(this.tail.value.clone().addVector(direction.clone().negative()))) {
selectedDirection = direction;
}
}
const newPart = ledSquare.wrapAround(tail.value.clone().addVector(selectedDirection));
this.parts.push(newPart);
}
private gameOver() {
basic.showString("GAME OVER! SCORE: " + this.score);
}
public hasCollectedCollectible() {
return this.isOnPosition(Collectible.instance.position);
}
public isOnPosition(position: Vector) {
return this.getPartsTouchingPosition(position).length !== 0;
}
public getPartsTouchingPosition(position: Vector) {
const parts = [];
for (const part of this.parts.elements) {
if (part.value.equals(position)) {
parts.push(part);
}
}
return parts;
}
public toString() {
let string = "[";
for (const position of this.parts.elements) {
string += position.value.toString();
}
string += "]";
return string;
}
public static generateRandomParts(partCount: number): BiDirectionalLinkedList<SnakePart> {
let currentPosition = Vector.getRandomVector(minimumX, minimumY, maximumX, maximumY);
const parts: BiDirectionalLinkedList<SnakePart> = new BiDirectionalLinkedList(
[{
value: currentPosition.clone(),
next: null,
previous: null
}]
)
for (let i = 1; i < partCount; i++) {
const directions = cloneArray(Vector.cardinalDirections);
for (let i = 0; i < directions.length; i++) {
const direction = directions[i];
const positionClone = currentPosition.clone();
positionClone.addVector(direction);
const isOutOfBounds = !ledSquare.includes(positionClone);
if (isOutOfBounds) {
directions.splice(i, 1);
i--;
}
}
const direction = chooseRandomly(directions);
currentPosition.addVector(direction);
parts.push(currentPosition.clone());
}
return parts;
}
}