import pygame, sys
from pygame.locals import *
import random
 
pygame.init()

SCREEN_WIDTH = 400 
FPS = 60
FramePerSec = pygame.time.Clock()
 
BLUE  = (0, 0, 255)
RED   = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
 
DISPLAYSURF = pygame.display.set_mode((600,600))
DISPLAYSURF.fill(WHITE)
pygame.display.set_caption("Game")
 
class Pumpkin(pygame.sprite.Sprite):
      def __init__(self, x, y):
        super().__init__() 
        self.image = pygame.image.load("pumpkin.png")
        self.rect = self.image.get_rect()
        self.rect.center=(x,y) 
        self.liv = 1
        self.exploding = 0
        self.explosion_frames_nr = 0
        self.nuclear_button = 0
 
      def move(self, player):
        self.rect.move_ip(0,10)
        if (self.rect.bottom > 600):
            self.rect.top = 0
            self.rect.center = (random.randint(30, 370), 0)

        if self.rect.colliderect(player.rect):
                print("HIT")

      def draw(self, surface):
        surface.blit(self.image, self.rect) 
 
 
class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__() 
        self.image = pygame.image.load("ghost.png")
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
 
    def update(self):
        pressed_keys = pygame.key.get_pressed()
        if pressed_keys[K_UP]:
             self.rect.move_ip(0, -5)
        if pressed_keys[K_DOWN]:
             self.rect.move_ip(0,5)
         
        if self.rect.left > 0:
              if pressed_keys[K_LEFT]:
                  self.rect.move_ip(-5, 0)
        if self.rect.right < SCREEN_WIDTH:        
              if pressed_keys[K_RIGHT]:
                  self.rect.move_ip(5, 0)
 
    def draw(self, surface):
        surface.blit(self.image, self.rect)     
    
    def get_pos(self):
        return self.rect.pos

         
P1 = Player(185, 290)


list_pumpkins = [Pumpkin(185, 290), Pumpkin(430, 290), Pumpkin(310, 260), Pumpkin(310, 190), Pumpkin(185, 370), Pumpkin(450, 370)]

clicks = 0

mus_nede = 0
                   
pygame.font.init()  
my_font = pygame.font.SysFont('Comic Sans MS', 30)
text_surface = my_font.render('Clicks: ' + str(clicks), False, (255, 255, 255))


bg = pygame.image.load("bg.png")
nuclear_button = pygame.image.load("nuclear-bomb.png")
nuclear_seconds = 0
countdown = 10

explosion_frames = []
for i in range(64):
    explosion_frames.append(pygame.image.load("explosion/exp_frame_"+str(i)+".png"))
explosion_frames_nr = 0
explosion_on = 0

ticks_seconds = 0

while True:     
    if random.randint(1, 50) == 25: 
        for pumpkin in list_pumpkins:
            pumpkin.liv = 1    
    for event in pygame.event.get():              
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    P1.update()
    #E1.move(P1)

    #DISPLAYSURF.fill(WHITE)
    DISPLAYSURF.blit(bg, (0, 0))

    if nuclear_seconds > 0:
        DISPLAYSURF.blit(nuclear_button, (16, 32))


    #P1.draw(DISPLAYSURF)
    for pumpkin in list_pumpkins:
        if pumpkin.liv == 1:
            pumpkin.draw(DISPLAYSURF)

        if pumpkin.exploding == 1:
            DISPLAYSURF.blit(explosion_frames[pumpkin.explosion_frames_nr], (pumpkin.rect.x-96,pumpkin.rect.y-96))
            pumpkin.explosion_frames_nr = (pumpkin.explosion_frames_nr +1)
            if pumpkin.explosion_frames_nr == 64:
                pumpkin.exploding = 0
                pumpkin.explosion_frames_nr = 0
    
    DISPLAYSURF.blit(text_surface, (0,0))

    

    if event.type == MOUSEBUTTONDOWN:
        mouse_pos = event.pos # Now it will have the coordinates of click point.
        print('Click')
        for pumpkin in list_pumpkins:
            if pumpkin.rect.collidepoint(mouse_pos):

                if pumpkin.liv == 1:
                    pumpkin.liv = 0
                    if pumpkin.nuclear_button == 1:
                        nuclear_seconds = 5
                        pumpkin.nuclear_button = 0
                        list_pumpkins[random_pumpkin-1].image = pygame.image.load("pumpkin.png")

                    if mus_nede == 0:
                        mus_nede = 1
                        print('Touched')
                        clicks = clicks + 1
                        countdown = countdown - 1

                        if countdown == 0:
                            random_pumpkin = random.randint(1, len(list_pumpkins))
                            list_pumpkins[random_pumpkin-1].nuclear_button = 1
                            list_pumpkins[random_pumpkin-1].image = pygame.image.load("nuclear-bomb.png")

                        if nuclear_seconds > 0:
                            pumpkin.exploding = 1
                            clicks = clicks + 79

                        text_surface = my_font.render('Clicks: ' + str(clicks), False, (255, 255, 255))
    elif event.type == MOUSEBUTTONUP:
        mus_nede = 0
    
    ticks_seconds = ticks_seconds + 1
    
    if (ticks_seconds % 60) == 0:
        if nuclear_seconds > 0:
            nuclear_seconds = nuclear_seconds - 1  
            countdown = 10
    
    pygame.display.update()
    FramePerSec.tick(FPS)