import pygame
import random

# 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")
}

coin_image = pygame.image.load("images/coin_gold.png")
bomb_image = pygame.image.load("images/bomb.png")
object_types = ["coin", "bomb"]

data = {
    "x": 0,
    "y": HEIGHT - IMAGE_HEIGHT,
    "x_speed": 0,
    "facing": "right",
    "frame": 0,
    "mode": "game",
    "score": 0,
    "object_x": 0,
    "object_y": 0,
    "object_yspeed": 10,
    "object_type": "coin",
    "character_bb": (40, 76, 112, 180),
    "coin_bb": (32, 32, 64, 64),
    "bomb_bb": (11, 13, 104, 115)
}

def check_collision():
    cbx, cby, cbw, cbh = data["character_bb"]
    cl = data["x"] + cbx
    cr = data["x"] + cbx + cbw
    ct =  data["y"] + cby
    cb = data["y"] + cby + cbh

    type = data["object_type"]
    obx, oby, obw, obh = data[type + "_bb"]
    ol = data["object_x"] + obx
    o_r = data["object_x"] + obx + obw
    ot = data["object_y"] + oby
    ob = data["object_y"] + oby + obh

    return cr >= ol and cl <= o_r and cb >= ot and ct <= ob

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

    if check_collision():
        if data["object_type"] == "coin":
            data["score"] += 1
            reset_object()
        else:
            print("game over!")
            data["mode"] = "game_over"

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 reset_object():
    data["object_y"] = -128
    data["object_x"] = random.randint(0, WIDTH - 128)
    data["object_type"] = random.choice(object_types)

def update_object():
    new_y = data["object_y"] + data["object_yspeed"]
    if new_y >= HEIGHT:
        reset_object()
    else:
        data["object_y"] = new_y

def draw_object():
    x = data["object_x"]
    y = data["object_y"]
    type = data["object_type"]
    if type == "coin":
        window.blit(coin_image, (x, y))
    else:
        window.blit(bomb_image, (x, y))

def redraw():
    update_object()     # <- add this
    update_character()
    draw_background()
    draw_character()
    draw_object()       # <- add this
    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()