Make a futuristic looking snake web game for 2 players one is playinf with wasd and the…
- User
Make a futuristic looking snake web game for 2 players one is playinf with wasd and the other one with the arrow keys make is futuristic and modern
Assistant
Futuristic Snake Web Game
Game Description
A modern, futuristic snake game for 2 players. Player 1 uses WASD keys, and Player 2 uses arrow keys. The game features a sleek, neon-lit design and a competitive gameplay experience.
Game Code
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Futuristic Snake Game</title> <link rel="stylesheet" href="styles.css"> </head> <body> <canvas id="game-canvas" width="800" height="600"></canvas> <script src="script.js"></script> </body> </html>CSS (in styles.css file)
body { background-color: #2f2f2f; font-family: Arial, sans-serif; } #game-canvas { border: 1px solid #ffffff; background-color: #000000; }JavaScript (in script.js file)
// Get the canvas element const canvas = document.getElementById('game-canvas'); const ctx = canvas.getContext('2d'); // Set the canvas dimensions canvas.width = 800; canvas.height = 600; // Define the game variables let player1 = { x: 100, y: 100, velocityX: 0, velocityY: 0, score: 0, snake: [{ x: 100, y: 100 }, { x: 90, y: 100 }, { x: 80, y: 100 }] }; let player2 = { x: 700, y: 100, velocityX: 0, velocityY: 0, score: 0, snake: [{ x: 700, y: 100 }, { x: 710, y: 100 }, { x: 720, y: 100 }] }; let food = { x: Math.floor(Math.random() * (canvas.width / 10)) * 10, y: Math.floor(Math.random() * (canvas.height / 10)) * 10 }; // Draw the game elements function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); // Draw the players player1.snake.forEach((segment, index) => { if (index === 0) { ctx.fillStyle = '#00ff00'; // Green for player 1 } else { ctx.fillStyle = '#33ff33'; // Light green for player 1 } ctx.fillRect(segment.x, segment.y, 10, 10); }); player2.snake.forEach((segment, index) => { if (index === 0) { ctx.fillStyle = '#ff0000'; // Red for player 2 } else { ctx.fillStyle = '#ff3333'; // Light red for player 2 } ctx.fillRect(segment.x, segment.y, 10, 10); }); // Draw the food ctx.fillStyle = '#ffff00'; // Yellow for food ctx.fillRect(food.x, food.y, 10, 10); // Draw the scores ctx.font = '24px Arial'; ctx.fillStyle = '#ffffff'; ctx.textAlign = 'left'; ctx.textBaseline = 'top'; ctx.fillText(`Player 1: ${player1.score}`, 10, 10); ctx.textAlign = 'right'; ctx.fillText(`Player 2: ${player2.score}`, canvas.width - 10, 10); } // Update the game state function update() { // Move the players player1.x += player1.velocityX; player1.y += player1.velocityY; player2.x += player2.velocityX; player2.y += player2.velocityY; // Update the player snakes player1.snake.unshift({ x: player1.x, y: player1.y }); if (player1.snake.length > 3 + player1.score) { player1.snake.pop(); } player2.snake.unshift({ x: player2.x, y: player2.y }); if (player2.snake.length > 3 + player2.score) { player2.snake.pop(); } // Check for collisions with food if (player1.x === food.x && player1.y === food.y) { player1.score++; food = { x: Math.floor(Math.random() * (canvas.width / 10)) * 10, y: Math.floor(Math.random() * (canvas.height / 10)) * 10 }; } if (player2.x === food.x && player2.y === food.y) { player2.score++; food = { x: Math.floor(Math.random() * (canvas.width / 10)) * 10, y: Math.floor(Math.random() * (canvas.height / 10)) * 10 }; } // Check for collisions with walls and self if (player1.x < 0 || player1.x >= canvas.width || player1.y < 0 || player1.y >= canvas.height) { alert('Player 1 lost!'); player1 = { x: 100, y: 100, velocityX: 0, velocityY: 0, score: 0, snake: [{ x: 100, y: 100 }, { x: 90, y: 100 }, { x: 80, y: 100 }] }; } if (player2.x < 0 || player2.x >= canvas.width || player2.y < 0 || player2.y >= canvas.height) { alert('Player 2 lost!'); player2 = { x: 700, y: 100, velocityX: 0, velocityY: 0, score: 0, snake: [{ x: 700, y: 100 }, { x: 710, y: 100 }, { x: 720, y: 100 }] }; } for (let i = 1; i < player1.snake.length; i++) { if (player1.snake[0].x === player1.snake[i].x && player1.snake[0].y === player1.snake[i].y) { alert('Player 1 lost!'); player1 = { x: 100, y: 100, velocityX: 0, velocityY: 0, score: 0, snake: [{ x: 100, y: 100 }, { x: 90, y: 100 }, { x: 80, y: 100 }] }; } } for (let i = 1; i < player2.snake.length; i++) { if (player2.snake[0].x === player2.snake[i].x && player2.snake[0].y === player2.snake[i].y) { alert('Player 2 lost!'); player2 = { x: 700, y: 100, velocityX: 0, velocityY: 0, score: 0, snake: [{ x: 700, y: 100 }, { x: 710, y: 100 }, { x: 720, y: 100 }] }; } } } // Handle keyboard input document.addEventListener('keydown', (event) => { switch (event.key) { case 'w': player1.velocityY = -10; player1.velocityX = 0; break; case 'a': player1.velocityX = -10; player1.velocityY = 0; break; case 's': player1.velocityY = 10; player1.velocityX = 0; break; case 'd': player1.velocityX = 10; player1.velocityY = 0; break; case 'ArrowUp': player2.velocityY = -10; player2.velocityX = 0; break; case 'ArrowDown': player2.velocityY = 10; player2.velocityX = 0; break; case 'ArrowLeft': player2.velocityX = -10; player2.velocityY = 0; break; case 'ArrowRight': player2.velocityX = 10; player2.velocityY = 0; break; } }); // Main game loop setInterval(() => { update(); draw(); }, 100);How to Run the Game
- Create a new HTML file and copy the provided HTML code into it.
- Create a new CSS file named
styles.cssand copy the provided CSS code into it. - Create a new JavaScript file named
script.jsand copy the provided JavaScript code into it. - Open the HTML file in a web browser to play the game.
Gameplay
- Player 1 uses WASD keys to control their snake.
- Player 2 uses arrow keys to control their snake.
- The game ends when one player collides with the wall or their own snake.
- The player with the highest score at the end of
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.