Robot Runner Game.html
Robot Runner Game.html
DOCTYPE html>
<html>
<head>
<title>Robot Runner</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-
scale=1.0, user-scalable=no">
<style>
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
height: 100vh;
background-color: #1a1a1a;
touch-action: none;
overflow: hidden;
font-family: 'Arial', sans-serif;
}
canvas {
border: 2px solid #00ff00;
background: #000;
max-width: 100%;
margin-top: 10px;
}
#controls {
position: fixed;
bottom: 20px;
width: 100%;
display: flex;
justify-content: space-between;
padding: 0 20px;
box-sizing: border-box;
}
.control-btn {
width: 60px;
height: 60px;
background-color: rgba(0, 255, 0, 0.2);
border: 2px solid #00ff00;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
user-select: none;
touch-action: none;
color: #00ff00;
}
#movement-buttons {
display: flex;
gap: 20px;
}
#hud {
position: fixed;
top: 10px;
left: 10px;
color: #00ff00;
font-family: 'Courier New', monospace;
z-index: 100;
background-color: rgba(0, 0, 0, 0.5);
padding: 5px;
}
#gameOver {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.9);
padding: 20px;
border: 2px solid #ff0000;
text-align: center;
color: #ff0000;
z-index: 1000;
}
#restartBtn {
margin-top: 20px;
padding: 10px 20px;
background-color: #ff0000;
border: none;
color: white;
cursor: pointer;
font-family: 'Courier New', monospace;
}
</style>
</head>
<body>
<div id="hud">Nivel: <span id="level">1</span> | Energía: <span
id="energy">100</span></div>
<canvas id="gameCanvas" width="800" height="400"></canvas>
<div id="controls">
<div id="movement-buttons">
<div class="control-btn" id="leftBtn">←</div>
<div class="control-btn" id="rightBtn">→</div>
</div>
<div class="control-btn" id="jumpBtn">↑</div>
</div>
<div id="gameOver">
<h2>GAME OVER</h2>
<p>¡Has perdido toda tu energía!</p>
<button id="restartBtn">Volver a jugar</button>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const gameOverScreen = document.getElementById('gameOver');
const restartBtn = document.getElementById('restartBtn');
let currentLevel = 1;
let energy = 100;
let gameIsOver = false;
const player = {
x: 50,
y: 260,
width: 30,
height: 40,
speed: 5,
jumpForce: 12,
gravity: 0.5,
velocityY: 0,
velocityX: 0,
isJumping: false
};
const keys = {
left: false,
right: false,
up: false
};
function resizeCanvas() {
const maxWidth = Math.min(800, window.innerWidth - 20);
const ratio = canvas.height / canvas.width;
canvas.style.width = maxWidth + 'px';
canvas.style.height = (maxWidth * ratio) + 'px';
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// Controles táctiles
const leftBtn = document.getElementById('leftBtn');
const rightBtn = document.getElementById('rightBtn');
const jumpBtn = document.getElementById('jumpBtn');
handleTouch(leftBtn, 'left');
handleTouch(rightBtn, 'right');
handleTouch(jumpBtn, 'up');
// Controles de teclado
document.addEventListener('keydown', (e) => {
if (!gameIsOver) {
switch(e.key) {
case 'ArrowLeft': keys.left = true; break;
case 'ArrowRight': keys.right = true; break;
case 'ArrowUp':
case ' ':
if (!player.isJumping) {
player.velocityY = -player.jumpForce;
player.isJumping = true;
}
break;
}
}
});
function getCurrentLevel() {
return levels[currentLevel - 1];
}
function resetPlayer() {
player.x = 50;
player.y = 260;
player.velocityY = 0;
player.velocityX = 0;
player.isJumping = false;
}
function showGameOver() {
gameIsOver = true;
gameOverScreen.style.display = 'block';
}
function restartGame() {
gameIsOver = false;
gameOverScreen.style.display = 'none';
currentLevel = 1;
energy = 100;
document.getElementById('level').textContent = currentLevel;
document.getElementById('energy').textContent = energy;
resetPlayer();
}
restartBtn.addEventListener('click', restartGame);
function update() {
if (gameIsOver) return;
// Movimiento horizontal
if (keys.left) {
player.velocityX = -player.speed;
} else if (keys.right) {
player.velocityX = player.speed;
} else {
player.velocityX = 0;
}
// Actualizar posición
player.x += player.velocityX;
player.velocityY += player.gravity;
player.y += player.velocityY;
// Comprobar meta
if (checkCollision(player, level.goal)) {
currentLevel++;
document.getElementById('level').textContent = currentLevel;
if (currentLevel > levels.length) {
alert('¡Felicidades! Has completado todos los niveles');
currentLevel = 1;
document.getElementById('level').textContent = currentLevel;
}
resetPlayer();
}
// Caída al vacío
if (player.y > canvas.height) {
resetPlayer();
energy -= 20;
document.getElementById('energy').textContent = energy;
if (energy <= 0) {
showGameOver();
}
}
}
// Ojos
ctx.fillStyle = '#ffffff';
ctx.fillRect(x + width/4, y + height/4, width/6, height/6);
ctx.fillRect(x + width*5/8, y + height/4, width/6, height/6);
// Antena
ctx.strokeStyle = '#00ff00';
ctx.beginPath();
ctx.moveTo(x + width/2, y);
ctx.lineTo(x + width/2, y - height/4);
ctx.stroke();
}
function draw() {
const level = getCurrentLevel();
// Limpiar canvas
ctx.fillStyle = '#000000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Dibujar plataformas
level.platforms.forEach(platform => {
ctx.fillStyle = '#00aa00';
ctx.fillRect(platform.x, platform.y, platform.width,
platform.height);
});
// Dibujar peligros
level.hazards.forEach(hazard => {
ctx.fillStyle = '#ff0000';
ctx.fillRect(hazard.x, hazard.y, hazard.width, hazard.height);
});
// Dibujar meta
ctx.fillStyle = '#00ffff';
ctx.fillRect(level.goal.x, level.goal.y, level.goal.width,
level.goal.height);
// Dibujar jugador
drawRobot(player.x, player.y, player.width, player.height);
}
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
gameLoop();
</script>
</body>
</html>