-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.js
168 lines (135 loc) · 3.66 KB
/
objects.js
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
class Block {
constructor(x, y, w, h, col, row, color, edgeColor) {
this.x = x;
this.y = y;
this.width = w;
this.height = h;
this.column = col;
this.row = row;
this.color = color;
this.edgeColor = edgeColor;
}
draw() {
fill(this.color);
stroke(this.edgeColor);
rect(this.x, this.y, this.width, this.height);
}
}
class Food {
constructor(col, row, blocks, img) {
this.column = col;
this.row = row;
this.blocks = blocks;
this.image = img;
}
draw() {
let b = this.blocks[this.column][this.row];
image(this.image, b.x, b.y, b.width, b.height);
}
}
class Snake {
constructor(shape, directions, blocks, color, edgeColor, foodImage) {
this.shape = shape;
this.directions = directions;
this.blocks = blocks;
this.score = 0;
this.color = color;
this.edgeColor = edgeColor;
this.foodImage = foodImage;
this.generateFood();
}
generateFood() {
checkFood:
while (true) {
let b = random(random(this.blocks))
this.food = new Food(b.column, b.row, this.blocks, this.foodImage);
let spawnedInSnake = false;
for (i = 0; i < this.shape.length; i++) {
if ((this.shape[i][0] === this.food.column) && (this.shape[i][1] === this.food.row)) {
print(`food spawned inside snake at ${[this.food.column, this.food.row]}`);
spawnedInSnake = true;
}
}
if (!spawnedInSnake) {
break checkFood;
}
}
}
grow() {
let pos = this.shape[this.shape.length-1];
let dir = this.directions[this.directions.length-1];
this.shape.push([pos[0] - dir[0], pos[1] - dir[1]]);
this.directions.push(this.directions[this.directions.length-1]);
console.log("growing");
}
draw() {
if (!(this.shape.length == this.directions.length)) {
console.log(`length this.shape(${this.shape.length}) != length this.directions(${this.directions.length})`);
}
for (i = 0; i < this.shape.length; i++) {
let b = this.blocks[this.shape[i][0]][this.shape[i][1]];
let newBlock = new Block(b.x, b.y, b.width, b.height, b.column, b.row, this.color, this.edgeColor);
newBlock.draw();
}
this.food.draw();
}
tick(key) {
switch (key) {
case "ArrowUp":
case "w":
if (!(this.directions[0][0] == 0 && this.directions[0][1] == 1)) {
this.directions[0] = [0, -1]
}
break;
case "ArrowDown":
case "s":
if (!(this.directions[0][0] == 0 && this.directions[0][1] == -1)) {
this.directions[0] = [0, 1]
}
break;
case "ArrowLeft":
case "a":
if (!(this.directions[0][0] == 1 && this.directions[0][1] == 0)) {
this.directions[0] = [-1, 0]
}
break;
case "ArrowRight":
case "d":
if (!(this.directions[0][0] == -1 && this.directions[0][1] == 0)) {
this.directions[0] = [1, 0]
}
break;
}
// push the snake one step further
for (i = 0; i < this.shape.length; i++) {
this.shape[i][0] += this.directions[i][0];
this.shape[i][1] += this.directions[i][1];
}
// check if the snake touched the food
for (i = 0; i < this.shape.length; i++) {
if ((this.shape[i][0] === this.food.column) && (this.shape[i][1] === this.food.row)) {
this.score++;
this.grow();
this.generateFood();
}
}
// check if the snake touches itself or the wall
let strPos = []
for (i = 0; i < this.shape.length; i++) {
if ((this.shape[i][0] < 0) || (this.shape[i][1] < 0) || (this.shape[i][0] > this.blocks.length-1) || (this.shape[i][1] > this.blocks[0].length-1)) {
return -1;
}
let pos = JSON.stringify(this.shape[i])
if (strPos.indexOf(pos) != -1) {
return -1;
}
else {
strPos.push(pos);
}
}
// update directions
for (i = this.directions.length-1; i > 0; i--) {
this.directions[i] = this.directions[i-1];
}
}
}