-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
175 lines (154 loc) · 4.42 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
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
// Render canvas
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 512;
canvas.height = 480;
document.body.appendChild(canvas)
// Styling
canvas.style.border = "1px solid black";
var backgroundColor = "#ffffff";
var lineColor = "#000000";
// Game objects
var snake = {
speed: 256, // pixels per second
maxparts: 50,
direction: 'down',
x: 200,
y: 200,
tail: [], //[(20, 30)]
};
var food = {
x: 100,
y: 100,
radius: 6
};
var foodCaught = 0;
var gameOver = false;
// Key handlers
addEventListener("keydown", function(e) {
keyDown(e.keyCode)
}, false);
var keyDown = function(keycode) {
oldDir = snake.direction;
if (keycode == 38 && !(oldDir == "down")) { snake.direction = "up"; }
else if (keycode == 40 && !(oldDir == "up")) { snake.direction = "down" }
else if (keycode == 37 && !(oldDir == "right")) { snake.direction = "left" }
else if (keycode == 39 && !(oldDir == "left")) { snake.direction = "right" }
}
// Handle the case when we catch a food item
var caughtFood = function() {
foodCaught++;
snake.speed += 30;
snake.maxparts += 50;
food.x = 10 + Math.random() * (canvas.width-20);
food.y = 10 + Math.random() * (canvas.height-20);
console.log(foodCaught);
}
var drawSnake = function() {
var currX = snake.x,
currY = snake.y;
for (i = snake.tail.length - 1; i > 0; i--) {
ctx.beginPath();
ctx.moveTo(currX, currY);
newX = snake.tail[i].x;
newY = snake.tail[i].y;
if (!(Math.abs(newX - currX) > 100 || Math.abs(newY - currY) > 100)){
// If difference is less than 100. Draw it.
ctx.lineTo(newX,newY);
ctx.stroke();
}
currX = newX;
currY = newY;
}
};
var snakePos = function(x, y, dx, dy) {
if (x + dx > canvas.width) {
x = 0;
} else if (x + dx < 0) {
x = canvas.width;
} else if (y + dy > canvas.height) {
y = 0;
} else if (y + dy < 0) {
y = canvas.height;
} else {
x += dx;
y += dy;
}
return {x:x,y:y}
}
var checkCrash = function() {
for (i = 0; i < snake.tail.length; i++) {
if (Math.abs(snake.tail[i].x - snake.x) < 0.9
&& Math.abs(snake.tail[i].y - snake.y) < 0.9) {
gameOver = true;
}
}
}
var moveSnake = function(modifier) {
// Move the snake head
if (snake.direction == "up") { newPos = snakePos(snake.x, snake.y, 0, -snake.speed * modifier) }
else if (snake.direction == "down") { newPos = snakePos(snake.x, snake.y, 0, snake.speed * modifier) }
else if (snake.direction == "left") { newPos = snakePos(snake.x, snake.y, -snake.speed * modifier, 0) }
else if (snake.direction == "right") { newPos = snakePos(snake.x, snake.y, snake.speed * modifier, 0) }
snake.x = newPos.x;
snake.y = newPos.y;
if (Math.abs(snake.x - food.x) < food.radius && Math.abs(snake.y - food.y) < food.radius) {
caughtFood();
}
checkCrash();
// Save the old position
snake.tail.push({x:snake.x,y:snake.y})
// Remove the last element in the tail
if (snake.tail.length > snake.maxparts) {
snake.tail = snake.tail.slice(1)
}
};
var drawFood = function() {
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.arc(food.x,food.y,food.radius,0,2*Math.PI,true);
ctx.closePath();
ctx.fill();
ctx.fillStyle = lineColor;
};
var clearBoard = function() {
ctx.save();
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.restore();
};
var printScore = function() {
ctx.font = "16px Arial";
ctx.fillText("Score: " + foodCaught, canvas.width-90, 20);
ctx.fillText("Speed: " + snake.speed, canvas.width-90, 40);
}
var printGameOver = function() {
ctx.fillStyle = backgroundColor;
ctx.lineWidth = 7;
ctx.strokeStyle = 'black';
ctx.fillRect(canvas.width-425,canvas.height-400,canvas.width-200,canvas.height-200);
ctx.strokeRect(canvas.width-425,canvas.height-400,canvas.width-200,canvas.height-200);
ctx.fillStyle = lineColor;
ctx.font = "bold 30px Arial";
ctx.fillText("GAME OVER", canvas.width-365, canvas.height- 300);
ctx.font = "20px Arial";
ctx.fillText("You played a good game.", canvas.width-380, canvas.height-270);
ctx.fillText("Score: " + foodCaught, canvas.width-310, canvas.height-245);
}
var main = function() {
var now = Date.now();
var delta = now - then;
clearBoard();
moveSnake(delta/1000);
drawSnake();
drawFood();
printScore();
then = now;
if (gameOver){
clearInterval(loop);
printGameOver();
}
}
clearBoard();
var then = Date.now();
var loop = setInterval(main, 1);