-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
103 lines (88 loc) · 2.84 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Jogo da Cobrinha 🐍</title>
</head>
<body>
<canvas id="stage" width="400" height="400"></canvas>
<script type="text/javascript">
window.onload = function(){
var stage = document.getElementById('stage');
var ctx = stage.getContext('2d'); //contexto
document.addEventListener("keydown", keyPush);
setInterval(game, 60);
const vel = 1;
var vx = vy = 0;
var px = 10;
var py = 15;
var tp = 20;
var qp = 20;
var ax = ay = 15;
var trail = [];
tail = 5;
function game(){
px += vx;
py += vy;
if(px < 0){
px = qp - 1;
}
if(px > qp - 1){
px = 0;
}
if(py < 0){
py = qp - 1;
}
if(py > qp - 1){
py = 0;
}
ctx.fillStyle = "black";
ctx.fillRect(0, 0, stage.width, stage.height);
ctx.fillStyle = "red";
ctx.fillRect(ax*tp, ay*tp, tp, tp);
ctx.fillStyle = "gray";
for(let i = 0; i < trail.length; i++){
ctx.fillRect(trail[i].x*tp, trail[i].y*tp, tp-1, tp-1);
if(trail[i].x == px && trail[i].y == py){
vx = vy = 0;
tail = 5;
}
}
trail.push({x:px, y:py});
while(trail.length > tail){
trail.shift();
}
if(ax == px && ay == py){
tail++;
ax = Math.floor(Math.random() * qp);
ay = Math.floor(Math.random() * qp);
}
}
function keyPush(event){
switch(event.keyCode){
case 37: //left
vx = -vel;
vy = 0;
break;
case 38: //up
vx = 0;
vy = -vel;
break;
case 39: //right
vx = vel;
vy = 0;
break;
case 40: //down
vx = 0;
vy = vel;
break;
default:
break;
}
}
}
</script>
</body>
</html>