-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
103 lines (95 loc) · 2.33 KB
/
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
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
const gridContainer = document.querySelector('.grid-container');
const score = document.querySelector('#score');
const resetButton = document.querySelector('#reset-button');
const gridSize = 20;
const cellSize = 20;
const snake = [{x: 10, y: 10}];
let food = getRandomFood();
let scoreValue = 0;
let interval;
function getRandomFood() {
return {
x: Math.floor(Math.random() * gridSize),
y: Math.floor(Math.random() * gridSize)
};
}
function render() {
gridContainer.innerHTML = '';
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
const cell = document.createElement('div');
cell.classList.add('cell');
if (snake.some(segment => segment.x === j && segment.y === i)) {
cell.classList.add('snake');
}
if (food.x === j && food.y === i) {
cell.classList.add('food');
}
gridContainer.appendChild(cell);
}
}
}
function moveSnake() {
const head = { ...snake[0] };
switch (direction) {
case 'Up':
head.y -= 1;
break;
case 'Down':
head.y += 1;
break;
case 'Left':
head.x -= 1;
break;
case 'Right':
head.x += 1;
break;
}
snake.unshift(head);
if (snake[0].x === food.x && snake[0].y === food.y) {
food = getRandomFood();
scoreValue += 10;
score.innerText = scoreValue;
} else {
snake.pop();
}
if (
snake.some((segment, index) =>
index !== 0 && segment.x === snake[0].x && segment.y === snake[0].y
) ||
snake[0].x < 0 || snake[0].x >= gridSize || snake[0].y < 0 || snake[0].y >= gridSize
) {
clearInterval(interval);
alert(`¡Fin del juego! Puntos ganados: ${scoreValue}`);
} else {
render();
}
}
let direction = 'Right';
document.addEventListener('keydown', event => {
switch (event.key) {
case 'ArrowUp':
direction = 'Up';
break;
case 'ArrowDown':
direction = 'Down';
break;
case 'ArrowLeft':
direction = 'Left';
break;
case 'ArrowRight':
direction = 'Right';
break;
}
});
resetButton.addEventListener('click', () => {
clearInterval(interval);
scoreValue = 0;
score.innerText = scoreValue;
snake.splice(1);
snake[0] = {x: 10, y: 10};
food = getRandomFood();
direction = 'Right';
interval = setInterval(moveSnake, 100);
});
interval = setInterval(moveSnake, 100);