import pygame

# pygame initialization
pygame.init()
clock = pygame.time.Clock()

WIDTH = 800
HEIGHT = 400

window = pygame.display.set_mode((WIDTH, HEIGHT))
background = pygame.image.load("images/bg2.jpg")
window.blit(background, (0, 0))
pygame.display.update()

# prepare messages
pygame.font.init()
font = pygame.font.SysFont('Verdana', 24)
help0 = font.render("Type q to quit.", True, (0, 0, 0))
help1 = font.render("Use the left and right arrows to move side to side.", True, (0, 0, 0))
help2 = font.render("Press h to toggle help.", True, (0, 0, 0))

# set window title
pygame.display.set_caption("Walker")
pygame.display.update()

IMAGE_WIDTH = 192
IMAGE_HEIGHT = 256
NUM_FRAMES = 8

character_images = {
    "left_idle": pygame.image.load("images/zombie_left_idle.png"),
    "left_walk0": pygame.image.load("images/zombie_left_walk0.png"),
    "left_walk1": pygame.image.load("images/zombie_left_walk1.png"),
    "left_walk2": pygame.image.load("images/zombie_left_walk2.png"),
    "left_walk3": pygame.image.load("images/zombie_left_walk3.png"),
    "left_walk4": pygame.image.load("images/zombie_left_walk4.png"),
    "left_walk5": pygame.image.load("images/zombie_left_walk5.png"),
    "left_walk6": pygame.image.load("images/zombie_left_walk6.png"),
    "left_walk7": pygame.image.load("images/zombie_left_walk7.png"),
    "right_idle": pygame.image.load("images/zombie_right_idle.png"),
    "right_walk0": pygame.image.load("images/zombie_right_walk0.png"),
    "right_walk1": pygame.image.load("images/zombie_right_walk1.png"),
    "right_walk2": pygame.image.load("images/zombie_right_walk2.png"),
    "right_walk3": pygame.image.load("images/zombie_right_walk3.png"),
    "right_walk4": pygame.image.load("images/zombie_right_walk4.png"),
    "right_walk5": pygame.image.load("images/zombie_right_walk5.png"),
    "right_walk6": pygame.image.load("images/zombie_right_walk6.png"),
    "right_walk7": pygame.image.load("images/zombie_right_walk7.png")
}

data = {
    "x": 0,
    "y": HEIGHT - IMAGE_HEIGHT,
    "x_speed": 0,
    "facing": "right",
    "frame": 0,
    "mode": "game"
}

def update_character():
    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT] and keys[pygame.K_LEFT]:
        data["x_speed"] = 0
    elif keys[pygame.K_RIGHT]:
        data["x_speed"] = 10
        data["facing"] = "right"
    elif keys[pygame.K_LEFT]:
        data["x_speed"] = -10
        data["facing"] = "left"
    else:
        data["x_speed"] = 0

    new_x = data["x"] + data["x_speed"]

    if new_x >= WIDTH - IMAGE_WIDTH:
        new_x = WIDTH - IMAGE_WIDTH
    elif new_x <= 0:
        new_x = 0

    data["x"] = new_x

def draw_character():
    direction = data["facing"]
    speed = data["x_speed"]
    x = data["x"]
    y = data["y"]
    image_name = ""

    if speed != 0:
        new_frame = data["frame"] + 1
        if new_frame >= NUM_FRAMES:
            new_frame = 0
        data["frame"] = new_frame
        image_name = direction + "_walk" + str(new_frame)
    else:
        image_name = direction + "_idle"

    image = character_images[image_name]
    window.blit(image, (x, y))

def draw_background():
    window.blit(background, (0, 0))

def redraw():
    update_character()
    draw_background()
    draw_character()
    pygame.display.update()

def run_app():
    app_is_running = True
    while app_is_running == True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    app_is_running = False
            elif event.type == pygame.QUIT:
                app_is_running = False

        redraw()
        clock.tick(24)
    pygame.quit()


run_app()