-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript1.js
More file actions
382 lines (320 loc) · 12.4 KB
/
script1.js
File metadata and controls
382 lines (320 loc) · 12.4 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
const cvs = document.getElementById("myCanvas");
const ctx = cvs.getContext("2d");
cvs.width = 400;
cvs.height = 400;
let frames = 0;
let foodEaten = false;
let openSet = [];
let closedSet = [];
let current;
let path = [];
const direction = {
current : 0,
idle : 0,
right : 1,
down : 2,
left : 3,
up : 4
}
document.addEventListener("keydown", function(evt){
switch(evt.keyCode){
case 37:
//move left
if(direction.current != direction.left && direction.current != direction.right) direction.current = direction.left;
break;
case 38:
//move up
if(direction.current != direction.up && direction.current != direction.down) direction.current = direction.up;
break;
case 39:
//move right
if(direction.current != direction.right && direction.current != direction.left) direction.current = direction.right;
break;
case 40:
//move down
if(direction.current != direction.down && direction.current != direction.up) direction.current = direction.down;
break;
}
});
function getDistance(pointX1, pointY1, pointX2, pointY2) {
let distanceX = pointX2 - pointX1;
let distanceY = pointY2 - pointY1;
return Math.sqrt(Math.pow(distanceX,2) + Math.pow(distanceY,2));
}
const grid = {
position: new Array(20),
init: function() {
for (let i = 0; i < 20; i++) {
this.position[i] = new Array(20);
}
for (let i = 0; i < 20; i++) {
for (let j = 0; j < 20; j++) {
this.position[i][j] = {i: i, j: j, x: j * 20 + 10, y: i * 20 + 10, g: 0, h: 0, f: 9999999, cameFrom: 0};
}
}
},
draw: function() {
for (let i = 0; i < 20; i++) {
for (let j = 0; j < 20; j++) {
ctx.beginPath();
// Set the grid line color and width here
ctx.strokeStyle = "green"; // Change the color of the grid lines
ctx.lineWidth = 0.1; // Set the grid line width
ctx.rect(this.position[i][j].x - 10, this.position[i][j].y - 10, 20, 20); // Draw the grid cell
ctx.stroke();
ctx.closePath();
}
}
},
getHValue : function(node, goal){
let diffX = goal.x - node.x;
let diffY = goal.y - node.y;
return Math.sqrt(Math.pow(diffX,2) + Math.pow(diffY,2));
},
addNeighbour : function(neighbour){
let isNeighbourSnake = false;
// if the neighbour is snake
for(let i=0; i<snake.position.length; i++){
if(neighbour.x == snake.position[i].x && neighbour.y == snake.position[i].y){
isNeighbourSnake = true;
break;
}
}
if(!isNeighbourSnake){
let tempG = current.g + 1;
let tempH = this.getHValue(neighbour, food);
let tempF = tempG + tempH;
if(tempF < neighbour.f){
neighbour.cameFrom = current;//{i : current.i, j : current.j};
neighbour.g = tempG;
neighbour.h = tempH;
neighbour.f = tempF;
}
if(!openSet.includes(neighbour)){
openSet.push(neighbour);
}
}
},
aStar : function(){
let p = Math.floor((snake.position[0].y - 10)/20);
let q = Math.floor((snake.position[0].x - 10)/20);
openSet.push(grid.position[p][q]);
while(openSet.length > 0){
current = openSet[0];
openSet.splice(0,1);
closedSet.push(current);
if(current.x == food.x && current.y == food.y){
break;
}
// for all neighbours
// i, j+1
if(current.j < 19){
let neighbour = this.position[current.i][current.j+1];
if (!closedSet.includes(neighbour)){
this.addNeighbour(neighbour);
}
}
if(current.i < 19){
let neighbour = this.position[current.i + 1][current.j];
if (!closedSet.includes(neighbour)){
this.addNeighbour(neighbour);
}
}
if(current.j > 0){
let neighbour = this.position[current.i][current.j-1];
if (!closedSet.includes(neighbour)){
this.addNeighbour(neighbour);
}
}
if(current.i > 0){
let neighbour = this.position[current.i - 1][current.j];
if (!closedSet.includes(neighbour)){
this.addNeighbour(neighbour);
}
}
// bring the cell with lowest F value to the index 0 in openSet
for(let r = openSet.length -1 ; r >0 ; r--){
if(openSet[r].f < openSet[r-1].f){
let temp = openSet[r-1];
openSet[r-1] = openSet[r];
openSet[r] = temp;
}
}
}
path.splice(0);
this.findPath();
},
findPath : function(){
let previous = '';
for(let i = closedSet.length - 1; i>=0; i--){
if(i == closedSet.length - 1 && previous == ''){
previous = closedSet[i];
path.push(previous);
}
else if(previous.cameFrom.i == closedSet[i].i && previous.cameFrom.j == closedSet[i].j){
previous = closedSet[i];
path.push(previous);
}
}
},
drawPath : function(){
ctx.strokeStyle = "blue";
for(let i=0; i<path.length -1; i++){
ctx.beginPath();
ctx.moveTo(path[i].x, path[i].y);
ctx.lineTo(path[i+1].x, path[i+1].y);
ctx.stroke();
ctx.closePath();
}
}
}
const foodImage = new Image();
foodImage.src = './body/neonapple.png ';
const food = {
x : 10,
y : 10,
r : 10,
draw : function(){
ctx.drawImage(foodImage, this.x - this.r, this.y - this.r, this.r * 2, this.r * 2);
},
update : function(){
if(frames % 6 == 0){
if(direction.current == direction.right) this.x += 19;
if(direction.current == direction.left) this.x -= 19;
if(direction.current == direction.up) this.y -= 19;
if(direction.current == direction.down) this.y += 19;
if(this.x < 0 ) this.x = cvs.width - 10;
if(this.x > cvs.width ) this.x = 10;
if(this.y < 0 ) this.y = cvs.height - 10;
if(this.y > cvs.height ) this.y = 10;
}
}
}
let score = 0;
const scoreDisplay = {
x: 10,
y: 30,
draw: function () {
ctx.fillStyle = "white"; // Set font color to white
ctx.font = "20px Arial";
ctx.fillText(`Score: ${score}`, this.x, this.y);
}
};
const snakeHeadImage = new Image();
snakeHeadImage.src = './body/head.png ';
const snakeBodyImage = new Image();
snakeBodyImage.src = './body/bodyyy.png ';
const snake = {
radius: 10,
position: [{x: 110, y: 110}],
snakeHeadIndex: 0, // Start with the head being the first element in the position array
draw: function() {
// Draw the snake body
for (let i = 1; i < this.position.length; i++) { // Start from 1 to skip the head
let p = this.position[i];
ctx.drawImage(snakeBodyImage, p.x - this.radius, p.y - this.radius, this.radius * 2, this.radius * 2); // Draw body using image
}
if (getDistance(food.x, food.y, this.position[0].x, this.position[0].y) <= 2 * food.r) {
let newFoodIndexI = Math.floor(Math.random() * 20);
let newFoodIndexJ = Math.floor(Math.random() * 20);
food.x = grid.position[newFoodIndexI][newFoodIndexJ].x;
food.y = grid.position[newFoodIndexI][newFoodIndexJ].y;
this.position.push({
x: this.position[this.position.length - 1].x,
y: this.position[this.position.length - 1].y,
});
// Increment the score when food is eaten
score += 1;
// Create new aStar path
openSet.splice(0);
closedSet.splice(0);
grid.init();
grid.aStar();
this.snakeHeadIndex = path.length - 1;
}
// Draw the snake head using an image
let headPosition = this.position[0];
ctx.drawImage(snakeHeadImage, headPosition.x - this.radius, headPosition.y - this.radius, this.radius * 2, this.radius * 2);
},
update : function() {
if(frames % 6 == 0){
if(foodEaten == true){
this.position.push({
x : this.position[this.position.length -1].x,
y : this.position[this.position.length -1].y
});
foodEaten = false;
}
if(this.position[0].x < 0 ) this.position[0].x = cvs.width - 10;
if(this.position[0].x > cvs.width ) this.position[0].x = 10;
if(this.position[0].y < 0 ) this.position[0].y = cvs.height - 10;
if(this.position[0].y > cvs.height ) this.position[0].y = 10;
for( let i = this.position.length -1; i > 0; i--){
if(this.position[0].x == this.position[i].x && this.position[0].y == this.position[i].y && this.position.length > 2) {
this.position.splice(1);
break;
}
this.position[i].x = this.position[i-1].x;
this.position[i].y = this.position[i-1].y;
}
/*if(direction.current == direction.right) {
this.position[0].x += 20;
}
if(direction.current == direction.left) {
this.position[0].x -= 20;
}
if(direction.current == direction.up) {
this.position[0].y -= 20;
}
if(direction.current == direction.down) {
this.position[0].y += 20;
}; */
// snake ate food
if(getDistance(food.x,food.y,this.position[0].x, this.position[0].y) <= 2*food.r){
let newFoodIndexI = Math.floor(Math.random() * 20);
let newFoodIndexJ = Math.floor(Math.random() * 20);
food.x = grid.position[newFoodIndexI][newFoodIndexJ].x;
food.y = grid.position[newFoodIndexI][newFoodIndexJ].y;
//foodEaten = true;
this.position.push({
x : this.position[this.position.length -1].x,
y : this.position[this.position.length -1].y
});
// create new aStar path
openSet.splice(0);
closedSet.splice(0);
//this.snakeHeadIndex = path.length-1; //1;
grid.init();
grid.aStar();
this.snakeHeadIndex = path.length-1; //1;
}
// move snake along the aStar path
if(this.snakeHeadIndex < 0) this.snakeHeadIndex = path.length - 1;
this.position[0].x = path[path.length - 2].x; // closedSet[this.snakeHeadIndex].x;
this.position[0].y = path[path.length - 2].y; //closedSet[this.snakeHeadIndex].y;
if(this.snakeHeadIndex > 0) this.snakeHeadIndex--; //this.snakeHeadIndex++;
// create new aStar path
openSet.splice(0);
closedSet.splice(0);
//this.snakeHeadIndex = path.length-1; //1;
grid.init();
grid.aStar();
this.snakeHeadIndex = path.length-1; //1;
}
}
}
function main() {
ctx.clearRect(0, 0, cvs.width, cvs.height);
snake.update();
snake.draw();
food.update();
food.draw();
grid.draw();
grid.drawPath();
scoreDisplay.draw(); // Display the score
frames++;
requestAnimationFrame(main);
}
grid.init();
grid.aStar();
requestAnimationFrame(main);