-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjuego_snake.html
More file actions
239 lines (206 loc) · 6.08 KB
/
Copy pathjuego_snake.html
File metadata and controls
239 lines (206 loc) · 6.08 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake Arcade Iframe-Ready</title>
<style>
:root {
--neon: #00ff41;
--bg: #050505;
}
* { box-sizing: border-box; }
body {
background: var(--bg);
color: white;
font-family: 'Courier New', Courier, monospace;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
overflow: hidden;
}
#game-container {
position: relative;
width: 400px;
border: 3px solid #222;
background: #000;
}
#ui {
display: flex;
justify-content: space-around;
background: #111;
padding: 10px;
border-bottom: 2px solid #222;
}
.stat { text-align: center; }
.label { font-size: 9px; color: #666; text-transform: uppercase; }
.val { font-size: 16px; color: var(--neon); font-weight: bold; }
canvas { display: block; outline: none; }
.overlay {
position: absolute;
top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0,0,0,0.95);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 100;
}
.hidden { display: none !important; }
input {
background: #000;
border: 1px solid var(--neon);
color: var(--neon);
padding: 8px;
font-size: 16px;
text-align: center;
width: 80px;
margin-bottom: 15px;
}
button {
background: var(--neon);
color: #000;
border: none;
padding: 12px 24px;
font-size: 14px;
font-weight: bold;
cursor: pointer;
border-radius: 3px;
}
button:active { transform: scale(0.98); }
</style>
</head>
<body>
<div id="game-container">
<div id="ui">
<div class="stat"><div class="label">Puntos</div><div id="score-val" class="val">0</div></div>
<div class="stat"><div class="label">Jugador</div><div id="name-val" class="val">---</div></div>
<div class="stat"><div class="label">Vidas</div><div id="lives-val" class="val" style="color:#ff3131">❤❤❤</div></div>
</div>
<canvas id="c" width="400" height="400" tabindex="1"></canvas>
<div id="overlay-start" class="overlay">
<h2 style="color:var(--neon); margin-bottom:10px;">SNAKE ARCADE</h2>
<input type="text" id="p-name" maxlength="3" value="PLY">
<button id="start-trigger">JUGAR AHORA</button>
</div>
<div id="overlay-death" class="overlay hidden">
<h2 style="color:#ff3131">GAME OVER</h2>
<div id="final-stats" style="margin-bottom:15px"></div>
<button id="retry-trigger">REINTENTAR</button>
</div>
</div>
<script>
/**
* LÓGICA COMPATIBLE CON IFRAME
*/
const canvas = document.getElementById('c');
const ctx = canvas.getContext('2d');
const GS = 20, TILE = 20;
let snake, food, dir, nextDir, score, lives, loop, active = false;
const animals = ['🐭', '🐹', '🐰', '🐿️'];
// Elementos
const overStart = document.getElementById('overlay-start');
const overDeath = document.getElementById('overlay-death');
function setup() {
const name = document.getElementById('p-name').value.toUpperCase();
document.getElementById('name-val').textContent = name;
score = 0;
lives = 3;
overStart.classList.add('hidden');
overDeath.classList.add('hidden');
// CRITICO PARA IFRAMES: Forzar el foco al canvas
canvas.focus();
resetSnake();
if(loop) clearInterval(loop);
loop = setInterval(draw, 100);
active = true;
}
function resetSnake() {
snake = [{x:10, y:10}, {x:9, y:10}, {x:8, y:10}];
dir = {x:1, y:0};
nextDir = {x:1, y:0};
spawnFood();
}
function spawnFood() {
food = {
x: Math.floor(Math.random() * TILE),
y: Math.floor(Math.random() * TILE),
icon: animals[Math.floor(Math.random() * animals.length)]
};
}
function draw() {
if(!active) return;
dir = nextDir;
const head = { x: snake[0].x + dir.x, y: snake[0].y + dir.y };
// Paredes
if (head.x < 0) head.x = TILE-1;
else if (head.x >= TILE) head.x = 0;
if (head.y < 0) head.y = TILE-1;
else if (head.y >= TILE) head.y = 0;
// Choque cuerpo
if (snake.some(s => s.x === head.x && s.y === head.y)) {
handleHit();
return;
}
snake.unshift(head);
if (head.x === food.x && head.y === food.y) {
score += 10;
document.getElementById('score-val').textContent = score;
spawnFood();
} else {
snake.pop();
}
// Renderizado
ctx.fillStyle = '#000';
ctx.fillRect(0,0,400,400);
// Comida
ctx.font = "16px serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(food.icon, food.x*GS + GS/2, food.y*GS + GS/2);
// Serpiente
snake.forEach((s, i) => {
ctx.fillStyle = i === 0 ? '#00ff41' : '#007a1f';
ctx.beginPath();
ctx.roundRect(s.x*GS+1, s.y*GS+1, GS-2, GS-2, 4);
ctx.fill();
});
}
function handleHit() {
lives--;
document.getElementById('lives-val').textContent = "❤".repeat(lives).padEnd(3, "🖤");
active = false;
if(lives <= 0) {
document.getElementById('final-stats').innerHTML = `Score: ${score}`;
overDeath.classList.remove('hidden');
} else {
ctx.fillStyle = 'rgba(255,0,0,0.4)';
ctx.fillRect(0,0,400,400);
setTimeout(() => {
resetSnake();
active = true;
}, 800);
}
}
// Eventos de teclado con prevención de scroll
window.addEventListener('keydown', e => {
const k = e.key;
if (k === 'ArrowUp' && dir.y === 0) nextDir = {x:0, y:-1};
if (k === 'ArrowDown' && dir.y === 0) nextDir = {x:0, y:1};
if (k === 'ArrowLeft' && dir.x === 0) nextDir = {x:-1, y:0};
if (k === 'ArrowRight' && dir.x === 0) nextDir = {x:1, y:0};
if(["ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].includes(k)) {
e.preventDefault();
}
});
// Vinculación de botones
document.getElementById('start-trigger').onclick = setup;
document.getElementById('retry-trigger').onclick = setup;
// Asegurar que el canvas pueda recibir foco
canvas.addEventListener('click', () => canvas.focus());
</script>
</body>
</html>