-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.js
96 lines (79 loc) · 3.23 KB
/
setup.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
var canvas = document.getElementById("game"),
ctx = canvas.getContext("2d");
var gridWidth = 30,
gridHeight = 20,
//Math.min returns the lowest number among all options
//This line maximizes the size of the game without adding scrollbars
squareSize = Math.min((window.innerWidth - 270) / gridWidth, (window.innerHeight - 20) / gridHeight);
special = false;
var gameSpeed = document.getElementById("speed").value;
document.getElementById("snake").addEventListener("change", function() {
document.getElementById("snakecolor").style.backgroundColor = document.getElementById("snake").value
});
document.getElementById("bg").addEventListener("change", function() {
document.getElementById("bgcolor").style.backgroundColor = document.getElementById("bg").value
});
document.getElementById("apply").addEventListener("click", function() {
gameSpeed = parseInt(document.getElementById("speed").value);
gridWidth = parseInt(document.getElementById("width").value);
gridHeight = parseInt(document.getElementById("height").value);
special = document.getElementById("special").checked;
squareSize = Math.min((window.innerWidth - 270) / gridWidth, (window.innerHeight - 20) / gridHeight);
canvas.width = gridWidth * squareSize;
canvas.height = gridHeight * squareSize;
doGameOver();
Snake.init();
gameLoop(true);
});
var score = 0;
//calculate pixel width and height of the canvas
canvas.width = gridWidth * squareSize;
canvas.height = gridHeight * squareSize;
//always keep the canvas in focus, otherwise the game will not work
canvas.focus();
canvas.addEventListener("blur", function() {
this.focus();
});
ctx.clearCanvas = function() {
ctx.fillStyle = document.getElementById("bg").value;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
var Food = {
x : Math.floor(Math.random() * gridWidth),
y : Math.floor(Math.random() * gridHeight),
type : 0 //type 0 is normal, 1 is special
}
Food.init = function() {
do {
this.x = Math.floor(Math.random() * gridWidth);
this.y = Math.floor(Math.random() * gridHeight);
onSnake = false; //temporary variable to check if it spawned on the snake
for (i = 0; i < Snake.nodes.length; i++) {
if (this.x == Snake.nodes[i][0] && this.y == Snake.nodes[i][1]) {
onSnake = true;
break;
}
}
} while ((this.x == Snake.x && this.y == Snake.y) || onSnake) //try again if the food spawned on the snake
this.type = special ? Math.round(Math.random() - .4) : 0; //if special food is on, small chance of spawning special; otherwise, always spawn normal food
}
var Snake = {
moving : false,
x : 1, //x and y position of the snake in terms of the game grid
y : 1,
dir : 39, //initial direction: right
nodes : [], //2-D array (will store [x, y] in terms of game grid of each node)
curlen : 0, //current length
initialized : true
}
Snake.init = function() {
this.moving = false;
this.x = 1;
this.y = 1;
this.dir = 39;
this.nodes = [];
this.curlen = 0;
this.initialized = true;
score = 0;
Food.init();
}