import pygame
import sys

# Set up display
screen_width = 900
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))

# Set up colors (RGB format)
kirby_pink_color = (255, 105, 180)
kirby_yellow_color = (255, 255, 0)
ground_color = (0, 255, 0)

# Kirby properties
circle_radius = 25  # Initial radius
circle_y_offset = 25  # Offset from the top of Kirby
crouch_scale = 0.5  # Crouch scale
gravity = 1
jump_height = -15  # Set jump height

# Kirby position and velocity
kirby_x = 425
kirby_y = 500
kirby_x_speed = 0
kirby_y_speed = 0
scroll_offset = 0

# Kirby crouching and in air states
is_crouching = False
in_air = False

# Kirby movement flags
is_moving_left = False
is_moving_right = False

# Load the Kirby face image
kirby_face = pygame.image.load(“kirby_face.png”)

# Ground properties
ground_x = 0
ground_y = 600

# Game loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_s and not is_crouching:
                is_crouching = True
            elif event.key == pygame.K_w and not in_air:
                kirby_y_speed = jump_height
                in_air = True
            elif event.key == pygame.K_e and not is_crouching:
                is_floating = True
            elif event.key == pygame.K_r:  # Kirby exhale
                is_floating = False
            elif event.key == pygame.K_a:  # Kirby move left
                is_moving_left = True
            elif event.key == pygame.K_d:  # Kirby move right
                is_moving_right = True
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_s:  # Kirby stop crouch
                is_crouching = False
            elif event.key == pygame.K_a:  # Kirby stop moving left
                is_moving_left = False
            elif event.key == pygame.K_d:  # Kirby stop moving right
                is_moving_right = False

    # Kirby Floating set up
    if is_floating:
        gravity = 0.3
        jump_height = -6.5
        in_air = False
        is_crouching = False
        circle_radius = 35
        circle_y_offset = 35
    else:
        gravity = 1
        jump_height = -15
        in_air = True
        circle_radius = 25
        circle_y_offset = 25

    # Apply gravity to Kirby
    kirby_y_speed += gravity

    # Apply horizontal motion
    if is_moving_left:
        kirby_x_speed = -5
    elif is_moving_right:
        kirby_x_speed = 5
    else:
        kirby_x_speed = 0

    # Update Kirby position
    kirby_x += kirby_x_speed
    kirby_y += kirby_y_speed

    # Collision with the ground
    if kirby_y + circle_radius >= ground_y:
        kirby_y = ground_y - circle_radius
        kirby_y_speed = 0
        gravity = 1
        jump_height = -15
        is_floating = False
        in_air = False

    # Collision with the sides of the screen
    if kirby_x < 0:
        kirby_x = 0
    elif kirby_x > screen_width - 2 * circle_radius:
        kirby_x = screen_width - 2 * circle_radius

    # Scroll screen to keep Kirby on the left side
    if kirby_x - scroll_offset < 200:
        scroll_offset = kirby_x - 200
    elif kirby_x - scroll_offset > 700:
        scroll_offset = kirby_x - 700

    # Draw background
    screen.fill((100, 100, 255))  # Blue background

    # Draw ground
    pygame.draw.rect(screen, ground_color, (ground_x - scroll_offset, ground_y, screen_width, 50))

    # Draw Kirby
    if is_crouching:
        pygame.draw.ellipse(screen, kirby_pink_color,
                            (kirby_x - scroll_offset,
                             kirby_y + circle_radius * (1.5 - crouch_scale),
                             2 * circle_radius,
                             crouch_scale * 2 * circle_radius))
        # Scale and draw the Kirby face when crouching
        kirby_face_scaled = pygame.transform.scale(kirby_face, (2 * circle_radius, 2 * circle_radius * crouch_scale))
        screen.blit(kirby_face_scaled, (kirby_x - scroll_offset, kirby_y + circle_radius * (1.5 - crouch_scale)))
    else:
        pygame.draw.circle(screen, kirby_pink_color,
                           (kirby_x - scroll_offset + circle_radius, kirby_y + circle_radius),
                           circle_radius)
        # Scale and draw the Kirby face when not crouching
        kirby_face_scaled = pygame.transform.scale(kirby_face, (2 * circle_radius, 2 * circle_radius))
        screen.blit(kirby_face_scaled, (kirby_x - scroll_offset, kirby_y))

    # Update the display
    pygame.display.flip()

    # Cap the frame rate
    pygame.time.Clock().tick(60)