
import pygame
import random
import time 

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
 
# --- Classes
 
 
class Block(pygame.sprite.Sprite):
    """ This class represents the block. """
    def __init__(self, color):
        # Call the parent class (Sprite) constructor
        super().__init__()
 
        #self.image = pygame.Surface([10, 10])
        #self.image.fill(BLUE)
        self.image = pygame.image.load("maali.png").convert()
        self.image.set_colorkey(BLACK)
 
        self.rect = self.image.get_rect()
 
    def update(self):
        """ Update the player's position. """
        # Get the current mouse position. This returns the position
        # as a list of two numbers.
        
 
        # Set the player x position to the mouse x position
        self.rect.y += +1
        
       
 
class Player(pygame.sprite.Sprite):
    """ This class represents the Player. """
 
    def __init__(self):
        """ Set up the player on creation. """
        # Call the parent class (Sprite) constructor
        super().__init__()
 
        self.image = pygame.Surface([64, 64])
        self.image = pygame.image.load("alus.png").convert()
        self.image.set_colorkey(BLACK)
        self.rect = self.image.get_rect()
 
    def update(self):
        """ Update the player's position. """
        # Get the current mouse position. This returns the position
        # as a list of two numbers.
        pos = pygame.mouse.get_pos()
 
        # Set the player x position to the mouse x position
        self.rect.x = pos[0]
 
 
class Bullet(pygame.sprite.Sprite):
    """ This class represents the bullet . """
    def __init__(self):
        # Call the parent class (Sprite) constructor
        super().__init__()
 
        self.image = pygame.Surface([4, 10])
        self.image.fill(RED)
 
        self.rect = self.image.get_rect()
 
    def update(self):
        """ Move the bullet. """
        self.rect.y -= 3
 
 
# --- Create the window
 
# Initialize Pygame
pygame.init()
 
# Set the height and width of the screen
screen_width = 800
screen_height = 600
score = 0
screen = pygame.display.set_mode([screen_width, screen_height])
taustakuva = pygame.image.load("tausta.png").convert()
aloituskuva = pygame.image.load("aloitus.png").convert()
pygame.display.set_caption("Pelin nimi")
laser_aani = pygame.mixer.Sound("laser.ogg")
pygame.mixer.music.load("tausta.ogg")
pygame.mixer.music.set_endevent(pygame.constants.USEREVENT)
pygame.mixer.music.play()
pisteetFontti = pygame.font.SysFont("Arial",30, True, False)
pisteetTeksti = pisteetFontti.render("Pisteet:"+str(score), True, WHITE, RED)
# --- Sprite lists
 
# This is a list of every sprite. All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()
 
# List of each block in the game
block_list = pygame.sprite.Group()
 
# List of each bullet
bullet_list = pygame.sprite.Group()
 
# --- Create the sprites
 

def uusi():
    for i in range(random.randint(1,2)):
        # This represents a block
        block = Block(BLUE)
     
        # Set a random location for the block
        block.rect.x = random.randint(0,screen_width)
        block.rect.y = 0
     
        # Add the block to the list of objects
        block_list.add(block)
        all_sprites_list.add(block)
 
# Create a red player block
player = Player()
all_sprites_list.add(player)
 
# Loop until the user clicks the close button.
done = False
 
# Used to manage how fast the screen updates
clock = pygame.time.Clock()


screen.blit(aloituskuva,[0,0])
pygame.display.flip()
time.sleep(5)

player.rect.y = screen_height - 64
 
# -------- Main Program Loop -----------
while not done:
    # --- Event Processing
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
 
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # Fire a bullet if the user clicks the mouse button
            bullet = Bullet()
            
            laser_aani.play()
            # Set the bullet so it is where the player is
            bullet.rect.x = player.rect.x + 32
            bullet.rect.y = player.rect.y
            # Add the bullet to the lists
            all_sprites_list.add(bullet)
            bullet_list.add(bullet)
            
        elif event.type == pygame.constants.USEREVENT:
            pygame.mixer.music.play()
        
        elif score < 0:
            pisteetTeksti = pisteetFontti.render("GAME OVAR", True, WHITE, RED)   
 
    # --- Game logic
 
    # Call the update() method on all the sprites
    all_sprites_list.update()
 
    # Calculate mechanics for each bullet
    for bullet in bullet_list:
 
        # See if it hit a block
        block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
 
        # For each block hit, remove the bullet and add to the score
        for block in block_hit_list:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)
            score += 1
            pisteetTeksti = pisteetFontti.render("Pisteet:"+str(score), True, WHITE, RED)   
 
        # Remove the bullet if it flies up off the screen
        if bullet.rect.y < -10:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)
            score-=1
            pisteetTeksti = pisteetFontti.render("Pisteet:"+str(score), True, WHITE, RED)   
            
    for block in block_list:
        
        if block.rect.y > screen_height:
            block_list.remove(block)
            all_sprites_list.remove(block)
            score-=1
            pisteetTeksti = pisteetFontti.render("Pisteet:"+str(score), True, WHITE, RED)   
    # --- Draw a frame
    uusiko = random.randint(1,50)
    if uusiko == 3:    
        uusi()
    # Clear the screen
    screen.blit(taustakuva,[0,0])
    
    # Draw all the spites
    all_sprites_list.draw(screen)
    screen.blit(pisteetTeksti,[0,0])
 
    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
 
    # --- Limit to 20 frames per second
    clock.tick(60)
 
pygame.quit()