Use pygame to make a game called "squirrel finder":
-You are a koala icon that can move quickly using the arrow keys. The icon image is koala.png
-A strawberry randomly spawns every second and bounces around.
The icon image is strawberry.png
-After three seconds, a squirrel icon spawns and bounces around.
-If a strawberry touches you, you die.
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
Sure! Below is the complete Python code for the "Squirrel Finder" game using Pygame. Make sure you have koala.png, strawberry.png, and squirrel.png images placed in the same directory as this script. Each image should be 40x40 pixels, or the script will scale them to that size.
python
import pygame
import random
import sys
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# Frames per second
FPS = 60
# Colors (retro palette)
BLACK = (0, 0, 0)
RETRO_GREEN = (0, 255, 0)
# Set up the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Squirrel Finder")
# Load images and scale to 40x40 pixels
koala_img = pygame.image.load('koala.png')
koala_img = pygame.transform.scale(koala_img, (40, 40))
strawberry_img = pygame.image.load('strawberry.png')
strawberry_img = pygame.transform.scale(strawberry_img, (40, 40))
squirrel_img = pygame.image.load('squirrel.png')
squirrel_img = pygame.transform.scale(squirrel_img, (40, 40))
# Retro font
font = pygame.font.Font(None, 36)
# Define game object classes
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = koala_img
self.rect = self.image.get_rect()
self.rect.center = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
self.speed = 7 # Move quickly
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
self.rect.x -= self.speed
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
self.rect.x += self.speed
if keys[pygame.K_UP] or keys[pygame.K_w]:
self.rect.y -= self.speed
if keys[pygame.K_DOWN] or keys[pygame.K_s]:
self.rect.y += self.speed
# Keep player on the screen
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > SCREEN_HEIGHT:
self.rect.bottom = SCREEN_HEIGHT
class Strawberry(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = strawberry_img
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, SCREEN_WIDTH - self.rect.width)
self.rect.y = random.randint(0, SCREEN_HEIGHT - self.rect.height)
self.speedx = random.choice([-4, -3, -2, 2, 3, 4])
self.speedy = random.choice([-4, -3, -2, 2, 3, 4])
def update(self):
self.rect.x += self.speedx
self.rect.y += self.speedy
# Bounce off walls
if self.rect.left <= 0 or self.rect.right >= SCREEN_WIDTH:
self.speedx *= -1
if self.rect.top <= 0 or self.rect.bottom >= SCREEN_HEIGHT:
self.speedy *= -1
class Squirrel(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = squirrel_img
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, SCREEN_WIDTH - self.rect.width)
self.rect.y = random.randint(0, SCREEN_HEIGHT - self.rect.height)
self.speedx = random.choice([-5, -4, -3, 3, 4, 5])
self.speedy = random.choice([-5, -4, -3, 3, 4, 5])
def update(self):
self.rect.x += self.speedx
self.rect.y += self.speedy
# Bounce off walls
if self.rect.left <= 0 or self.rect.right >= SCREEN_WIDTH:
self.speedx *= -1
if self.rect.top <= 0 or self.rect.bottom >= SCREEN_HEIGHT:
self.speedy *= -1
# Create sprite groups
all_sprites = pygame.sprite.Group()
strawberry_group = pygame.sprite.Group()
squirrel_group = pygame.sprite.Group()
# Create player
player = Player()
all_sprites.add(player)
# Set up clock
clock = pygame.time.Clock()
# Game variables
running = True
game_over = False
start_time = pygame.time.get_ticks()
strawberry_spawn_time = pygame.time.get_ticks()
squirrel_spawned = False
show_instructions = True
while running:
clock.tick(FPS)
if show_instructions:
# Display instructions
screen.fill(BLACK)
instructions = [
"Squirrel Finder",
"You are the koala.",
"Use arrow keys to move.",
"Avoid the strawberries!",
"Find and touch the squirrel to win.",
"Press any key to start."
]
for idx, line in enumerate(instructions):
text = font.render(line, True, RETRO_GREEN)
text_rect = text.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 - 100 + idx*40))
screen.blit(text, text_rect)
pygame.display.flip()
# Wait for any key
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
show_instructions = False
elif event.type == pygame.KEYDOWN:
show_instructions = False
start_time = pygame.time.get_ticks()
continue
else:
# Process input
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update
now = pygame.time.get_ticks()
# Spawn strawberries every second
if now - strawberry_spawn_time > 1000:
strawberry = Strawberry()
all_sprites.add(strawberry)
strawberry_group.add(strawberry)
strawberry_spawn_time = now
# Spawn squirrel after 3 seconds
if not squirrel_spawned and now - start_time > 3000:
squirrel = Squirrel()
all_sprites.add(squirrel)
squirrel_group.add(squirrel)
squirrel_spawned = True
all_sprites.update()
# Check collisions
if pygame.sprite.spritecollide(player, strawberry_group, False):
# Player dies
game_over = True
result_text = "You touched a strawberry! You lose."
if pygame.sprite.spritecollide(player, squirrel_group, False):
# Player wins
game_over = True
result_text = "You found the squirrel! You win!"
# Draw
screen.fill(BLACK)
all_sprites.draw(screen)
# Display "openai"
openai_text = font.render("openai", True, RETRO_GREEN)
screen.blit(openai_text, (10, 10))
# Display timer
elapsed_time = (pygame.time.get_ticks() - start_time) // 1000
timer_text = font.render(f"Time: {elapsed_time}", True, RETRO_GREEN)
screen.blit(timer_text, (SCREEN_WIDTH - 150, 10))
pygame.display.flip()
# If game over, reset
if game_over:
# Display result
screen.fill(BLACK)
result = font.render(result_text, True, RETRO_GREEN)
result_rect = result.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
screen.blit(result, result_rect)
pygame.display.flip()
pygame.time.delay(3000)
# Reset game
all_sprites.empty()
all_sprites.add(player)
strawberry_group.empty()
squirrel_group.empty()
player.rect.center = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
game_over = False
squirrel_spawned = False
start_time = pygame.time.get_ticks()
strawberry_spawn_time = pygame.time.get_ticks()
# Quit Pygame
pygame.quit()
sys.exit()
Instructions:
Dependencies: Make sure you have Pygame installed. You can install it using:
code
pip install pygame
Images: Place koala.png, strawberry.png, and squirrel.png in the same directory as the script. Ensure each image is 40x40 pixels or larger (the script will scale larger images down to 40x40 pixels).
Run the Game:
code
python squirrel_finder.py
Game Controls:
Use arrow keys or WASD to move the koala.
Avoid the moving strawberries.
After 3 seconds, a squirrel will appear. Touch the squirrel to win.
If a strawberry touches you, you lose.
The game automatically restarts when you win or lose.
Game Screen Elements:
"openai" is displayed in the top-left corner.
A timer is displayed in the top-right corner, showing the elapsed time since the game started.