import pygame
from random import randint
pygame.init()
puntos=0
ventana = pygame.display.set_mode((640,480))
pygame.display.set_caption("Exemplo 4")
ball = pygame.image.load("ball.png")
ballrect = ball.get_rect()
toque= pygame.mixer.Sound("toque.mp3")
derrota=pygame.mixer.Sound("derrota.mp3")
# a velocidade cambia cun valor aleatorio entre 3 e 6
speed = [4,4]
ballrect.move_ip(0,0)
bate = pygame.image.load("bate.png")
baterect = bate.get_rect()
baterect.move_ip(240,460)
# Esta é a fonte que usaremos para o texto que aparecerá en pantalla (tamaño 36)
fuente = pygame.font.Font(None, 36)
img_ladrillo = pygame.image.load("ladrillo.png")

ladrillos = [] # Aquí gardaremos todos os ladrillos
# Creamos 3 filas de 8 ladrillos
ladrillos = []
img_ladrillo = pygame.image.load("ladrillo.png")

# Bucle para as FILAS (0 e 1)
for fila in range(3): 
    # Bucle para as COLUMNAS (0 ao 7)
    for i in range(8):
        # a X calculase igual que antes
        pos_x = i * 75 + 20
        # a Y ahora depende da fila:
        # Se a fila é 0: 0 * 40 + 50 = 50
        # Se a fila é 1: 1 * 40 + 50 = 90
        # Se a fila é 2: 2*100 +50 =250
        pos_y = fila * 150 + 100 
        
        nuevo_ladrillo = img_ladrillo.get_rect(topleft=(pos_x, pos_y))
        ladrillos.append(nuevo_ladrillo)
# Bucle principal
jugando = True
while jugando:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            jugando = False
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        baterect = baterect.move(-6,0)
    if keys[pygame.K_RIGHT]:
        baterect = baterect.move(6,0)
    ballrect = ballrect.move(speed)    
    if ballrect.left < 0 or ballrect.right > ventana.get_width():
        speed[0] = -speed[0]
    if ballrect.top < 0: 
        speed[1] = -speed[1]
    # Se a pelota toca o borde inferior, perdiches ("Game Over")
    if ballrect.bottom > ventana.get_height():
        texto = fuente.render("Game Over", True, (125,125,125))
        texto_rect = texto.get_rect()
        texto_x = ventana.get_width() / 2 - texto_rect.width / 2
        texto_y = ventana.get_height() / 2 - texto_rect.height / 2
        ventana.blit(texto, [texto_x, texto_y])
        derrota.play()
    
            
    else:
        ventana.fill((252, 243, 207))
        if baterect.colliderect(ballrect):
            speed[0]=speed[0]*1.1
            speed[1] = -speed[1]*1.1
            puntos+=1
            toque.play()
        
        for l in ladrillos:
            if ballrect.colliderect(l):
                speed[1] = -speed[1]
                toque.play()# Rebota arriba/abaixo
                ladrillos.remove(l)  # Borrámolo da lista e desaparece!
                break# Saímos do bucle para non chocar con dous á vez
        
        #Debuxo o marcador
        texto_marcador=fuente.render(f"puntos:{puntos}",True,(255,0,0))
        ventana.blit(texto_marcador,(10,10))
        #Debuxo a pelota
        ventana.blit(ball, ballrect)
        # Debuxo o bate
        ventana.blit(bate, baterect)
        # Debuxa cada ladrillo que ainda está na lista
        for l in ladrillos:
            ventana.blit(img_ladrillo, l)

    pygame.display.flip()
    pygame.time.Clock().tick(60)

pygame.quit()
