-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
88 lines (71 loc) · 2.79 KB
/
script.js
File metadata and controls
88 lines (71 loc) · 2.79 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
let canvas = document.getElementById("snake");
let context = canvas.getContext("2d"); /* Renderiza o desenho dentro do nosso canvas em 2d no HTML */
let box = 32;
let score = 0;
let snake = [];
snake[0] = {
x: 8 * box,
y: 8 * box
}
let direction = "right";
let food = {
x: Math.floor(Math.random() * 15 + 1) * box,
y: Math.floor(Math.random() * 15 + 1) * box /* Números aleatórios distribuidos dentro do canvas*/
}
function criarBG() {
context.fillStyle = "Lightblue"; /* Define a cor do canvas*/
context.fillRect(0, 0, 16 * box, 16 * box); /* Desenha o retangulo onde o jogo vai ser aberto*/
}
function criarCobrinha() {
for( i = 0; i < snake.length; i++) {
context.fillStyle = "green";
context.fillRect(snake[i].x, snake[i].y, box, box);
}
}
function drawFood() {
context.fillStyle = "orange";
context.fillRect(food.x, food.y, box, box);
}
document.addEventListener("keydown", update);
function update (event) {
if(event.keyCode == 37 && direction !="right") direction = "left"; /* Evita que a cobrinha mude para a direção oposta diretamente*/
if(event.keyCode == 38 && direction !="down") direction = "up";
if(event.keyCode == 39 && direction !="left") direction = "right";
if(event.keyCode == 40 && direction !="up") direction = "down";
}
function iniciarJogo() {
for ( i = 1; i < snake.length; i++){
if(snake[0].x == snake[i].x && snake[0].y == snake[i].y){
clearInterval(jogo);
alert("If it was a snake, it would have bit you. Game over :( ")
}
}
if(snake[0].x > 15 * box && direction == "right") snake[0].x = 0 * box; /* Faz a cobrinha aparecer do outro lado quando cruza a borda do canvas*/
if(snake[0].x < 0 * box && direction == "left") snake[0].x = 16 * box;
if(snake[0].y > 15 * box && direction == "down") snake[0].y = 0 * box;
if(snake[0].y < 0 * box && direction == "up") snake[0].y = 16 * box;
criarBG();
criarCobrinha();
drawFood();
let snakeX = snake[0].x;
let snakeY = snake[0].y;
if(direction == "right") snakeX += box;
if(direction == "left") snakeX -= box;
if(direction == "down") snakeY += box;
if(direction == "up") snakeY -= box;
if(snakeX != food.x || snakeY != food.y) {
snake.pop();
}else {food.x = Math.floor(Math.random() * 15 + 1) * box;
food.y = Math.floor(Math.random() * 15 + 1) * box;
score++;
}
let newHead = {
x: snakeX,
y: snakeY
}
snake.unshift(newHead);
context.fillStyle = "white";
context.font = "50px Changa one";
context.fillText(score,2*box, 1.5*box);
}
let jogo = setInterval(iniciarJogo, 100);