import pygame
import sys

# Constants
SCREEN_WIDTH, SCREEN_HEIGHT = 2400, 1200
SPRITE_WIDTH = 165
SPRITE_HEIGHT = 300  # Updated character height
SPRITESHEET_WIDTH = 1024
SPRITESHEET_HEIGHT = 1024
FPS = 60

# Load sprite frames from the sheet
def load_frames(sheet, row, num_frames):
    frames = []
    for i in range(num_frames):
        frame = sheet.subsurface((i * SPRITE_WIDTH, row * SPRITE_HEIGHT, SPRITE_WIDTH, SPRITE_HEIGHT))
        frames.append(frame)
    return frames

def main():
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption("Side Scroller Example")
    clock = pygame.time.Clock()

    # Load sprite sheet
    sheet = pygame.image.load("spritesheet.png").convert_alpha()

    # Assume first row (row 0) is walking right, 5 frames
    walk_right_frames = load_frames(sheet, 0, 5)
    # Flip frames for walking left
    walk_left_frames = [pygame.transform.flip(frame, True, False) for frame in walk_right_frames]

    # Player state
    player_x = 100
    player_y = SCREEN_HEIGHT - SPRITE_HEIGHT
    frame_idx = 0
    frame_timer = 0
    frame_delay = 100  # ms per frame
    velocity_y = 0
    is_jumping = False
    gravity = 1
    move_left = False
    move_right = False
    facing_right = True

    running = True
    while running:
        dt = clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE and not is_jumping:
                    velocity_y = -18
                    is_jumping = True

        # Move player
        keys = pygame.key.get_pressed()
        move_left = keys[pygame.K_LEFT]
        move_right = keys[pygame.K_RIGHT]

        if move_right:
            player_x += 5
            facing_right = True
            frame_timer += dt
            if frame_timer > frame_delay:
                frame_idx = (frame_idx + 1) % len(walk_right_frames)
                frame_timer = 0
        elif move_left:
            player_x -= 5
            facing_right = False
            frame_timer += dt
            if frame_timer > frame_delay:
                frame_idx = (frame_idx + 1) % len(walk_left_frames)
                frame_timer = 0
        else:
            frame_idx = 0  # Idle frame

        # Jumping and gravity
        if is_jumping:
            player_y += velocity_y
            velocity_y += gravity
            if player_y >= SCREEN_HEIGHT - SPRITE_HEIGHT:
                player_y = SCREEN_HEIGHT - SPRITE_HEIGHT
                is_jumping = False
                velocity_y = 0

        # Draw
        screen.fill((50, 50, 50))
        if facing_right:
            screen.blit(walk_right_frames[frame_idx], (player_x, player_y))
        else:
            screen.blit(walk_left_frames[frame_idx], (player_x, player_y))
        pygame.display.flip()

    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    main()