-
Notifications
You must be signed in to change notification settings - Fork 12
/
game-1.js
173 lines (150 loc) · 4.08 KB
/
game-1.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
171
172
let f = false
let start;
// TODO: finish timing code???
const c = document.getElementById('c');
c.width = 640;
c.height = 640;
let point = 0;
const g = c.getContext('2d');
let d = 'up';
let dx = 10, dy = 10;
let food = { x: getRandomArbitrary(1, 640), y: getRandomArbitrary(1, 640) };
let s = [{ x: 320, y: 320 }];
const MOVE = {
'up': (pos) => { return { x: pos.x, y: pos.y - dy } },
'down': (pos) => { return { x: pos.x, y: pos.y + dy } },
'left': (pos) => { return { x: pos.x - dx, y: pos.y } },
'right': (pos) => { return { x: pos.x + dx, y: pos.y } }
}
const generateFood = () => {
const { x, y } = drawFood();
food = { x, y };
}
const drawFood = () => {
const pos = { x: Math.floor(getRandomArbitrary(10, 640)), y: Math.floor(getRandomArbitrary(10, 640)) };
new Circle(pos.x, pos.y, 10).draw(g);
return pos;
}
const drawSnake = () => {
s.forEach(r => new Rectangle(r.x, r.y, 10, 10).draw(g));
}
const checkIfLegalMove = (s) => {
return s.every(pos => pos.x > 0 && pos.x < 640 && pos.y > 0 && pos.y < 640);
}
const moveSnake = (timestamp) => {
if(!f) {
return;
}
if(!start) {
start = timestamp;
}
const oldS = s;
s = s.map((p, i) => {
return i === 0 ? MOVE[d](p) : oldS[i - 1];
});
const legal = checkIfLegalMove(s);
const id = legal && f && window.requestAnimationFrame(moveSnake);
new Circle(food.x, food.y, 10).draw(g);
if(!legal) {
drawSnake();
gameOver();
return;
}
if(checkFoodCollision()) {
generateFood();
const endOfSnake = s[s.length - 1];
switch(d) {
case 'up':
s.push({ x: endOfSnake.x, y: endOfSnake.y - dy });
break;
case 'down':
s.push({ x: endOfSnake.x, y: endOfSnake.y + dy });
break;
case 'left':
s.push({ x: endOfSnake.x + dx, y: endOfSnake.y });
break;
case 'right':
s.push({ x: endOfSnake.x - dx, y: endOfSnake.y });
break;
}
point += 50;
}
drawSnake();
}
// TODO : implement Scoreboard?
const intro = () => {
g.font = '30px Arial';
g.fillStyle = 'black';
g.textAlign = 'center';
g.fillText('Play Snake - press Enter to Start', 320, 320);
}
const startGame = () => {
if(f) {
s = [{ x: 320, y: 320 }];
d = 'up';
generateFood();
moveSnake();
}
}
const gameOver = () => {
f = false;
g.font = '30px Arial';
g.fillStyle = 'black';
g.textAlign = 'center';
g.fillText(`Game over! You earned ${point} points.`, 320, 320);
g.fillText(`Press Enter to play again.`, 320, 360);
setTimeout(() => {
if(!f) {
intro();
}
}, 10000);
}
const checkFoodCollision = () => {
return ((food.x - Math.floor( 5 * Math.PI) < s[0].x ) || (food.x + Math.floor( 5 * Math.PI)) > s[0].x) &&
((food.y - Math.floor( 5 * Math.PI) < s[0].y ) || (food.y + Math.floor( 5 * Math.PI)) > s[0].y)
}
window.addEventListener('keydown', (e) => {
const { key } = e;
switch(key) {
case 'Enter':
e.preventDefault();
if(!f) {
f = true;
startGame();
}
break;
case 's':
case 'ArrowDown':
if(d === 'up' && s.length > 1) {
gameOver();
return;
}
d = 'down';
break;
case 'w':
case 'ArrowUp':
if(d === 'down' && s.length > 1) {
gameOver();
return;
}
d = 'up';
break;
case 'a':
case 'ArrowLeft':
if(d === 'right' && s.length > 1) {
gameOver();
return;
}
d = 'left';
break;
case 'd':
case 'ArrowRight':
if(d === 'left' && s.length > 1) {
gameOver();
return;
}
d = 'right';
break;
}
});
intro();