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í guardaremos todos los ladrillos

# Creamos 8 ladrillos en una fila
for i in range(8):
    # Calculamos la X: cada ladrillo se mueve 70 píxeles a la derecha
    # (ajusta el 70 según el ancho de tu imagen)
    nuevo_ladrillo = img_ladrillo.get_rect(topleft=(i * 75 + 20, 300))
    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/abajo
                ladrillos.remove(l)  # ¡Lo borramos de la lista y desaparece!
                break# Salimos del bucle para no chocar con dos a la 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)
        # Dibuja cada ladrillo que aún esté en la lista
        for l in ladrillos:
            ventana.blit(img_ladrillo, l)

    pygame.display.flip()
    pygame.time.Clock().tick(60)

pygame.quit()
