-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
88 lines (83 loc) · 2.23 KB
/
main.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
function update()
{
var nextMove = (moveList!=null)? moveList[0]:null;
if (nextMove!=null)
{
var moveX = nextMove.coordinates[0]-snake.bits[0][0];
var moveY = nextMove.coordinates[1]-snake.bits[0][1];
moveX = moveX>gridNum-1? -1:moveX%gridNum
moveY = moveY>gridNum-1? -1:moveY%gridNum
var direction = [moveX,moveY];
if (direction[0]!=0 || direction[1]!=0)
snake.direction = direction;
}
if (snake.bits.length == gridNum*gridNum)
console.log("exceded Grid");
snake.move(fruit);
snake.updateSelf();
if (snake.bits[0][0] == fruit.location[0] && snake.bits[0][1] == fruit.location[1])
fruitEaten();
if (moveList!=null)
moveList.shift();
}
function fruitEaten()
{
fruit = new Fruit(snake,gridSize);
pathAI = new A_Star(snake.bits[0],fruit.location,snake);
fruit.drawSelf();
moveList = pathAI.FindPath();
console.log(moveList);
}
function DrawGrid(lineThickness=4)
{
ctx.fillStyle = "#171717";
ctx.beginPath();
ctx.rect(0,0,800,800);
ctx.fill();
ctx.strokeStyle = "#2e2e2e";
ctx.lineWidth = lineThickness;
/*
for (var x = canvas.width; x >= 0; x-=gridSize)
{
ctx.moveTo(x,0);
ctx.lineTo(x,800);
ctx.stroke();
}
for (var y = canvas.height; y >= 0; y-=gridSize)
{
ctx.moveTo(0,y);
ctx.lineTo(800,y);
ctx.stroke();
}
*/
}
function onPress(event)
{
if(event.keyCode == 65 && snake.direction[0] !=1)
snake.direction = [-1,0]
if(event.keyCode == 68 && snake.direction[0] !=-1)
snake.direction = [1,0]
if(event.keyCode == 87 && snake.direction[1] !=1)
snake.direction = [0,-1]
if(event.keyCode == 83 && snake.direction[1] !=-1)
snake.direction = [0,1]
}
var canvas = document.createElement('canvas');
var gridNum = 16;
var gridSize=800/gridNum;
canvas.width = canvas.height = 800;
var ctx = canvas.getContext('2d');
var speed = 100;
var holder = document.getElementById("canvasContainer");
holder.appendChild(canvas);
var snake = new Snake(gridSize);
DrawGrid();
document.addEventListener('keydown',onPress);
snake.drawSelf();
var state = Math.floor(Math.random()*(0x80000000-1))
var fruit = new Fruit(snake,gridSize);
var graph = [];
var pathAI = new A_Star(snake.bits[0],fruit.location,snake);
var moveList = pathAI.FindPath();
var timer = setInterval(update,speed);
//console.log(new Node([5,5],{g:-1},[10,10]));