-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.js
164 lines (127 loc) · 3.32 KB
/
snake.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
// ! canvas code
const canvas = document.querySelector(".canvas");
const ctx = canvas.getContext("2d");
const scale = 10;
const rows = canvas.height / scale;
const columns = canvas.width / scale;
let snake;
(function setup() {
snake = new Snake();
fruit = new Fruit();
fruit.pickLocation();
window.setInterval(() => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
fruit.draw();
snake.update();
snake.draw();
if (snake.eat(fruit)) {
fruit.pickLocation();
}
snake.checkCollision();
document.querySelector('.score')
.innerText = snake.total;
}, 250);
}());
window.addEventListener('keydown', ((evt) => {
const direction = evt.key.replace('Arrow', '');
snake.changeDirection(direction);
}));
// ! fruit code
function Fruit() {
this.x;
this.y;
this.pickLocation = function() {
this.x = (Math.floor(Math.random() *
columns - 1) + 1) * scale;
this.y = (Math.floor(Math.random() *
rows - 1) + 1) * scale;
}
this.draw = function() {
ctx.fillStyle = "#4cafab";
ctx.fillRect(this.x, this.y, scale, scale)
}
}
// ! Main snake
function Snake() {
this.x = 0;
this.y = 0;
this.xSpeed = scale * 1;
this.ySpeed = 0;
this.total = 0;
this.tail = [];
// increasing the length of the snake
this.draw = function() {
ctx.fillStyle = "#FF2D00";
for (let i = 0; i < this.tail.length; i++) {
ctx.fillRect(this.tail[i].x,
this.tail[i].y, scale, scale);
}
ctx.fillRect(this.x, this.y, scale, scale);
}
// moving snake
this.update = function() {
for (let i = 0; i < this.tail.length - 1; i++) {
this.tail[i] = this.tail[i + 1];
}
this.tail[this.total - 1] = { x: this.x, y: this.y };
this.x += this.xSpeed;
this.y += this.ySpeed;
// snake moves to the other side of the canvas when touching the edge
// snake hitting boundary, game end mechanism yet to be implemented
// if snake moves to the right boundary shifted to the left
if (this.x > canvas.width) {
this.x = 0;
}
// if snake moves to the bottom boundary shifted to the top
if (this.y > canvas.height) {
this.y = 0;
}
// if snake moves to the left boundary shifted to the right
if (this.x < 0) {
this.x = canvas.width;
}
// if snake moves to the top boundary shifted to the bottom
if (this.y < 0) {
this.y = canvas.height;
}
}
this.changeDirection = function(direction) {
switch (direction) {
case 'Up':
this.xSpeed = 0;
this.ySpeed = -scale * 1;
break;
case 'Down':
this.xSpeed = 0;
this.ySpeed = scale * 1;
break;
case 'Left':
this.xSpeed = -scale * 1;
this.ySpeed = 0;
break;
case 'Right':
this.xSpeed = scale * 1;
this.ySpeed = 0;
break;
}
}
// eat fruit and increase the length of the snake
this.eat = function(fruit) {
if (this.x === fruit.x &&
this.y === fruit.y) {
this.total++;
return true;
}
return false;
}
// detect collision with own part of snake length
this.checkCollision = function() {
for (let i = 0; i < this.tail.length; i++) {
if (this.x === this.tail[i].x &&
this.y === this.tail[i].y) {
this.total = 0;
this.tail = [];
}
}
}
}