-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.html
163 lines (130 loc) · 2.99 KB
/
snake.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
<!DOCTYPE html>
<html lang="pt-br">
<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</title>
</head>
<body style="background-color: rgb(31, 30, 30);">
<div class="titulo" style="text-align: center; margin-top: 10vh; font-size: 25px;" >
<h1 style="color:rgb(196, 47, 21)">
𝕤𝕟𝕒𝕜𝕖𝕪𝕖𝕒𝕙
</h1>
<form style="margin-top: 5vh;">
<label for="pt" style="color:rgb(208, 226, 43); font-size: 30px;">Pontos:</label>
<input type="text" id="pt" value="0" style="text-align: left; background-color: rgb(31, 30, 30); border: 0; color: rgb(208, 226, 43); font-size: 27px;
width: 50px;">
<label for="hs" style="color:rgb(208, 226, 43); font-size: 30px;">Recorde:</label>
<input type="text" id="hs" value="0" style="text-align: left; background-color: rgb(31, 30, 30); border: 0; color: rgb(208, 226, 43); font-size: 27px;
width: 50px;">
</form>
</div>
<!-- margin-top: 220px; -->
<div class="a" style="text-align: center; margin-top: 10vh;" >
<div class="b" style="">
<canvas id="gc" width="400" height="400"></canvas>
</div>
</div>
</body>
</html>
<script>
window.onload=function() {
canv=document.getElementById("gc");
ctx=canv.getContext("2d");
document.addEventListener("keydown",keyPush);
setInterval(game,1000/15);
}
px=py=10;
gs=tc=20;
ax=ay=15;
xv=yv=0;
trail=[];
tail = 5;
pontos = 0;
record = localStorage.getItem("info");
let pnts = document.getElementById('pt');
let rec = document.getElementById('hs');
if(localStorage.info){
rec.value = localStorage.getItem("info");
}
function game() {
px+=xv;
py+=yv;
if(px<0) {
px= tc-1;
}
if(px>tc-1) {
px= 0;
}
if(py<0) {
py= tc-1;
}
if(py>tc-1) {
py= 0;
}
ctx.fillStyle="yellow";
ctx.fillRect(0,0,canv.width,canv.height);
ctx.fillStyle="black";
for(var i=0;i<trail.length;i++) {
ctx.fillRect(trail[i].x*gs,trail[i].y*gs,gs-2,gs-2);
if(trail[i].x==px && trail[i].y==py) {
tail = 5;
// console.log("------NOVO JOGO------");
// vericando se há recorde novo
if(pontos!=0){
if(pontos>record){
localStorage.info=pontos;
rec.value = localStorage.info;
record = pontos;
}
}
// zerando pontuação atual
pontos=0;
pnts.value = pontos;
}
}
trail.push({x:px,y:py});
while(trail.length>tail) {
trail.shift();
}
if(ax==px && ay==py) {
tail++;
pontos++;
pnts.value = pontos;
// console.log("pontos:", pontos)
ax=Math.floor(Math.random()*tc);
ay=Math.floor(Math.random()*tc);
}
ctx.fillStyle="red";
ctx.fillRect(ax*gs,ay*gs,gs-2,gs-2);
}
function keyPush(evt) {
switch(evt.keyCode) {
case 37:
xv=-1;yv=0;
break;
case 38:
xv=0;yv=-1;
break;
case 39:
xv=1;yv=0;
break;
case 40:
xv=0;yv=1;
break;
case 65:
xv=-1;yv=0;
break;
case 87:
xv=0;yv=-1;
break;
case 68:
xv=1;yv=0;
break;
case 83:
xv=0;yv=1;
break;
}
}
</script>