-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnakeGame.html
259 lines (217 loc) · 7.06 KB
/
SnakeGame.html
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake Game</title>
</head>
<body class="corpo">
<div style="display: flex; align-items: center; justify-content: center; flex-direction: column">
<span style="font-size: 26px">Snake Game</span>
<span style="font-size: 22px" id="maiorPonto">Maior Ponto: 0</span>
<span style="font-size: 22px" id="ponto">Pontos: 0</span>
</div>
<canvas id="canvas"></canvas>
<div class="commands" style="display: flex; align-items: center; justify-content: center; position: absolute; top: 65%;">
<div style="position: relative">
<img src="./1670503.png" style="width: 250px;">
<button onclick="clickButton(38)" style="padding: 30px; position: absolute; top: 10px; right: 90px; opacity: 0"></button>
<button onclick="clickButton(37)" style="padding: 30px; position: absolute; top: 95px; right: 170px; opacity: 0"></button>
<button onclick="clickButton(40)" style="padding: 30px; position: absolute; top: 95px; right: 90px; opacity: 0"></button>
<button onclick="clickButton(39)" style="padding: 30px; position: absolute; top: 95px; right: 10px; opacity: 0"></button>
</div>
</div>
<style>
@media (pointer: fine){
.corpo{
display: block;
align-items: center;
justify-content: center;
}
#canvas{
position: absolute;
left: 50%;
right: 50%;
transform: translate(-50%, 50%);
border: 1px solid black;
}
.commands{
display: none !important;
}
}
@media (pointer: coarse){
.corpo{
display: flex;
align-items: flex-start;
justify-content: center;
}
#canvas{
position: absolute;
left: 50%;
right: 50%;
transform: translate(-50%, 30%);
border: 1px solid black;
}
}
</style>
</body>
<script>
const board = document.getElementById("canvas");
const context = board.getContext("2d");
document.addEventListener("keydown", changeDirection);
let snake =[
{x: 200, y: 200},
{x: 190, y: 200},
{x: 180, y: 200},
{x: 170, y: 200},
{x: 160, y: 200}
]
let game = 1;
let dx, dy;
dy = 0;
dx = 10;
let foodX, foodY;
let speed = 20;
let pontos = 0;
let maiorPonto = localStorage.getItem('maiorPonto') ? parseInt(localStorage.getItem('maiorPonto')) : 0;
board.width = 350;
board.height = 350;
if(maiorPonto == 0)
localStorage.setItem('maiorPonto', 0);
main();
genFood();
function main(){
if(game){
setTimeout(function onTick(){
clearCanvas();
drawSnake();
moveSnake();
reposition();
drawFruit();
eatFood();
main();
document.getElementById('maiorPonto').innerHTML = 'Maior ponto: ' + maiorPonto.toString();
document.getElementById('ponto').innerHTML = 'Pontos: ' + pontos.toString();
if(pontos > maiorPonto){
console.log(maiorPonto);
maiorPonto = pontos;
localStorage.setItem('maiorPonto', maiorPonto);
}
}, speed);
}
else
lose();
}
function drawSnake(){
snake.forEach(drawSnakePart);
}
function clearCanvas(){
context.fillStyle = 'white';
context.strokestyle = 'black';
context.fillRect(0,0, board.width, board.height);
context.strokeRect(0,0, board.width, board.height);
}
function drawSnakePart(snakePart){
context.fillStyle = 'lightBlue';
context.strokestyle = 'black';
context.fillRect(snakePart.x, snakePart.y, 10, 10);
context.strokeRect(snakePart.x, snakePart.y, 10, 10);
}
function growSnake(){
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
snake.unshift(head);
snake.unshift(head);
}
function genFood(){
foodX = Math.round(Math.random() * ((board.width - 10) + 0) / 10) * 10;
foodY = Math.round(Math.random() * ((board.height - 10) + 0) / 10) * 10;
}
function eatFood(){
snake.forEach(function eatenFood(part){
if(part.x == foodX && part.y == foodY){
genFood();
growSnake();
levelOptimization();
pontos ++;
}
});
}
function drawFruit(){
context.fillStyle = 'lightgreen';
context.strokestyle = 'darkgreen';
context.fillRect(foodX, foodY, 10, 10);
context.strokeRect(foodX, foodY, 10, 10);
}
function moveSnake(){
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
snake.unshift(head);
snake.pop();
}
function levelOptimization(){
speed -= 1;
}
function clickButton(code){
let event = {
keyCode: code
}
changeDirection(event);
}
function changeDirection(event){
const LEFT_KEY = 37;
const UP_KEY = 38;
const RIGHT_KEY = 39;
const DOWN_KEY = 40;
const keyPressed = event.keyCode;
const goingUp = dy === -10;
const goingDown = dy === 10;
const goingRight = dx === 10;
const goingLeft = dx === -10;
if (keyPressed === LEFT_KEY && !goingRight)
{
dx = -10;
dy = 0;
}
if (keyPressed === UP_KEY && !goingDown)
{
dx = 0;
dy = -10;
}
if (keyPressed === RIGHT_KEY && !goingLeft)
{
dx = 10;
dy = 0;
}
if (keyPressed === DOWN_KEY && !goingUp)
{
dx = 0;
dy = 10;
}
}
function reposition(){
if(false){
if(snake[0].x < 0)
snake[0].x = 350;
if(snake[0].x > 350)
snake[0].x = 0;
if(snake[0].y < 0)
snake[0].y = 350;
if(snake[0].y > 350)
snake[0].y = 0;
}
else{
if(snake[0].x < 0 || snake[0].x > 350 || snake[0].y < 0 || snake[0].y > 350)
lose();
}
}
function lose(){
context.fillStyle = 'red';
context.strokestyle = 'black';
context.fillRect(0,0, board.width, board.height);
context.strokeRect(0,0, board.width, board.height);
context.font = "48px serif";
context.fillStyle = "#000000";
context.fillText("Fim de Jogo", (board.width / 2) - 115, (board.height / 2) + 15);
}
</script>
</html>