-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.js
135 lines (119 loc) · 4.7 KB
/
game.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
gameLoop(true);
var turnedThisFrame = false; //used to disallow turning twice in one frame
function gameLoop(firstrun) {
turnedThisFrame = false;
ctx.clearCanvas(); //defined in setup.js
if (Snake.moving) {
Snake.nodes.unshift([Snake.x, Snake.y]); //create a new node
while (Snake.nodes.length > Snake.curlen) //delete oldest nodes
Snake.nodes.pop();
switch (Snake.dir) {
case 37: //left
Snake.x--;
break;
case 38: //up
Snake.y--;
break;
case 39: //right
Snake.x++;
break;
case 40: //down
Snake.y++;
break;
default:
console.log("error");
break;
}
}
//draw the snake
ctx.fillStyle = document.getElementById("snake").value;
ctx.fillRect(Snake.x * squareSize, Snake.y * squareSize, squareSize, squareSize);
onSnake = false; //temporary variable
for (i = 0; i < Snake.nodes.length; i++) {
ctx.fillRect(Snake.nodes[i][0] * squareSize, Snake.nodes[i][1] * squareSize, squareSize, squareSize);
if (Snake.nodes[i][0] == Snake.x && Snake.nodes[i][1] == Snake.y) {
onSnake = true;
//breaking out of the loop would result in only part of the snake being drawn, so I use a temporary variable here
}
}
if (onSnake) doGameOver();
//draw pellet, but smaller
if (Food.type == 1) {
ctx.strokeStyle = document.getElementById("snake").value;
ctx.strokeRect(Food.x * squareSize + (.25 * squareSize), Food.y * squareSize + (.25 * squareSize), squareSize / 2, squareSize / 2);
} else {
ctx.fillStyle = document.getElementById("snake").value;
ctx.fillRect(Food.x * squareSize + (.25 * squareSize), Food.y * squareSize + (.25 * squareSize), squareSize / 2, squareSize / 2);
}
//print score
ctx.textAlign = "left";
ctx.fillStyle = "#FFF";
ctx.font = "12px Consolas";
ctx.fillText("Length: " + (Snake.curlen + 1), 10, canvas.height - 25);
ctx.fillText(" Score: " + score, 10, canvas.height - 10);
//collision detection
if (Snake.x >= gridWidth || Snake.x < 0 || Snake.y >= gridHeight || Snake.y < 0) {
doGameOver();
} else if (Snake.x == Food.x && Snake.y == Food.y) {
if (Food.type == 0) {
Snake.curlen++;
} else {
Snake.curlen = Math.round(Snake.curlen * (3 / 4));
score += 10;
}
Food.init();
score += parseInt(gameSpeed);
}
//print instructions if on setup
if (firstrun) {
ctx.textAlign = "center";
ctx.fillStyle = "rgba(0,0,0,.4)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#FFF";
ctx.font = "20px Verdana";
ctx.fillText("Space to start", canvas.width / 2, canvas.height / 2);
}
if (Snake.moving) {
setTimeout(gameLoop, 220 - gameSpeed * 7);
} else {
if (Snake.initialized && !firstrun) { //if still playing, draw pause screen
ctx.textAlign = "center";
ctx.fillStyle = "rgba(0,0,0,.4)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#FFF";
ctx.font = "20px Verdana";
ctx.fillText("paused", canvas.width / 2, canvas.height / 2);
}
}
}
canvas.addEventListener("keydown", function(e){
if (Snake.moving) {
//does not let the player turn directly around
if ((e.keyCode == 37 && Snake.dir !== 39) || (e.keyCode == 39 && Snake.dir !== 37) || (e.keyCode == 38 && Snake.dir !== 40) || (e.keyCode == 40 && Snake.dir !== 38))
if (turnedThisFrame == false) {
Snake.dir = e.keyCode;
turnedThisFrame = true;
}
}
if (e.keyCode == 32) {
Snake.moving = !Snake.moving;
if (Snake.moving) {
if (Snake.initialized) {
gameLoop(); //unpause if already initialized (player was paused)
} else {
Snake.init();
gameLoop(true); //reset if not (player died)
}
}
}
});
function doGameOver() {
Snake.initialized = false;
Snake.moving = false;
ctx.textAlign = "center";
ctx.fillStyle = "rgba(0,0,0,.4)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#FFF";
ctx.font = "20px Verdana";
ctx.fillText("Game over (press space)", canvas.width / 2, canvas.height / 2);
}