-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathgame.js
More file actions
63 lines (53 loc) · 1.36 KB
/
game.js
File metadata and controls
63 lines (53 loc) · 1.36 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
var gameStarted = false;
$( document ).ready(function() {
showIntro();
// Start game on spacebar press.
this.onkeypress = function(e) {
if (gameStarted == false && e.keyCode == 32) { // 32 = Spacebar
gameStarted = true;
gamerun();
}
}
});
function gamerun() {
init();
}
function step(){
update();
draw();
}
function update() {
if (!movesnake()) {
die();
showConclusion(size)
}
}
function draw() {
if (gameStarted) {
screenclear();
drawsnake();
drawfood();
}
}
function showIntro() {
var canvas = document.getElementById("canvas");
var ctx=canvas.getContext("2d");
ctx.font="30px Arial";
ctx.fillStyle = "white";
ctx.textAlign = "center";
ctx.fillText("SNAKE", canvas.width/2, canvas.height/2);
ctx.font="20px Arial";
ctx.fillText("press space to start", canvas.width/2, canvas.height/2+40);
}
function showConclusion(score) {
screenclear();
var canvas = document.getElementById("canvas");
var ctx=canvas.getContext("2d");
ctx.font="30px Arial";
ctx.fillStyle = "white";
ctx.textAlign = "center";
ctx.fillText("GAME OVER", canvas.width/2, canvas.height/2);
ctx.fillText("score: " + score, canvas.width/2, canvas.height/2-40);
ctx.font="20px Arial";
ctx.fillText("press space to start", canvas.width/2, canvas.height/2+80);
}