<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tron Light Cycle</title>
<style>
body {
margin: 0;
padding: 0;
font-family: 'Arial', sans-serif;
background-color: #121212;
color: #00FFFF;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
overflow: hidden;
}
#game-container {
position: relative;
width: 800px;
height: 600px;
border: 2px solid #00FFFF;
box-shadow: 0 0 20px #00FFFF;
}
canvas {
background-color: #000;
display: block;
}
#main-menu, #game-over {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 10;
}
h1 {
font-size: 48px;
text-shadow: 0 0 10px #00FFFF;
margin-bottom: 30px;
}
.menu-button {
background-color: transparent;
color: #00FFFF;
border: 2px solid #00FFFF;
padding: 10px 20px;
margin: 10px;
font-size: 18px;
cursor: pointer;
transition: all 0.3s;
width: 200px;
text-align: center;
}
.menu-button:hover {
background-color: #00FFFF;
color: #000;
box-shadow: 0 0 15px #00FFFF;
}
#game-over {
display: none;
}
#high-score {
margin-top: 20px;
font-size: 24px;
text-align: center;
}
#hud {
position: absolute;
top: 10px;
right: 10px;
font-size: 18px;
z-index: 5;
}
#pause-btn {
position: absolute;
top: 10px;
left: 10px;
z-index: 5;
background-color: transparent;
color: #00FFFF;
border: 1px solid #00FFFF;
padding: 5px 10px;
cursor: pointer;
}
#pause-btn:hover {
background-color: rgba(0, 255, 255, 0.2);
}
.hidden {
display: none !important;
}
</style>
</head>
<body>
<div id="game-container">
<canvas id="game-canvas" width="800" height="600"></canvas>
<div id="main-menu">
<h1>TRON LIGHT CYCLE</h1>
<button class="menu-button" id="start-btn">START GAME</button>
<div id="high-score">HIGH SCORE: 0</div>
</div>
<div id="game-over" class="hidden">
<h1>GAME OVER</h1>
<div id="final-score">YOUR SCORE: 0</div>
<div id="new-high-score" class="hidden">NEW HIGH SCORE!</div>
<button class="menu-button" id="restart-btn">PLAY AGAIN</button>
<button class="menu-button" id="menu-btn">MAIN MENU</button>
</div>
<button id="pause-btn" class="hidden">PAUSE</button>
<div id="hud" class="hidden">
<div id="score">SCORE: 0</div>
</div>
</div>
<script>
// Game variables
const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');
const gridSize = 10;
const width = canvas.width;
const height = canvas.height;
// Game states
const STATES = {
MENU: 0,
PLAYING: 1,
PAUSED: 2,
GAME_OVER: 3
};
// Game objects
let player;
let enemies = [];
let gameState;
let score;
let highScore;
let animationFrameId;
let lastTime;
// DOM elements
const mainMenu = document.getElementById('main-menu');
const gameOver = document.getElementById('game-over');
const startBtn = document.getElementById('start-btn');
const restartBtn = document.getElementById('restart-btn');
const menuBtn = document.getElementById('menu-btn');
const pauseBtn = document.getElementById('pause-btn');
const highScoreDisplay = document.getElementById('high-score');
const finalScoreDisplay = document.getElementById('final-score');
const newHighScoreDisplay = document.getElementById('new-high-score');
const scoreDisplay = document.getElementById('score');
const hud = document.getElementById('hud');
// Event listeners
startBtn.addEventListener('click', startGame);
restartBtn.addEventListener('click', startGame);
menuBtn.addEventListener('click', showMainMenu);
pauseBtn.addEventListener('click', togglePause);
document.addEventListener('keydown', handleKeyDown);
// Initialize the game
initGame();
function initGame() {
// Load high score from localStorage
highScore = parseInt(localStorage.getItem('tronHighScore')) || 0;
highScoreDisplay.textContent = `HIGH SCORE: ${highScore}`;
gameState = STATES.MENU;
}
function startGame() {
// Reset game variables
score = 0;
scoreDisplay.textContent = `SCORE: ${score}`;
// Create player
player = {
x: Math.floor(width / 2 / gridSize) * gridSize,
y: Math.floor(height / 2 / gridSize) * gridSize,
dx: gridSize,
dy: 0,
trail: [],
trailLength: 5,
color: '#00FFFF'
};
// Create enemies
enemies = [
createEnemy('#FF0000', width / 4, height / 4, 0, gridSize),
createEnemy('#00FF00', width * 3/4, height / 4, 0, gridSize),
createEnemy('#FFFF00', width * 3/4, height * 3/4, -gridSize, 0)
];
// Hide menus, show game elements
mainMenu.classList.add('hidden');
gameOver.classList.add('hidden');
pauseBtn.classList.remove('hidden');
hud.classList.remove('hidden');
newHighScoreDisplay.classList.add('hidden');
// Set game state
gameState = STATES.PLAYING;
// Start game loop
lastTime = performance.now();
gameLoop();
}
function createEnemy(color, x, y, dx, dy) {
return {
x: Math.floor(x / gridSize) * gridSize,
y: Math.floor(y / gridSize) * gridSize,
dx: dx,
dy: dy,
trail: [],
trailLength: 5,
color: color,
updateDirection: function() {
// Simple AI for enemy movement
// Each enemy has 10% chance to change direction every frame
if (Math.random() < 0.1) {
const directions = [
{ dx: gridSize, dy: 0 },
{ dx: -gridSize, dy: 0 },
{ dx: 0, dy: gridSize },
{ dx: 0, dy: -gridSize }
];
// Filter out invalid directions (opposite of current direction)
const validDirections = directions.filter(dir => {
return !(dir.dx + this.dx === 0 && dir.dy + this.dy === 0);
});
// Choose a random valid direction
const newDir = validDirections[Math.floor(Math.random() * validDirections.length)];
this.dx = newDir.dx;
this.dy = newDir.dy;
}
}
};
}
function handleKeyDown(e) {
// If in main menu, start game on any key press
if (gameState === STATES.MENU && e.key === 'Enter') {
startGame();
return;
}
// Pause game on ESC
if (e.key === 'Escape') {
togglePause();
return;
}
// If game is not in playing state, ignore other inputs
if (gameState !== STATES.PLAYING) return;
// Player controls
switch(e.key) {
case 'ArrowUp':
if (player.dy === 0) {
player.dx = 0;
player.dy = -gridSize;
}
break;
case 'ArrowDown':
if (player.dy === 0) {
player.dx = 0;
player.dy = gridSize;
}
break;
case 'ArrowLeft':
if (player.dx === 0) {
player.dx = -gridSize;
player.dy = 0;
}
break;
case 'ArrowRight':
if (player.dx === 0) {
player.dx = gridSize;
player.dy = 0;
}
break;
}
}
function togglePause() {
if (gameState === STATES.PLAYING) {
gameState = STATES.PAUSED;
pauseBtn.textContent = 'RESUME';
cancelAnimationFrame(animationFrameId);
} else if (gameState === STATES.PAUSED) {
gameState = STATES.PLAYING;
pauseBtn.textContent = 'PAUSE';
lastTime = performance.now();
gameLoop();
}
}
function showMainMenu() {
gameState = STATES.MENU;
cancelAnimationFrame(animationFrameId);
mainMenu.classList.remove('hidden');
gameOver.classList.add('hidden');
pauseBtn.classList.add('hidden');
hud.classList.add('hidden');
}
function gameOver() {
gameState = STATES.GAME_OVER;
// Check for new high score
if (score > highScore) {
highScore = score;
localStorage.setItem('tronHighScore', highScore);
highScoreDisplay.textContent = `HIGH SCORE: ${highScore}`;
newHighScoreDisplay.classList.remove('hidden');
}
finalScoreDisplay.textContent = `YOUR SCORE: ${score}`;
// Show game over screen
gameOver.classList.remove('hidden');
pauseBtn.classList.add('hidden');
}
function update() {
// Update player position
player.trail.push({x: player.x, y: player.y});
while (player.trail.length > player.trailLength) {
player.trail.shift();
}
player.x += player.dx;
player.y += player.dy;
// Check for wall collision
if (player.x < 0 || player.x >= width || player.y < 0 || player.y >= height) {
gameOver();
return;
}
// Check for collision with player's own trail
for (let i = 0; i < player.trail.length - 1; i++) {
if (player.x === player.trail[i].x && player.y === player.trail[i].y) {
gameOver();
return;
}
}
// Update enemies
for (let enemy of enemies) {
enemy.updateDirection();
enemy.trail.push({x: enemy.x, y: enemy.y});
while (enemy.trail.length > enemy.trailLength) {
enemy.trail.shift();
}
enemy.x += enemy.dx;
enemy.y += enemy.dy;
// Check for wall collision for enemy
if (enemy.x < 0 || enemy.x >= width || enemy.y < 0 || enemy.y >= height) {
// Respawn enemy at a random position
enemy.x = Math.floor(Math.random() * (width / gridSize)) * gridSize;
enemy.y = Math.floor(Math.random() * (height / gridSize)) * gridSize;
enemy.trail = [];
// Increase score when enemy crashes
score += 10;
scoreDisplay.textContent = `SCORE: ${score}`;
// Increase player's trail length as a reward
player.trailLength += 2;
continue;
}
// Check for collision with player
if (player.x === enemy.x && player.y === enemy.y) {
gameOver();
return;
}
// Check for collision with player's trail
for (let i = 0; i < player.trail.length; i++) {
if (enemy.x === player.trail[i].x && enemy.y === player.trail[i].y) {
// Respawn enemy
enemy.x = Math.floor(Math.random() * (width / gridSize)) * gridSize;
enemy.y = Math.floor(Math.random() * (height / gridSize)) * gridSize;
enemy.trail = [];
// Increase score
score += 15;
scoreDisplay.textContent = `SCORE: ${score}`;
// Increase player's trail length
player.trailLength += 3;
break;
}
}
// Check for collision with enemy's own trail
for (let i = 0; i < enemy.trail.length - 1; i++) {
if (enemy.x === enemy.trail[i].x && enemy.y === enemy.trail[i].y) {
// Respawn enemy
enemy.x = Math.floor(Math.random() * (width / gridSize)) * gridSize;
enemy.y = Math.floor(Math.random() * (height / gridSize)) * gridSize;
enemy.trail = [];
// Increase score
score += 5;
scoreDisplay.textContent = `SCORE: ${score}`;
break;
}
}
// Check for collision with other enemies' trails
for (let otherEnemy of enemies) {
if (enemy === otherEnemy) continue;
for (let i = 0; i < otherEnemy.trail.length; i++) {
if (enemy.x === otherEnemy.trail[i].x && enemy.y === otherEnemy.trail[i].y) {
// Respawn enemy
enemy.x = Math.floor(Math.random() * (width / gridSize)) * gridSize;
enemy.y = Math.floor(Math.random() * (height / gridSize)) * gridSize;
enemy.trail = [];
break;
}
}
}
}
// Check for collision with enemy trails
for (let enemy of enemies) {
for (let i = 0; i < enemy.trail.length; i++) {
if (player.x === enemy.trail[i].x && player.y === enemy.trail[i].y) {
gameOver();
return;
}
}
}
// Increase score
score += 1;
scoreDisplay.textContent = `SCORE: ${score}`;
// Increase trail length every 100 points
if (score % 100 === 0) {
player.trailLength += 1;
}
}
function render() {
// Clear canvas
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, width, height);
// Draw grid (optional)
ctx.strokeStyle = '#333';
ctx.lineWidth = 0.5;
for (let x = 0; x < width; x += gridSize) {
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, height);
ctx.stroke();
}
for (let y = 0; y < height; y += gridSize) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(width, y);
ctx.stroke();
}
// Draw player trail
ctx.fillStyle = player.color;
for (let i = 0; i < player.trail.length; i++) {
ctx.fillRect(player.trail[i].x, player.trail[i].y, gridSize - 1, gridSize - 1);
}
// Draw player
ctx.fillStyle = '#FFFFFF';
ctx.fillRect(player.x, player.y, gridSize - 1, gridSize - 1);
// Draw enemies and their trails
for (let enemy of enemies) {
// Draw enemy trail
ctx.fillStyle = enemy.color;
for (let i = 0; i < enemy.trail.length; i++) {
ctx.fillRect(enemy.trail[i].x, enemy.trail[i].y, gridSize - 1, gridSize - 1);
}
// Draw enemy
ctx.fillStyle = '#FFFFFF';
ctx.fillRect(enemy.x, enemy.y, gridSize - 1, gridSize - 1);
}
// Draw pause screen
if (gameState === STATES.PAUSED) {
ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = '#00FFFF';
ctx.font = '48px Arial';
ctx.textAlign = 'center';
ctx.fillText('PAUSED', width / 2, height / 2);
}
}
function gameLoop(timestamp) {
// Calculate delta time
const deltaTime = timestamp - lastTime;
lastTime = timestamp;
// Update game state
update();
// Render game
render();
// Continue game loop if still playing
if (gameState === STATES.PLAYING) {
animationFrameId = requestAnimationFrame(gameLoop);
}
}
</script>
</body>
</html>
Would you like me to explain or break down the code?