o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Use html with cdn packages to make a game called "squirrel finder": - You are a koala…
- User
Use html with cdn packages to make a game called "squirrel finder":
- You are a koala icon that can move quickly using the arrow keys. The icon image is koa la. png
- A strawberry randomly spawns every second and bounces around. The icon image is strawberry . png
- If a strawberry touches you, you die.
- After three seconds, a squirrel icon spawns and bounces around. The icon image is squirrel. png. If you touch it, you win.
- Make sure all icons are 40 pixels by 40 pixels.
- Use retro colors and a dark background.
- Put "openai" in the game screen and a timer in the corner.
- Display the instructions before the game starts.
- Automatically restart the game when you win or lose.
- Give the code in a single file.
- Assistant
<!DOCTYPE html> <html> <head> <title>Squirrel Finder</title> <style> body { margin: 0; overflow: hidden; background-color: #222; color: #fff; font-family: 'Courier New', monospace; } #gameContainer { position: relative; width: 800px; height: 600px; margin: 0 auto; } #gameCanvas { background-color: #000; border: 2px solid #fff; } #instructionsOverlay { position: absolute; top: 0; left: 0; width: 800px; height: 600px; background-color: rgba(0,0,0,0.8); color: #fff; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; font-size: 20px; z-index: 10; } </style> </head> <body> <div id="gameContainer"> <canvas id="gameCanvas" width="800" height="600"></canvas> <div id="instructionsOverlay"> <h1>Squirrel Finder</h1> <p>You are a koala. Use the arrow keys to move.</p> <p>Avoid the bouncing strawberries. If one touches you, you die.</p> <p>After 3 seconds, a squirrel will appear. Touch it to win!</p> <p>Press any key to start.</p> </div> </div> <script> const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); let gameState = 'instructions'; // 'instructions', 'playing', 'won', 'lost' let keysPressed = {}; let koala = {}; let strawberries = []; let squirrel = null; let gameStartTime = 0; let lastStrawberrySpawnTime = 0; let elapsedTime = 0; // Load images const koalaImage = new Image(); koalaImage.src = 'koala.png'; // Provide the koala.png image in the same folder const strawberryImage = new Image(); strawberryImage.src = 'strawberry.png'; // Provide the strawberry.png image in the same folder const squirrelImage = new Image(); squirrelImage.src = 'squirrel.png'; // Provide the squirrel.png image in the same folder document.addEventListener('keydown', function(e) { if (gameState === 'instructions') { startGame(); } else { keysPressed[e.key] = true; } }); document.addEventListener('keyup', function(e) { keysPressed[e.key] = false; }); function startGame() { document.getElementById('instructionsOverlay').style.display = 'none'; gameState = 'playing'; gameStartTime = Date.now(); lastStrawberrySpawnTime = Date.now(); koala = { x: canvas.width / 2 - 20, y: canvas.height / 2 - 20, width: 40, height: 40, speed: 5, }; strawberries = []; squirrel = null; elapsedTime = 0; requestAnimationFrame(gameLoop); } function gameLoop() { update(); render(); if (gameState === 'playing') { requestAnimationFrame(gameLoop); } else { // Game over, restart after short delay setTimeout(() => { gameState = 'instructions'; document.getElementById('instructionsOverlay').style.display = 'flex'; }, 2000); } } function update() { let now = Date.now(); elapsedTime = Math.floor((now - gameStartTime) / 1000); // Move koala if (keysPressed['ArrowLeft'] || keysPressed['a']) { koala.x -= koala.speed; } if (keysPressed['ArrowRight'] || keysPressed['d']) { koala.x += koala.speed; } if (keysPressed['ArrowUp'] || keysPressed['w']) { koala.y -= koala.speed; } if (keysPressed['ArrowDown'] || keysPressed['s']) { koala.y += koala.speed; } // Keep koala within bounds if (koala.x < 0) koala.x = 0; if (koala.x + koala.width > canvas.width) koala.x = canvas.width - koala.width; if (koala.y < 0) koala.y = 0; if (koala.y + koala.height > canvas.height) koala.y = canvas.height - koala.height; // Spawn strawberries every second if (now - lastStrawberrySpawnTime > 1000) { spawnStrawberry(); lastStrawberrySpawnTime = now; } // Update strawberries strawberries.forEach(function(strawberry) { strawberry.x += strawberry.vx; strawberry.y += strawberry.vy; // Bounce off walls if (strawberry.x < 0 || strawberry.x + strawberry.width > canvas.width) { strawberry.vx *= -1; } if (strawberry.y < 0 || strawberry.y + strawberry.height > canvas.height) { strawberry.vy *= -1; } // Check collision with koala if ( koala.x < strawberry.x + strawberry.width && koala.x + koala.width > strawberry.x && koala.y < strawberry.y + strawberry.height && koala.y + koala.height > strawberry.y ) { // Collision detected gameState = 'lost'; } }); // After 3 seconds, spawn squirrel if not already spawned if (elapsedTime >= 3 && !squirrel) { spawnSquirrel(); } // Update squirrel if (squirrel) { squirrel.x += squirrel.vx; squirrel.y += squirrel.vy; // Bounce off walls if (squirrel.x < 0 || squirrel.x + squirrel.width > canvas.width) { squirrel.vx *= -1; } if (squirrel.y < 0 || squirrel.y + squirrel.height > canvas.height) { squirrel.vy *= -1; } // Check collision with koala if ( koala.x < squirrel.x + squirrel.width && koala.x + koala.width > squirrel.x && koala.y < squirrel.y + squirrel.height && koala.y + koala.height > squirrel.y ) { // Collision detected gameState = 'won'; } } } function render() { // Clear canvas ctx.clearRect(0, 0, canvas.width, canvas.height); // Draw koala ctx.drawImage(koalaImage, koala.x, koala.y, koala.width, koala.height); // Draw strawberries strawberries.forEach(function(strawberry) { ctx.drawImage(strawberryImage, strawberry.x, strawberry.y, strawberry.width, strawberry.height); }); // Draw squirrel if (squirrel) { ctx.drawImage(squirrelImage, squirrel.x, squirrel.y, squirrel.width, squirrel.height); } // Draw 'openai' on the game screen ctx.fillStyle = '#fff'; ctx.font = '30px Courier New'; ctx.fillText('openai', canvas.width / 2 - 50, 50); // Draw timer in the corner ctx.fillStyle = '#fff'; ctx.font = '20px Courier New'; ctx.fillText('Time: ' + elapsedTime + 's', 10, 30); // If game over, display message if (gameState === 'won') { ctx.fillStyle = '#0f0'; ctx.font = '50px Courier New'; ctx.fillText('You Win!', canvas.width / 2 - 100, canvas.height / 2); } else if (gameState === 'lost') { ctx.fillStyle = '#f00'; ctx.font = '50px Courier New'; ctx.fillText('You Died!', canvas.width / 2 - 100, canvas.height / 2); } } function spawnStrawberry() { const strawberry = { x: Math.random() * (canvas.width - 40), y: Math.random() * (canvas.height - 40), width: 40, height: 40, vx: (Math.random() - 0.5) * 4, vy: (Math.random() - 0.5) * 4, }; strawberries.push(strawberry); } function spawnSquirrel() { squirrel = { x: Math.random() * (canvas.width - 40), y: Math.random() * (canvas.height - 40), width: 40, height: 40, vx: (Math.random() - 0.5) * 4, vy: (Math.random() - 0.5) * 4, }; } </script> </body> </html>Instructions:
- Ensure that you have the following images saved in the same directory as this HTML file:
koala.png: The image representing the koala.strawberry.png: The image representing the strawberry.squirrel.png: The image representing the squirrel.
- All images should be 40 pixels by 40 pixels.
- Open the HTML file in a modern web browser to play the game.
Game Controls:
- Use the arrow keys (or W, A, S, D) to move the koala.
- Avoid the bouncing strawberries. If a strawberry touches you, you die.
- After three seconds, a squirrel will appear and bounce around.
- Touch the squirrel to win the game.
- The game will automatically restart when you win or lose.
Game Features:
- Retro colors with a dark background for a classic arcade feel.
- "openai" displayed on the game screen.
- A timer displayed in the corner showing the elapsed time.
- Instructions are displayed before the game starts.
Note:
- If you need placeholder images, you can create simple images using any image editing tool or use online resources for free game icons.
- Make sure the images are named correctly and are in the same directory as your HTML file.
Enjoy the game!
- Ensure that you have the following images saved in the same directory as this HTML file: