A standalone HTML5 Tron light cycle game with a pause button, a high score feature, and a main menu.
html
<!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 Game</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div id="mainMenu">
<h1>Tron Light Cycle Game</h1>
<button id="startButton">Start Game</button>
<button id="highScoreButton">High Score</button>
</div>
<div id="pauseMenu" style="display: none;">
<h1>Game Paused</h1>
<button id="resumeButton">Resume Game</button>
<button id="restartButton">Restart Game</button>
</div>
<div id="highScoreMenu" style="display: none;">
<h1>High Score: <span id="highScoreDisplay">0</span></h1>
<button id="backButton">Back to Main Menu</button>
</div>
<script src="script.js"></script>
</body>
</html>
css
body {
margin: 0;
background-color: #f0f0f0;
}
#gameCanvas {
border: 1px solid black;
}
#mainMenu, #pauseMenu, #highScoreMenu {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
button {
margin: 10px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
javascript
// Get canvas and context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Get menu elements
const mainMenu = document.getElementById('mainMenu');
const pauseMenu = document.getElementById('pauseMenu');
const highScoreMenu = document.getElementById('highScoreMenu');
const highScoreDisplay = document.getElementById('highScoreDisplay');
// Get buttons
const startButton = document.getElementById('startButton');
const highScoreButton = document.getElementById('highScoreButton');
const resumeButton = document.getElementById('resumeButton');
const restartButton = document.getElementById('restartButton');
const backButton = document.getElementById('backButton');
// Game variables
let gameRunning = false;
let paused = false;
let highScore = localStorage.getItem('highScore') || 0;
let score = 0;
let player = {
x: canvas.width / 2,
y: canvas.height / 2,
direction: 'right',
trail: []
};
let grid = [];
// Initialize grid
for (let i = 0; i < canvas.height; i++) {
grid[i] = [];
for (let j = 0; j < canvas.width; j++) {
grid[i][j] = 0;
}
}
// Event listeners
startButton.addEventListener('click', startGame);
highScoreButton.addEventListener('click', showHighScore);
resumeButton.addEventListener('click', resumeGame);
restartButton.addEventListener('click', restartGame);
backButton.addEventListener('click', backToMainMenu);
document.addEventListener('keydown', (e) => {
if (gameRunning && !paused) {
switch (e.key) {
case 'ArrowUp':
if (player.direction !== 'down') {
player.direction = 'up';
}
break;
case 'ArrowDown':
if (player.direction !== 'up') {
player.direction = 'down';
}
break;
case 'ArrowLeft':
if (player.direction !== 'right') {
player.direction = 'left';
}
break;
case 'ArrowRight':
if (player.direction !== 'left') {
player.direction = 'right';
}
break;
case 'p':
pauseGame();
break;
}
}
});
// Game loop
function update() {
if (gameRunning && !paused) {
// Move player
switch (player.direction) {
case 'up':
player.y -= 1;
break;
case 'down':
player.y += 1;
break;
case 'left':
player.x -= 1;
break;
case 'right':
player.x += 1;
break;
}
// Check collision
if (player.x < 0 || player.x >= canvas.width || player.y < 0 || player.y >= canvas.height || grid[player.y][player.x] === 1) {
gameOver();
} else {
// Update grid and trail
grid[player.y][player.x] = 1;
player.trail.push({ x: player.x, y: player.y });
// Draw everything
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'black';
for (let i = 0; i < player.trail.length; i++) {
ctx.fillRect(player.trail[i].x, player.trail[i].y, 1, 1);
}
ctx.fillStyle = 'red';
ctx.fillRect(player.x, player.y, 1, 1);
// Update score
score++;
if (score > highScore) {
highScore = score;
localStorage.setItem('highScore', highScore);
}
}
}
requestAnimationFrame(update);
}
// Game functions
function startGame() {
gameRunning = true;
paused = false;
score = 0;
player = {
x: canvas.width / 2,
y: canvas.height / 2,
direction: 'right',
trail: []
};
grid = [];
for (let i = 0; i < canvas.height; i++) {
grid[i] = [];
for (let j = 0; j < canvas.width; j++) {
grid[i][j] = 0;
}
}
mainMenu.style.display = 'none';
update();
}
function pauseGame() {
paused = true;
pauseMenu.style.display = 'block';
}
function resumeGame() {
paused = false;
pauseMenu.style.display = 'none';
}
function restartGame() {
gameRunning = false;
paused = false;
mainMenu.style.display = 'block';
pauseMenu.style.display = 'none';
startGame();
}
function gameOver() {
gameRunning = false;
paused = false;
alert(`Game Over! Your score: ${score}`);
mainMenu.style.display = 'block';
}
function showHighScore() {
highScoreDisplay.textContent = highScore;
mainMenu.style.display = 'none';
highScoreMenu.style.display = 'block';
}
function backToMainMenu() {
highScoreMenu.style.display = 'none';
mainMenu.style.display = 'block';
}
update();