Here is the code for the "Squirrel Finder" game in a single HTML file. You can copy and paste this into a file with a .html extension and open it in a web browser to play the game.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Squirrel Finder</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000000;
color: #FFFFFF;
font-family: 'Courier New', Courier, monospace;
}
#gameCanvas {
display: block;
margin: 0 auto;
background-color: #000000;
border: 1px solid #FFFFFF;
}
#instructions {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #000000;
padding: 20px;
border: 1px solid #FFFFFF;
text-align: center;
}
#instructions button {
margin-top: 10px;
padding: 5px 10px;
background-color: #FFFFFF;
color: #000000;
border: none;
cursor: pointer;
}
#instructions button:hover {
background-color: #CCCCCC;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="600" height="400"></canvas>
<div id="instructions">
<h1>Squirrel Finder</h1>
<p>You are the Koala. Use the arrow keys to move quickly.</p>
<p>A strawberry will spawn every second and bounce around.</p>
<p>If a strawberry touches you, you die.</p>
<p>After three seconds, a squirrel will appear and bounce around.</p>
<p>If you touch the squirrel, you win!</p>
<button onclick="startGame()">Start Game</button>
</div>
<script>
// Get the canvas and context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game variables
let koala = { x: 280, y: 180, width: 40, height: 40, speed: 5 };
let strawberries = [];
let squirrel = null;
let keysPressed = {};
let gameOver = false;
let gameWon = false;
let gameStart = false;
let timer = 0;
let gameTime = 0;
// Retro colors
const colors = {
background: '#000000',
koala: '#00FF00',
strawberry: '#FF0000',
squirrel: '#FFFF00',
text: '#FFFFFF'
};
// Event listeners for key presses
document.addEventListener('keydown', function(e) {
keysPressed[e.key] = true;
});
document.addEventListener('keyup', function(e) {
keysPressed[e.key] = false;
});
function startGame() {
document.getElementById('instructions').style.display = 'none';
gameStart = true;
gameOver = false;
gameWon = false;
timer = 0;
gameTime = 0;
koala.x = 280;
koala.y = 180;
strawberries = [];
squirrel = null;
requestAnimationFrame(gameLoop);
}
function gameLoop(timestamp) {
if (!gameStart) return;
update();
draw();
if (gameOver || gameWon) {
setTimeout(startGame, 2000); // Restart the game after 2 seconds
return;
}
requestAnimationFrame(gameLoop);
}
function update() {
// Update timer
timer += 1/60;
gameTime += 1/60;
// Move koala
if (keysPressed['ArrowUp'] && koala.y > 0) koala.y -= koala.speed;
if (keysPressed['ArrowDown'] && koala.y + koala.height < canvas.height) koala.y += koala.speed;
if (keysPressed['ArrowLeft'] && koala.x > 0) koala.x -= koala.speed;
if (keysPressed['ArrowRight'] && koala.x + koala.width < canvas.width) koala.x += koala.speed;
// Spawn strawberries every second
if (Math.floor(timer) >= 1) {
spawnStrawberry();
timer = 0;
}
// Update strawberries
strawberries.forEach(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 (checkCollision(koala, strawberry)) {
gameOver = true;
}
});
// Spawn squirrel after 3 seconds
if (!squirrel && gameTime >= 3) {
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 (checkCollision(koala, squirrel)) {
gameWon = true;
}
}
}
function draw() {
// Clear canvas
ctx.fillStyle = colors.background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw "openai" text
ctx.fillStyle = colors.text;
ctx.font = '20px Courier New';
ctx.fillText('openai', canvas.width / 2 - 30, 30);
// Draw timer
ctx.fillText('Time: ' + Math.floor(gameTime) + 's', 10, 30);
// Draw koala
drawKoala(koala.x, koala.y);
// Draw strawberries
strawberries.forEach(strawberry => {
drawStrawberry(strawberry.x, strawberry.y);
});
// Draw squirrel
if (squirrel) {
drawSquirrel(squirrel.x, squirrel.y);
}
// Display game over or win message
if (gameOver) {
ctx.fillStyle = colors.text;
ctx.font = '40px Courier New';
ctx.fillText('You Died!', canvas.width / 2 - 80, canvas.height / 2);
}
if (gameWon) {
ctx.fillStyle = colors.text;
ctx.font = '40px Courier New';
ctx.fillText('You Win!', canvas.width / 2 - 80, canvas.height / 2);
}
}
function spawnStrawberry() {
let strawberry = {
x: Math.random() * (canvas.width - 40),
y: Math.random() * (canvas.height - 40),
width: 40,
height: 40,
vx: (Math.random() < 0.5 ? -1 : 1) * 2,
vy: (Math.random() < 0.5 ? -1 : 1) * 2
};
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 ? -1 : 1) * 3,
vy: (Math.random() < 0.5 ? -1 : 1) * 3
};
}
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 drawKoala(x, y) {
ctx.fillStyle = colors.koala;
ctx.fillRect(x, y, 40, 40);
// Simple koala face
ctx.fillStyle = colors.background;
ctx.fillRect(x + 10, y + 10, 5, 5);
ctx.fillRect(x + 25, y + 10, 5, 5);
ctx.fillRect(x + 15, y + 25, 10, 5);
}
function drawStrawberry(x, y) {
ctx.fillStyle = colors.strawberry;
ctx.beginPath();
ctx.moveTo(x + 20, y);
ctx.lineTo(x + 40, y + 20);
ctx.lineTo(x + 20, y + 40);
ctx.lineTo(x, y + 20);
ctx.closePath();
ctx.fill();
}
function drawSquirrel(x, y) {
ctx.fillStyle = colors.squirrel;
ctx.fillRect(x, y, 40, 40);
// Simple squirrel tail
ctx.fillStyle = colors.background;
ctx.fillRect(x + 25, y + 10, 5, 20);
}
</script>
</body>
</html>
Instructions:
- Copy the entire code above.
- Paste it into a text editor and save the file with a name like
squirrel_finder.html.
- Open the saved HTML file in a modern web browser (e.g., Chrome, Firefox).
- Read the instructions displayed and click the "Start Game" button to begin playing.
- Use the arrow keys to move the koala.
- Avoid the strawberries and try to touch the squirrel after it appears to win the game.
- The game will automatically restart if you win or lose.
Game Features:
- Koala Movement: Control the koala using the arrow keys. The koala moves quickly to allow you to navigate the game area.
- Strawberries: A strawberry spawns every second at a random location and moves in a random direction. If a strawberry touches the koala, you lose.
- Squirrel: The squirrel spawns after three seconds, moves around the screen, and if you touch it, you win the game.
- Retro Graphics: The game uses simple shapes and colors to create a retro gaming experience on a dark background.
- Game Text: The word "openai" is displayed on the game screen, and there's a timer in the corner showing how long you've been playing.
- Instructions: Before the game starts, instructions are displayed to help you understand how to play.
- Automatic Restart: After winning or losing, the game automatically restarts after a short delay.
Enjoy playing "Squirrel Finder"!