Write a single page website that let's me play a racing game
- User
Write a single page website that let's me play a racing game
Assistant
Here's a simple racing game you can play in your browser. It uses HTML5 canvas for rendering and JavaScript for game logic:
<!DOCTYPE html> <html> <head> <title>Racing Game</title> <style> body { margin: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background: #f0f0f0; } canvas { border: 1px solid #000; background: #87ceeb; } #controls { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); } button { padding: 10px 20px; font-size: 16px; cursor: pointer; } #score { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,0.7); color: white; padding: 5px 10px; border-radius: 4px; } </style> </head> <body> <canvas id="gameCanvas" width="800" height="600"></canvas> <div id="controls"> <button id="startButton">Start Game</button> </div> <div id="score">Score: 0</div> <script> const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const startButton = document.getElementById('startButton'); let score = 0; let gameRunning = false; let interval; // Player car const player = { x: canvas.width / 2 - 20, y: canvas.height - 60, width: 40, height: 40, color: '#ff4500' }; // Computer cars const cars = []; let carSpeed = 2; // Game functions function draw() { // Clear canvas ctx.clearRect(0, 0, canvas.width, canvas.height); // Draw road ctx.fillStyle = '#87ceeb'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = '#4682b4'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = 'white'; for (let i = 0; i < 20; i++) { ctx.fillRect(380, i * 40, 20, 20); } // Draw player ctx.fillStyle = player.color; ctx.fillRect(player.x, player.y, player.width, player.height); // Draw computer cars cars.forEach(car => { ctx.fillStyle = car.color; ctx.fillRect(car.x, car.y, car.width, car.height); }); // Update score document.getElementById('score').textContent = `Score: ${score}`; } function moveCars() { cars.forEach(car => { car.y += carSpeed; if (car.y > canvas.height) { car.y = -car.height; car.x = Math.random() * (canvas.width - car.width); } }); } function collisionDetection() { for (const car of cars) { if ( player.x < car.x + car.width && player.x + player.width > car.x && player.y < car.y + car.height && player.y + player.height > car.y ) { return true; } } return false; } function startGame() { if (!gameRunning) { gameRunning = true; score = 0; cars.length = 0; carSpeed = 2; // Create computer cars for (let i = 0; i < 5; i++) { cars.push({ x: Math.random() * (canvas.width - 40), y: Math.random() * -200, width: 40, height: 40, color: `hsl(${Math.random() * 360}, 50%, 50%)` }); } // Start game loop interval = setInterval(() => { if (collisionDetection()) { clearInterval(interval); alert('Game Over! Your score: ' + score); gameRunning = false; } else { moveCars(); draw(); carSpeed += 0.005; } }, 30); // Add score increment setInterval(() => { score++; }, 1000); } } // Key controls document.addEventListener('keydown', (e) => { if (gameRunning) { if (e.key === 'ArrowLeft' && player.x > 0) { player.x -= 20; } else if (e.key === 'ArrowRight' && player.x < canvas.width - player.width) { player.x += 20; } } }); // Start button startButton.addEventListener('click', startGame); </script> </body> </html>To use this game:
- Save the code as an HTML file
- Open in any modern web browser
- Click "Start Game" to begin
- Use left/right arrow keys to move
- Avoid the oncoming cars
- Score increases over time
- Game ends when you collide
The game features:
- Simple 2D racing mechanics
- Increasing difficulty over time
- Collision detection
- Score tracking
- Responsive controls
- Basic graphics
You can modify the car colors, size, speed, and other parameters in the JavaScript section to customize the game further. The game uses a canvas element for rendering and basic physics calculations for movement and collisions.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.