-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
152 lines (129 loc) · 4.02 KB
/
script.js
File metadata and controls
152 lines (129 loc) · 4.02 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const startBtn = document.getElementById('startBtn');
// Controles mobile
const upBtn = document.getElementById('upBtn');
const downBtn = document.getElementById('downBtn');
const leftBtn = document.getElementById('leftBtn');
const rightBtn = document.getElementById('rightBtn');
// Configurações
const gridSize = 20;
let snake = [{x: 200, y: 200}];
let food = generateFood();
let dx = gridSize;
let dy = 0;
let score = 0;
let gameInterval;
let isGameRunning = false;
// Cores
const colors = {
snake: '#ff4d4d',
snakeHead: '#ff9999',
food: '#4dff4d',
bg: 'rgba(15, 15, 15, 0.2)'
};
// Inicialização
function init() {
drawGame();
setupControls();
}
// Gerar comida
function generateFood() {
return {
x: Math.floor(Math.random() * (canvas.width / gridSize)) * gridSize,
y: Math.floor(Math.random() * (canvas.height / gridSize)) * gridSize
};
}
// Desenhar elementos
function drawGame() {
// Fundo
ctx.fillStyle = colors.bg;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Comida
ctx.fillStyle = colors.food;
ctx.beginPath();
ctx.arc(food.x + gridSize/2, food.y + gridSize/2, gridSize/2, 0, Math.PI*2);
ctx.fill();
// Cobra
snake.forEach((segment, index) => {
ctx.fillStyle = index === 0 ? colors.snakeHead : colors.snake;
ctx.fillRect(segment.x, segment.y, gridSize, gridSize);
// Detalhes
ctx.strokeStyle = 'rgba(0, 0, 0, 0.1)';
ctx.strokeRect(segment.x, segment.y, gridSize, gridSize);
});
}
// Atualizar jogo
function updateGame() {
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
// Colisões
if (
head.x < 0 || head.x >= canvas.width ||
head.y < 0 || head.y >= canvas.height ||
snake.some(segment => segment.x === head.x && segment.y === head.y)
) {
endGame();
return;
}
snake.unshift(head);
if (head.x === food.x && head.y === food.y) {
score += 10;
scoreElement.textContent = score.toString().padStart(2, '0');
food = generateFood();
} else {
snake.pop();
}
drawGame();
}
// Controles
function setupControls() {
// Teclado
document.addEventListener('keydown', e => {
if (!isGameRunning) return;
switch(e.key) {
case 'ArrowUp': if (dy !== gridSize) { dx = 0; dy = -gridSize; } break;
case 'ArrowDown': if (dy !== -gridSize) { dx = 0; dy = gridSize; } break;
case 'ArrowLeft': if (dx !== gridSize) { dx = -gridSize; dy = 0; } break;
case 'ArrowRight': if (dx !== -gridSize) { dx = gridSize; dy = 0; } break;
}
});
// Mobile
upBtn.addEventListener('click', () => { if (dy !== gridSize) { dx = 0; dy = -gridSize; } });
downBtn.addEventListener('click', () => { if (dy !== -gridSize) { dx = 0; dy = gridSize; } });
leftBtn.addEventListener('click', () => { if (dx !== gridSize) { dx = -gridSize; dy = 0; } });
rightBtn.addEventListener('click', () => { if (dx !== -gridSize) { dx = gridSize; dy = 0; } });
// Botão Start
startBtn.addEventListener('click', () => {
if (!isGameRunning) {
isGameRunning = true;
gameInterval = setInterval(updateGame, 100);
startBtn.disabled = true;
}
});
}
// Finalizar jogo
function endGame() {
clearInterval(gameInterval);
isGameRunning = false;
startBtn.disabled = false;
// Efeito visual
ctx.fillStyle = 'rgba(255, 77, 77, 0.5)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
setTimeout(() => {
alert(`Game Over\nScore: ${score}`);
resetGame();
}, 100);
}
// Reiniciar jogo
function resetGame() {
snake = [{x: 200, y: 200}];
dx = gridSize;
dy = 0;
score = 0;
scoreElement.textContent = '00';
food = generateFood();
drawGame();
}
// Iniciar
init();