#!/usr/bin/env python3

import pygame, os, random, easygui
from pygame.sprite import spritecollide, RenderClear
from sprites import *

chance = 0
chance_ammo = 0

easygui.msgbox(msg="""Zurvival!\nDies ist ein Überlebensspiel wo man Zombies töten muss, Munition sammeln und überleben muss!\n
Steuerung:\nW: Vorwärts\nS:Rückwärts\nA:Links\nD:Rechts\nR:Nachladen\nY:Screenshot\nViel Spaß!""")
answer = easygui.choicebox(msg="Wähle die Schwierigkeit!", choices=("Leicht", "Mittel", "Schwer"))
if answer == "Leicht":
    chance = 150
    chance_ammo = 25
elif answer == "Mittel":
    chance = 100
    chance_ammo = 50
elif answer == "Schwer":
    chance = 50
    chance_ammo = 75

if chance == 0 and chance_ammo == 0:
    print("Abbruch.")
    exit(0)

pygame.init()

screen = pygame.display.set_mode((640, 480))
try:
    background = pygame.image.load(os.path.join("img", "background.png"))
except:
    background = pygame.Surface((640, 480))
    background.fill((85, 185, 0))
screen.blit(background, (0, 0))

players = RenderClear()
enemys = RenderClear()
dumbs = RenderClear()
shots = RenderClear()

player = Player()
players.add(player)

for i in range(random.randint(3, 4)):
    dumbs.add(Dumb())

speed = 3
look = "right"
punched = False
timer = 0
punkte = 0
god = False

while True:
    event = pygame.event.poll()
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_w:
            player.direction[1] = -speed
            try:
                player.image = pygame.image.load(os.path.join("img", "player_up.png"))
            except:
                pass
            look = "up"
        if event.key == pygame.K_s:
            player.direction[1] = speed
            try:
                player.image = pygame.image.load(os.path.join("img", "player_down.png"))
            except:
                pass
            look = "down"
        if event.key == pygame.K_a:
            player.direction[0] = -speed
            try:
                player.image = pygame.image.load(os.path.join("img", "player_left.png"))
            except:
                pass
            look = "left"
        if event.key == pygame.K_d:
            player.direction[0] = speed
            try:
                player.image = pygame.image.load(os.path.join("img", "player_right.png"))
            except:
                pass
            look = "right"

        if event.key == pygame.K_r and player.max_ammo > 0 and player.loaded_ammo == 0:
            player.max_ammo -= 8
            player.loaded_ammo = 8
            print("Ammo: %i/%i" % (player.loaded_ammo, player.max_ammo))
        if player.loaded_ammo == 0:
            print("Nachladen!")

        if event.key == pygame.K_SPACE and player.loaded_ammo > 0:
            if look == "right":
                shot = Shot((player.rect.topleft[0] + player.image.get_width() + 1, player.rect.topleft[1] + (player.image.get_height() / 2)))
                shot.direction[0] = 5
            if look == "left":
                shot = Shot((player.rect.topleft[0] - 1, player.rect.topleft[1] + (player.image.get_height() / 2)))
                shot.direction[0] = -5
            if look == "up":
                shot = Shot((player.rect.topleft[0] + (player.image.get_width() / 2), player.rect.topleft[1] + 1))
                shot.direction[1] = -5
            if look == "down":
                shot = Shot((player.rect.topleft[0] + (player.image.get_width() / 2), player.rect.topleft[1] + player.image.get_height() + 1))
                shot.direction[1] = 5

            shots.add(shot)
            if not god:
                player.loaded_ammo -= 1

        if event.key == pygame.K_ESCAPE:
            print("Abbruch")
            exit(0)

        if event.key == pygame.K_y:
            pygame.image.save(screen, "screenshot.JPEG")
            print("Screenshot!")

        if event.key == pygame.K_g:
            if not god:
                print("Godmode on!")
                god = True
            else:
                print("Godmode off!")
                god = False

    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_w:
            player.direction[1] = 0
        if event.key == pygame.K_s:   
            player.direction[1] = 0
        if event.key == pygame.K_a:   
            player.direction[0] = 0
        if event.key == pygame.K_d:    
            player.direction[0] = 0

    if random.randint(1, chance) == 1:
        enemys.add(Enemy())

    for enemy in enemys:
        if spritecollide(enemy, shots, True):
            background.blit(pygame.image.load(os.path.join("img", "blood.png")), enemy.rect.topleft)
            enemy.kill()
            punkte += 1
        if spritecollide(enemy, players, False) and not punched and not god:
            player.life -= 1
            punched = True
            if player.life == 0:
                print("Game Over")
                easygui.msgbox(msg="Super!\nDu hast %s Punkte erreicht!" % (punkte)) 
                exit(0)
            print("Autsch! Leben: %i" % (player.life))
        if spritecollide(enemy, dumbs, False):
            try:
                enemy.rect.topleft = (enemy.old[0], enemy.old[1])
            except:
                pass

    if punched:
        timer += 1
        if timer == 25:
            punched = False
            timer = 0

    if spritecollide(player, dumbs, False):
        if random.randint(1, chance_ammo) == 1 and player.max_ammo < 24:
            player.max_ammo += 8
            print("Munition gefunden!")
            print("Ammo: %i/%i" % (player.loaded_ammo, player.max_ammo))

    if spritecollide(player, dumbs, False):
        try:
            player.rect.topleft = (player.old[0], player.old[1])
        except:
            pass

    for obj in (players, dumbs, shots, enemys):
        obj.clear(screen, background)
        if obj == enemys:
            obj.update(player.rect.topleft)
        else:
            obj.update()
        obj.draw(screen)

    pygame.display.update()
    pygame.time.delay(20)
