import pgzrun
import random
from pgzhelper import *


WIDTH = 640
HEIGHT = 480
BACKGROUND_IMG = "background"

boss = Actor("boss_0") # default pos is 0,0
boss.images = ['boss_0', 'boss_1', 'boss_2', 'boss_3']
boss.fps = 10

dragon = Actor('dragon_0')
dragon.images = ['dragon_0', 'dragon_1', 'dragon_2', 'dragon_3', 'dragon_4', 'dragon_5']
dragon.fps = 20

# position the boss
boss.y = HEIGHT // 2
boss.right = WIDTH # sets image to right side
boss.frame = 1
delay_timer = 0
boss_direction = 1

dragon.y = HEIGHT // 2
dragon.left = 0
dragon.frame = 0
dragon.direction = 1


fireballs = [] # a list to collect the fireballs when created
fireball_delay = 0

demons = []
good_babies = []
evil_babys = []
score = 0

def update_boss():
    global delay_timer, boss_direction, demons, good_babies
    boss.animate()
    if delay_timer == 10:
        delay_timer = 0
        if boss_direction == 1:
            if boss.bottom < HEIGHT:
                boss.y += 20
            else:
                boss_direction = 0
        if boss_direction == 0:
            if boss.top > 0:
                boss.y -= 20
            else:
                boss_direction = 1
    else:
        delay_timer += 1

    if random.randint(1,75) == 1:
        demon = Actor('demon_0')
        demon.images = ['demon_0','demon_1','demon_2','demon_3']
        demon.fps = 15
        demon.animate()
        demon.x = boss.x - 40
        demon.y = boss.y - 40
        demon.direction = random.randint(110, 240)
        demons.append(demon)
        
    if random.randint(1,100) == 1:
        baby = Actor('baby_good')
        baby.fps = 15
        baby.x = boss.x - 40
        baby.y = boss.y - 40
        baby.direction = random.randint(120, 220)
        good_babies.append(baby)


def update_dragon():
    global fireball_delay, fireballs

    if keyboard.up:
        if dragon.top > 0:
            dragon.y -= 2

    if keyboard.down:
        if dragon.bottom < HEIGHT:
            dragon.y += 2

    if fireball_delay == 0:
        if keyboard.space:
            fireball = Actor('fireball')
            fireball.x = dragon.x + 50
            fireball.y = dragon.y - 50
            fireballs.append(fireball)
            fireball_delay = 20 # one-third of a second
    else:
        fireball_delay -= 1

def update_fireball():
    global fireballs
    for fireball in fireballs:
        fireball.x += 8
        if fireball.x > WIDTH:
            fireballs.remove(fireball)

def update_demon():
    global demons, score
    for demon in demons:
        demon.move_in_direction(8)
        if demon.x < 0: # off the left edge
            demons.remove(demon)
        if demon.y < 0 or demon.y > HEIGHT: # bounce of top or bottom
            demon.direction *= -1
        d_hit = demon.circle_collidepoints(50, fireballs)
        if d_hit != -1:
            score += 10
            fireballs.pop(d_hit)
            demons.remove(demon)

def update_good_babies():
    global good_babies, score
    for baby in good_babies:
        baby.move_in_direction(5)
        if baby.x < 0:
            good_babies.remove(baby)
        if baby.y < 0 or baby.y > HEIGHT:
            baby.direction *= -1
        b_hit = baby.colliderect(dragon)
        if b_hit != -1:
            good_babies.remove(baby)
            score += 50

def update_score():
    global score
    screen.draw.text("Score: " + str(score), (15, 10), color=(255,255,255), fontsize=30)

######################################

def draw():
    global fireballs, demons, good_babies
    screen.clear()
    screen.blit(BACKGROUND_IMG, (0,0))
    
    boss.draw()
    dragon.draw()
    
    for fireball in fireballs:
        fireball.draw()
        
    for demon in demons:
        demon.draw()
        
    for baby in good_babies:
        baby.draw()
        
    update_score()

def update():
    update_boss()
    update_dragon()
    update_fireball()
    update_demon()
    update_good_babies()



pgzrun.go()
