-
Notifications
You must be signed in to change notification settings - Fork 1
/
snake.ts
134 lines (120 loc) · 4.38 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
// ----------------------------------------------------------------------
// snake game based on https://forum.makecode.com/t/snake-and-apples/3027
// ----------------------------------------------------------------------
class SnakeGame {
apples: Sprite[] = []
head: Sprite = null
head_prior: Sprite = null
body: Sprite[] = []
direction: number = 0
game_over: boolean = false;
dir_change: boolean = false;
delay: number = 300;
constructor() {
this.create_head()
this.create_body()
this.create_random_apples()
input.onButtonPressed(Button.B, function () {
if (!this.dir_change)
this.direction = this.direction == 0 ? 3 : this.direction -1;
this.dir_change = true;
})
input.onButtonPressed(Button.A, function () {
if (!this.dir_change)
this.direction = this.direction == 3 ? 0 : this.direction +1;
this.dir_change = true;
})
forever(function () {
if (this.game_over) {
screen.setAll(neopixel.colors(NeoPixelColors.Red));
return;
}
if (this.check_collisions()) {
this.game_over = true;
return;
}
this.move(this.direction);
this.create_display();
this.dir_change = false;
pause(this.delay);
});
}
create_display() {
let border = toRGB(Color.Wine)
let blue = neopixel.colors(NeoPixelColors.Blue)
for(let p=0; p<256; p++) { screen.setRaw(p, blue+p*4 ); }
for(let c=0;c<16;c++) {
screen.setPixel(0, c, border);
screen.setPixel(c, 0, border);
screen.setPixel(15, c, border);
screen.setPixel(c, 15, border);
}
this.apples.forEach(s => { s.draw() });
this.body.forEach(s => { s.draw() });
this.head.draw();
screen.show()
}
check_collisions() {
// collide with wall
if (this.head.x == 0 || this.head.x == 15 || this.head.y == 0 || this.head.y == 15) {
return true;
}
let collideSelf = this.body.filter(b => { return b.x == this.head.x && b.y == this.head.y })
if (collideSelf && collideSelf.length > 0)
return true;
// collide with apple?
let collideApples = this.apples.filter(a => { return a.x == this.head.x && a.y == this.head.y });
if (collideApples && collideApples.length > 0) {
let del_a = collideApples[0]
this.apples.removeElement(del_a);
this.make_longer_snake();
if (this.delay > 80) {
this.delay -= 10;
}
this.create_random_apples();
}
return false;
}
move(d: number) {
this.move_body_where_head_was()
let x_delta = this.direction == 1 ? 1 : this.direction == 3 ? -1 : 0;
let y_delta = this.direction == 0 ? -1 : this.direction == 2 ? 1 : 0;
this.head.setPosition(this.head_prior.x + x_delta, this.head_prior.y + y_delta)
}
create_head () {
this.head = new Sprite(8,8)
this.head.setColor(neopixel.colors(NeoPixelColors.Green))
}
make_body_sprite (x: number, y: number) {
let body_sprite = new Sprite(x, y);
body_sprite.setColor(neopixel.colors(NeoPixelColors.Yellow));
this.body.push(body_sprite)
}
create_body () {
this.body = [];
for (let index = 0; index <= 1; index++) {
this.make_body_sprite(this.head.x, this.head.y + (index + 1))
}
}
make_longer_snake () {
let last = this.body[this.body.length - 1]
let next2last = this.body[this.body.length - 2]
let dx = next2last.x - last.x
let dy = next2last.y - last.y
this.make_body_sprite(last.x + dx, last.y + dy)
}
move_body_where_head_was () {
this.head_prior = this.head
this.body.insertAt(0, this.body.pop())
this.body[0].setPosition(this.head.x, this.head.y)
}
create_random_apples () {
for (let index = 0; index < randint(1, 2); index++) {
if (this.apples.length < 3) {
let apple_sprite = new Sprite(randint(1,14), randint(1,14));
apple_sprite.setColor(NeoPixelColors.Red);
this.apples.push(apple_sprite);
}
}
}
}