import pygame
import sys
# Initialize Pygame
pygame.init()
# Set up the window
window_size = (800, 600)
window = pygame.display.set_mode(window_size)
pygame.display.set_caption("Catch the Fish!")
# define colors
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
BLUE = ( 0, 0, 255)
GREEN = ( 0, 255, 0)
RED = (255, 0, 0)
# Set up the variables needed
clock = pygame.time.Clock()
FPS = 30
score = 0
# The player
player_size = 50
player_position = [350, 550]
# The fish
fish_size = 50
fish_position = [random.randint(0, window_size[0]-fish_size), 0]
fish_speed = 5
# The game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Move the player
# Get keys pressed
keys = pygame.key.get_pressed()
# Check if left or right arrow is pressed and move the player
if keys[pygame.K_LEFT] and player_position[0] > 0:
player_position[0] -= 5
if keys[pygame.K_RIGHT] and player_position[0] < window_size[0] - player_size:
player_position[0] += 5
# Move the fish
if fish_position[1] >= window_size[1]:
fish_position[0] = random.randint(0, window_size[0] - fish_size)
fish_position[1] = 0
else:
fish_position[1] += fish_speed
# Check if the player caught the fish
if fish_position[1] + fish_size >= player_position[1]:
# Check if the fish is in the same x position as the player
if fish_position[0] > player_position[0] and fish_position[0] < player_position[0] + player_size or \
fish_position[0] + fish_size > player_position[0] and fish_position[0] + fish_size < player_position[0] + player_size:
score += 1
fish_position[0] = random.randint(0, window_size[0] - fish_size)
fish_position[1] = 0
# Draw the window
window.fill(BLACK)
pygame.draw.rect(window, WHITE, [player_position[0], player_position[1], player_size, player_size])
pygame.draw.rect(window, GREEN, [fish_position[0], fish_position[1], fish_size, fish_size])
# Display the score
font = pygame.font.SysFont("Arial", 25)
text = font.render("Score: " + str(score), True, WHITE)
window.blit(text, [window_size[0]-100, 20])
# Update the screen
pygame.display.update()
# Tick the clock
clock.tick(FPS)