-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
47 lines (36 loc) · 914 Bytes
/
script.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
let canvas = document.getElementById("snake");
let context = canvas.getContext("2d");
let box = 32;
let snake = [];
snake[0] = {
x: 8 * box,
y: 8 * box
}
let direction = "right";
function criarBG(){
context.fillStyle = "lightgreen";
context.fillRect(0, 0, 16 * box, 16 * box);
}
function criarCobrinha(){
for(i=0; i < snake.length; i++){
context.fillStyle = "green";
context.fillRect(snake[i].x, snake[i].y, box, box);
}
}
function iniciarJogo() {
criarBG();
criarCobrinha();
let = snakeX = snake[0].x;
let = snakeY = snake[0].y;
if(direction == "right") snakeX += box
if(direction == "left") snakeX -= box
if(direction == "up") snakeY -= box
if(direction == "down") snakeY += box
snake.pop();
let newHead = {
x: snakeX,
y: snakeY
}
snake.unshift(newHead);
}
let jogo = setInterval(iniciarJogo, 100)