-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvasGame.js
More file actions
128 lines (104 loc) · 3.42 KB
/
canvasGame.js
File metadata and controls
128 lines (104 loc) · 3.42 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
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
function game() {
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
ctx.font = '15px Arial';
window.addEventListener('keydown',handleEvent);
let w = canvas.width;
let h = canvas.height;
let cellWidth = 10;
let direction;
let food;
let score;
let snake_array;
function init() {
direction = 'right';
createSnake();
createFood();
score =0;
if(typeof(game_loop) !='undefined') clearInterval(game_loop);
game_loop = setInterval(draw, 60);
}
init();
function createSnake() {
let length = 5;
snake_array=[];
for(let i=length-1;i>=0;i--){
snake_array.push({x:i,y:0});
}
}
function createFood() {
food ={
x:Math.round(Math.random() * (w-cellWidth)/cellWidth),
y:Math.round(Math.random() * (h-cellWidth)/cellWidth)
}
}
function draw() {
ctx.fillStyle = 'lightgray';
ctx.fillRect(0,0,w,h);
ctx.strokeStyle = "black";
ctx.strokeRect(0, 0, w, h);
let nx = snake_array[0].x;
let ny = snake_array[0].y;
if(direction == 'right') nx++;
else if(direction =='left') nx--;
else if(direction =='up') ny--;
else if(direction=='down')ny++;
//checks if the snakes hits one of the walls
//if so, the game restarts
if(nx==-1 || nx== w/cellWidth || ny==-1 || ny==h/cellWidth || checkCollision(nx,ny,snake_array)){
//restart game
init();
return;
}
//moving the snakes comes when the tail(or the last element)
//come before the first element (or the head)
//if the snake eats food, we push the tail in the snake array
if(nx == food.x && ny == food.y){
var tail ={x:nx,y:ny};
score++;
createFood();
}
else{
var tail = snake_array.pop();
tail.x =nx;
tail.y =ny;
}
snake_array.unshift(tail);
for(let i=0;i<snake_array.length;i++){
let singleSnakeCell = snake_array[i];
paintSnakeCell(singleSnakeCell.x,singleSnakeCell.y);
}
paintFoodCell(food.x,food.y);
ctx.fillStyle='black';
ctx.fillText(`Your score is: ${score}`,5,h-5);
}
function paintSnakeCell(x, y) {
ctx.fillStyle = 'blue';
ctx.fillRect(x*cellWidth,y*cellWidth,cellWidth,cellWidth);
}
function paintFoodCell(x,y) {
ctx.fillStyle = 'red';
ctx.fillRect(x*cellWidth,y*cellWidth,cellWidth,cellWidth);
}
function checkCollision(x, y, array) {
for(var i = 0; i < array.length; i++)
{
if(array[i].x == x && array[i].y == y)
return true;
}
return false;
}
function handleEvent(event) {
switch (event.code){
case "ArrowLeft": if(direction != 'right'){direction='left'}
break;
case "ArrowRight": if(direction != 'left'){direction='right'}
break;
case "ArrowUp": if(direction != 'down'){direction='up'}
break;
case "ArrowDown": if(direction != 'up'){direction='down'}
break;
}
}
}
game();