Repository navigation
Expand file tree
/
Copy pathscripts.js
More file actions
94 lines (80 loc) · 2.09 KB
/
Copy pathscripts.js
File metadata and controls
94 lines (80 loc) · 2.09 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
89
90
91
92
93
94
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const gridSize = 20;
const tileSize = canvas.width / gridSize;
let snake = [{ x: 10, y: 10 }];
let apple = { x: 15, y: 15 };
let velocity = { x: 0, y: 0 };
document.addEventListener("keydown", (e) => {
switch (e.key) {
case "ArrowUp":
if (velocity.y === 0) {
velocity.x = 0;
velocity.y = -1;
}
break;
case "ArrowDown":
if (velocity.y === 0) {
velocity.x = 0;
velocity.y = 1;
}
break;
case "ArrowLeft":
if (velocity.x === 0) {
velocity.x = -1;
velocity.y = 0;
}
break;
case "ArrowRight":
if (velocity.x === 0) {
velocity.x = 1;
velocity.y = 0;
}
break;
}
});
function gameLoop() {
update();
draw();
setTimeout(gameLoop, 1000 / 15);
}
function update() {
const head = { x: snake[0].x + velocity.x, y: snake[0].y + velocity.y };
snake.unshift(head);
if (head.x === apple.x && head.y === apple.y) {
placeApple();
} else {
snake.pop();
}
if (head.x < 0 || head.x >= gridSize || head.y < 0 || head.y >= gridSize) {
snake = [{ x: 10, y: 10 }];
velocity = { x: 0, y: 0 };
}
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
snake = [{ x: 10, y: 10 }];
velocity = { x: 0, y: 0 };
}
}
}
function draw() {
ctx.fillStyle = "#f0f0f0";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "green";
for (const part of snake) {
ctx.fillRect(part.x * tileSize, part.y * tileSize, tileSize, tileSize);
}
ctx.fillStyle = "red";
ctx.fillRect(apple.x * tileSize, apple.y * tileSize, tileSize, tileSize);
}
function placeApple() {
apple.x = Math.floor(Math.random() * gridSize);
apple.y = Math.floor(Math.random() * gridSize);
for (const part of snake) {
if (part.x === apple.x && part.y === apple.y) {
placeApple();
break;
}
}
}
gameLoop();