Use HTML5 to make a game called "squirrel finder":
You are a koala icon that can move quickly using the arrow keys.
-A strawberry randomly spawns every second and bounces around.
If a strawberry touches you, you die.
After three seconds, a squirrel icon spawns and bounces around. 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
Here's the code for the "Squirrel Finder" game as per your specifications. Since the code needs to be in a single file, I've represented the koala, strawberries, and squirrel with colored squares instead of images. The koala is blue, strawberries are red, and the squirrel is green. All entities are 40x40 pixels. Enjoy the game!
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Squirrel Finder Game</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000; /* Dark background */
color: #fff; /* Retro color for text */
font-family: 'Courier New', Courier, monospace;
}
#gameCanvas {
display: block;
margin: auto;
background-color: #000;
}
#instructions {
position: absolute;
top: 10%;
left: 50%;
transform: translateX(-50%);
color: #fff;
font-family: 'Courier New', Courier, monospace;
text-align: center;
}
#timer {
position: absolute;
top: 10px;
right: 10px;
color: #fff;
font-family: 'Courier New', Courier, monospace;
font-size: 20px;
}
#openai {
position: absolute;
bottom: 10px;
left: 10px;
color: #fff;
font-family: 'Courier New', Courier, monospace;
font-size: 20px;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div id="instructions">
<h1>Squirrel Finder</h1>
<p>You are the <span style="color:#00f;">Koala</span>. Use the arrow keys to move.</p>
<p>Avoid the <span style="color:#f00;">strawberries</span> that spawn every second.</p>
<p>After 3 seconds, a <span style="color:#0f0;">squirrel</span> will appear. Find and touch the squirrel to win!</p>
<p>If a strawberry touches you, you die.</p>
<p>Press any key to start.</p>
</div>
<div id="timer">Time: 0s</div>
<div id="openai">openai</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
let gameStarted = false;
let gameOver = false;
let gameWin = false;
let timeElapsed = 0;
let timeStart = null;
// Player object
let player = {
x: canvas.width / 2 - 20,
y: canvas.height / 2 - 20,
width: 40,
height: 40,
speed: 7,
dx: 0,
dy: 0,
color: '#00f' // Blue color for koala
};
// Enemies (strawberries)
let strawberries = [];
// Squirrel
let squirrel = null;
// Keyboard input
const keys = {};
document.addEventListener('keydown', function(e) {
keys[e.key] = true;
if (!gameStarted) {
gameStarted = true;
document.getElementById('instructions').style.display = 'none';
timeStart = Date.now();
gameLoop();
}
});
document.addEventListener('keyup', function(e) {
keys[e.key] = false;
});
function updatePlayer() {
if (keys['ArrowUp'] || keys['w']) {
player.dy = -player.speed;
} else if (keys['ArrowDown'] || keys['s']) {
player.dy = player.speed;
} else {
player.dy = 0;
}
if (keys['ArrowLeft'] || keys['a']) {
player.dx = -player.speed;
} else if (keys['ArrowRight'] || keys['d']) {
player.dx = player.speed;
} else {
player.dx = 0;
}
player.x += player.dx;
player.y += player.dy;
// Keep player within canvas
if (player.x < 0) player.x = 0;
if (player.y < 0) player.y = 0;
if (player.x + player.width > canvas.width) player.x = canvas.width - player.width;
if (player.y + player.height > canvas.height) player.y = canvas.height - player.height;
}
function spawnStrawberry() {
const strawberry = {
x: Math.random() * (canvas.width - 40),
y: Math.random() * (canvas.height - 40),
width: 40,
height: 40,
dx: (Math.random() < 0.5 ? -1 : 1) * 3,
dy: (Math.random() < 0.5 ? -1 : 1) * 3,
color: '#f00' // Red color for strawberry
};
strawberries.push(strawberry);
}
function updateStrawberries() {
strawberries.forEach(function(strawberry) {
strawberry.x += strawberry.dx;
strawberry.y += strawberry.dy;
// Bounce off walls
if (strawberry.x < 0 || strawberry.x + strawberry.width > canvas.width) {
strawberry.dx *= -1;
}
if (strawberry.y < 0 || strawberry.y + strawberry.height > canvas.height) {
strawberry.dy *= -1;
}
// Check collision with player
if (checkCollision(player, strawberry)) {
gameOver = true;
}
});
}
function spawnSquirrel() {
if (!squirrel) {
squirrel = {
x: Math.random() * (canvas.width - 40),
y: Math.random() * (canvas.height - 40),
width: 40,
height: 40,
dx: (Math.random() < 0.5 ? -1 : 1) * 3,
dy: (Math.random() < 0.5 ? -1 : 1) * 3,
color: '#0f0' // Green color for squirrel
};
}
}
function updateSquirrel() {
if (squirrel) {
squirrel.x += squirrel.dx;
squirrel.y += squirrel.dy;
// Bounce off walls
if (squirrel.x < 0 || squirrel.x + squirrel.width > canvas.width) {
squirrel.dx *= -1;
}
if (squirrel.y < 0 || squirrel.y + squirrel.height > canvas.height) {
squirrel.dy *= -1;
}
// Check collision with player
if (checkCollision(player, squirrel)) {
gameWin = true;
}
}
}
function checkCollision(a, b) {
return a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y;
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw player (koala)
ctx.fillStyle = player.color;
ctx.fillRect(player.x, player.y, player.width, player.height);
// Draw strawberries
strawberries.forEach(function(strawberry) {
ctx.fillStyle = strawberry.color;
ctx.fillRect(strawberry.x, strawberry.y, strawberry.width, strawberry.height);
});
// Draw squirrel
if (squirrel) {
ctx.fillStyle = squirrel.color;
ctx.fillRect(squirrel.x, squirrel.y, squirrel.width, squirrel.height);
}
}
function updateTimer() {
timeElapsed = Math.floor((Date.now() - timeStart) / 1000);
document.getElementById('timer').innerText = 'Time: ' + timeElapsed + 's';
}
function resetGame() {
gameOver = false;
gameWin = false;
gameStarted = false;
timeElapsed = 0;
timeStart = null;
player.x = canvas.width / 2 - 20;
player.y = canvas.height / 2 - 20;
strawberries = [];
squirrel = null;
document.getElementById('instructions').style.display = 'block';
}
function gameLoop() {
if (!gameOver && !gameWin) {
updatePlayer();
updateStrawberries();
if (timeElapsed >= 3) {
spawnSquirrel();
updateSquirrel();
}
draw();
updateTimer();
requestAnimationFrame(gameLoop);
} else {
if (gameOver) {
alert('You were caught by a strawberry! Game Over.');
} else if (gameWin) {
alert('You found the squirrel! You win!');
}
resetGame();
}
}
// Spawn strawberries every second
setInterval(function() {
if (gameStarted && !gameOver && !gameWin) {
spawnStrawberry();
}
}, 1000);
</script>
</body>
</html>
Instructions:
Copy the entire code above and save it in a file named squirrel_finder.html.
Open the file in a modern web browser (like Chrome, Firefox, or Edge) to play the game.
Use the arrow keys to move the blue square (your koala).
Avoid the red squares (strawberries) that spawn every second and bounce around.
After 3 seconds, a green square (squirrel) will appear. Touch it to win.
If a strawberry touches you, you lose.
The game will automatically restart when you win or lose.