-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
170 lines (142 loc) · 3.89 KB
/
index.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
document
.querySelector("#instructions h2")
.addEventListener("click", function () {
document.querySelector("#instructions").style.display = "none";
document.querySelector("#game_container").classList.remove("show_game");
});
//to initiliaze the game board...
function init() {
canvas = document.getElementById("my_canvas");
H = canvas.height = 600;
W = canvas.width = 960;
pen = canvas.getContext("2d");
score = 0;
level = 0;
difficulty = 150;
//cell of the snake body.
cs = 30; //cell size
//game over condition
game_over = true;
foodImg = new Image();
foodImg.src = "images/egg.png";
food = getRandomFood();
snake = {
init_size: 2, //initial size of the snake
snake_color: "white",
cells: [],
direction: "right",
createSnake: function () {
for (let i = 0; i < this.init_size; i++) this.cells.push({ x: i, y: 0 });
},
drawSnake: function () {
pen.fillStyle = this.snake_color;
for (let i = 0; i < this.cells.length; i++)
pen.fillRect(
this.cells[i].x * cs,
this.cells[i].y * cs,
cs - 3,
cs - 3
);
},
updateSnake: function () {
let headX = this.cells[this.cells.length - 1].x;
let headY = this.cells[this.cells.length - 1].y;
if (food.x == headX && food.y == headY) {
food = getRandomFood();
score++;
document.getElementById("display_score").innerHTML = score;
} else this.cells.shift();
let newX = headX + 1;
let newY = headY;
if (snake.direction === "ArrowUp") {
newX = headX;
newY = headY - 1;
} else if (snake.direction === "ArrowDown") {
newX = headX;
newY = headY + 1;
} else if (snake.direction === "ArrowLeft") {
newX = headX - 1;
newY = headY;
} else {
newX = headX + 1;
newY = headY;
}
if (
newX * cs < 0 ||
newY * cs < 0 ||
newX * cs + cs > W ||
newY * cs + cs > H
)
game_over = true;
else this.cells.push({ x: newX, y: newY });
},
};
snake.createSnake();
//handle keyBoard events for snake direction
document.addEventListener("keydown", function (e) {
snake.direction = e.key;
});
document.getElementById("game_start").addEventListener("click", function () {
game_over = false;
f = setInterval(gameLoop, difficulty);
document.getElementById("game_start").style.display = "none";
document.getElementById("game_level").style.display = "none";
});
document.querySelector(".plus").addEventListener("click", function () {
difficulty -= 10;
level++;
document.getElementById("display_level").innerHTML = level;
});
document.querySelector(".minus").addEventListener("click", function () {
difficulty += 10;
level--;
document.getElementById("display_level").innerHTML = level;
});
window.addEventListener(
"keydown",
function (e) {
// space and arrow keys
if (
e.key == "ArrowUp" ||
e.key == "ArrowDown" ||
e.key == "ArrowRight" ||
e.key == "ArrowLeft"
) {
e.preventDefault();
}
},
false
);
}
function draw() {
//clear the previous frame.
pen.clearRect(0, 0, W, H);
pen.shadowBlur = 20;
pen.shadowColor = "black";
snake.drawSnake();
pen.fillStyle = food.color;
pen.drawImage(foodImg, food.x * cs, food.y * cs, cs - 3, cs - 3);
// pen.fillRect(food.x * cs, food.y * cs, cs - 3, cs - 3);
}
function update() {
snake.updateSnake();
}
function getRandomFood() {
let foodX = Math.round((Math.random() * (W - cs)) / cs);
let foodY = Math.round((Math.random() * (H - cs)) / cs);
food = {
x: foodX,
y: foodY,
color: "black",
};
return food;
}
function gameLoop() {
if (game_over) {
clearInterval(f);
alert("Game Over! Your Final Score is " + score);
}
draw();
update();
}
init();