-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
213 lines (187 loc) · 6.8 KB
/
script.js
File metadata and controls
213 lines (187 loc) · 6.8 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*----- variables -----*/
let snake = [
{x: 300, y: 100, height: 20, width: 20, color: 'red'},
{x: 300, y: 70, height: 20, width: 20, color: 'red'},
{x: 300, y: 40, height: 20, width: 20, color: 'red'},
];
let direction = null;
let autoMoveInterval = setInterval(autoMoveSnake, 100);
let gameInterval = setInterval(gameloop, 50);
let points = 0;
let highPoints = 0;
/* ----- DOM SELECTORS ------ */
const canvas = document.querySelector('canvas');
const message = document.querySelector('.gameover')
const button = document.querySelector('button')
const score = document.querySelector('.score');
const highScore = document.querySelector('.highscore');
/* ----- CANVAS SETUP ------- */
const ctx = canvas.getContext('2d')
console.log(ctx);
canvas.setAttribute('height', getComputedStyle(canvas).height);
canvas.setAttribute('width', getComputedStyle(canvas).width);
/* ----- CLASSES ------ */
class Mouse {
constructor(x,y,width,height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.alive = true;
this.img = new Image()
this.img.src = '—Pngtree—mouse running away vector material_5756793.png'
}
on_image_loaded(f) {
this.img.onload = f;
}
render(){
ctx.drawImage(this.img, this.x, this.y, this.width, this.height)
}
}
/* ----- BOARD OBJECTS ------ */
const jerry = new Mouse (100,300,100,100)
jerry.on_image_loaded(function() {
jerry.render()
});
/*----- event listeners -----*/
document.addEventListener('keydown', moveSnake);
button.addEventListener('click', reset);
/*----------------------------------- functions --------------------------------------*/
function reset() {
// clearInterval(autoMoveInterval);
ctx.clearRect(0, 0, canvas.width, canvas.height);
snake = [
{x: 300, y: 100, height: 20, width: 20, color: 'red'},
{x: 300, y: 70, height: 20, width: 20, color: 'red'},
{x: 300, y: 40, height: 20, width: 20, color: 'red'},
];
direction = null;
points = 0;
score.innerHTML = `Score: 0`;
jerry.x = Math.floor(Math.random() * (canvas.width - jerry.width));
jerry.y = Math.floor(Math.random() * (canvas.height - jerry.height));
message.innerText = '';
autoMoveInterval = setInterval(autoMoveSnake, 100);
gameInterval = setInterval(gameloop, 50);
document.addEventListener('keydown', moveSnake);
console.log('reset interval')
}
function gameloop () {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawSnake(snake, ctx);
jerry.render();
collisionDetect(snake[0].x ,snake[0].y, jerry);
}
function drawSnake (snake, ctx){
snake.forEach(segment => {
ctx.fillStyle = segment.color;
ctx.fillRect(segment.x, segment.y, segment.width, segment.height);
});
}
function moveSnake (e){
switch(e.key.toLowerCase()) {
case 'w':
if (direction === 'down' || direction === null){
return;
} else {
direction = 'up';
}
break;
case 's':
if (direction === 'up'){
return;
} else {
direction = 'down';
}
break;
case 'd':
if (direction === 'left'){
return;
} else {
direction ='right';
}
break;
case 'a':
if (direction === 'right'){
return;
} else {
direction = 'left';
}
break;
default: console.log(`${e.key} not recognized`)
}
}
function autoMoveSnake(){
const speed = 30;
let snakeHeadx = snake[0].x;
let snakeHeady = snake[0].y;
const snakeTail = snake[snake.length];
if (direction === 'null') {
return;
} else if (direction === 'up'){
let newHeadup = {x: snakeHeadx, y: snakeHeady -= speed, height: 20, width: 20, color: 'red'};
snake.unshift(newHeadup);
snake.pop(snakeTail);
drawSnake(snake, ctx);
} else if (direction === 'down'){
let newHeaddown = {x: snakeHeadx, y: snakeHeady += speed, height: 20, width: 20, color: 'red'};
snake.unshift(newHeaddown);
snake.pop(snakeTail);
drawSnake(snake, ctx);
} else if (direction === 'right'){
let newHeadright = {x: snakeHeadx += speed, y: snakeHeady, height: 20, width: 20, color: 'red'};
snake.unshift(newHeadright);
snake.pop(snakeTail);
drawSnake(snake, ctx);
} else if (direction === 'left') {
let newHeadleft = {x: snakeHeadx -= speed, y: snakeHeady, height: 20, width: 20, color: 'red'};
snake.unshift(newHeadleft);
snake.pop(snakeTail);
drawSnake(snake, ctx);
}
}
function collisionDetect (snakeHeadx,snakeHeady, jerry) {
snakeHeadx = snake[0].x;
snakeHeady = snake[0].y;
let snakeHeadHeight = snake[0].height;
let snakeHeadWidth = snake[0].width;
const foodTop = snakeHeady + snakeHeadHeight > jerry.y;
const foodBottom = snakeHeady < jerry.y + jerry.height;
const foodLeft = snakeHeadx + snakeHeadWidth > jerry.x;
const foodRight = snakeHeadx < jerry.x + jerry.width;
if (foodBottom && foodLeft && foodRight && foodTop) {
jerry.x= Math.floor(Math.random() * (canvas.width - jerry.width));
jerry.y = Math.floor(Math.random() * (canvas.height - jerry.height));
jerry.render();
const lastSegment = snake[snake.length - 1]
const newSegment = {x:lastSegment.x, y: lastSegment.y, height: 20, width: 20, color:'red'}
snake.push(newSegment);
drawSnake(snake, ctx);
points++;
score.innerHTML = `Score: ${points}`;
if (points > highPoints){
highPoints = points;
highScore.innerHTML= `High Score: ${highPoints}`;
} else if (points < highPoints) {
highScore.innerHTML= `High Score: ${highPoints}`;
}
return true;
}
if (snakeHeady <= 0 || snakeHeadx <= 0 ||snakeHeadx + snakeHeadWidth >= canvas.width ||snakeHeady + snakeHeadHeight >= canvas.height) {
document.removeEventListener('keydown', moveSnake);
message.innerText = 'GAME OVER!!';
clearInterval(autoMoveInterval);
console.log('removed')
}
for (let i = 1; i < snake.length; i++) {
const segment = snake[i];
if (snakeHeadx === segment.x && snakeHeady === segment.y) {
document.removeEventListener('keydown', moveSnake);
message.innerText = 'GAME OVER!!';
clearInterval(autoMoveInterval);
console.log('removed-body')
return;
}
}
return false;
}