bacterialblitz.py
import pygame
import random

# Inicializar Pygame
pygame.init()

# Configuración de la pantalla
ANCHO, ALTO = 880, 600  # +80 para 2 columnas vacías
TAMANO_CELDA = 40
COLUMNAS, FILAS = ANCHO // TAMANO_CELDA, ALTO // TAMANO_CELDA  # 22 columnas
pantalla = pygame.display.set_mode((ANCHO, ALTO))
reloj = pygame.time.Clock()

# Colores
COLORES_BACTERIAS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (128, 0, 128)]
COLOR_UNIDAD = (255, 255, 0)
COLOR_PILDORA = (255, 255, 255)
COLOR_PARED_ROJO = (200, 0, 0)
COLOR_PARED_CARNE = (255, 182, 193)
COLOR_FONDO = (50, 50, 50)
COLOR_TEXTO = (255, 255, 255)

# Fuentes
fuente = pygame.font.SysFont('arial', 24)
fuente_game_over = pygame.font.SysFont('arial', 48)

# Clase para una bacteria
class Bacteria:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color

# Clase base para grupos de bacterias
class GrupoBacterias:
    def __init__(self, x, y):
        self.x, self.y = x, y
        self.color = random.choice(COLORES_BACTERIAS)
        self.orientacion = 0
        self.bacterias = []
        self.update_bacterias()

    def update_bacterias(self):
        pass

    def mover_arriba(self, bacterias_fijas):
        nuevo_y = self.y - 1
        if min(b.y for b in self.bacterias) > 1:
            temp_bacterias = self._get_bacterias_posicion(self.x, nuevo_y, self.orientacion)
            colision = False
            for b in temp_bacterias:
                if b.y <= 0:
                    colision = True
                    break
                for fija in bacterias_fijas:
                    if b.x == fija.x and b.y == fija.y:
                        colision = True
                        break
            if not colision:
                self.y = nuevo_y
                self.update_bacterias()

    def mover_abajo(self, bacterias_fijas):
        nuevo_y = self.y + 1
        if max(b.y for b in self.bacterias) < FILAS - 2:
            temp_bacterias = self._get_bacterias_posicion(self.x, nuevo_y, self.orientacion)
            colision = False
            for b in temp_bacterias:
                if b.y >= FILAS - 1:
                    colision = True
                    break
                for fija in bacterias_fijas:
                    if b.x == fija.x and b.y == fija.y:
                        colision = True
                        break
            if not colision:
                self.y = nuevo_y
                self.update_bacterias()

    def mover_derecha(self, bacterias_fijas):
        max_x = self.x
        while True:
            temp_bacterias = self._get_bacterias_posicion(max_x + 1, self.y, self.orientacion)
            colision = False
            for b in temp_bacterias:
                if b.x >= COLUMNAS:
                    colision = True
                    break
                for fija in bacterias_fijas:
                    if b.x == fija.x and b.y == fija.y:
                        colision = True
                        break
            if colision:
                break
            max_x += 1
        if max_x > self.x:
            self.x = max_x
            self.update_bacterias()
        return max_x > self.x

    def mover_izquierda_auto(self, bacterias_fijas):
        nuevo_x = self.x + 1
        if nuevo_x >= COLUMNAS:
            return False
        temp_bacterias = self._get_bacterias_posicion(nuevo_x, self.y, self.orientacion)
        colision = False
        for b in temp_bacterias:
            if b.x >= COLUMNAS or b.y <= 0 or b.y >= FILAS - 1:
                colision = True
                break
            for fija in bacterias_fijas:
                if b.x == fija.x and b.y == fija.y:
                    colision = True
                    break
        if not colision:
            self.x = nuevo_x
            self.update_bacterias()
        return not colision

    def rotar(self, bacterias_fijas):
        vieja_orientacion = self.orientacion
        self.orientacion = (self.orientacion + 1) % 4
        self.update_bacterias()
        for b in self.bacterias:
            if b.x < 0 or b.x >= COLUMNAS or b.y <= 0 or b.y >= FILAS - 1:
                self.orientacion = vieja_orientacion
                self.update_bacterias()
                return
            for fija in bacterias_fijas:
                if b.x == fija.x and b.y == fija.y:
                    self.orientacion = vieja_orientacion
                    self.update_bacterias()
                    return

    def _get_bacterias_posicion(self, x, y, orientacion):
        pass

# Forma: Un solo cuadrado (amarillo, cae una celda)
class Unidad(GrupoBacterias):
    def __init__(self, x, y):
        super().__init__(x, y)
        self.color = COLOR_UNIDAD

    def update_bacterias(self):
        self.bacterias = [Bacteria(self.x, self.y, self.color)]

    def _get_bacterias_posicion(self, x, y, orientacion):
        return [Bacteria(x, y, self.color)]

    def mover_izquierda_auto(self, bacterias_fijas):
        nuevo_x = self.x + 1
        if nuevo_x >= COLUMNAS:
            return False
        nuevo_y = self.y
        if nuevo_y < FILAS - 2:
            colision = False
            for fija in bacterias_fijas:
                if fija.x == nuevo_x and fija.y == nuevo_y + 1:
                    colision = True
                    break
            if not colision:
                nuevo_y += 1
        self.x = nuevo_x
        self.y = nuevo_y
        self.update_bacterias()
        return True

# Forma: Par (2 bloques en línea)
class Par(GrupoBacterias):
    def update_bacterias(self):
        if self.orientacion in [0, 2]:
            self.bacterias = [
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x + 1, self.y, self.color)
            ]
        elif self.orientacion in [1, 3]:
            self.bacterias = [
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color)
            ]

    def _get_bacterias_posicion(self, x, y, orientacion):
        if orientacion in [0, 2]:
            return [Bacteria(x, y, self.color), Bacteria(x + 1, y, self.color)]
        elif orientacion in [1, 3]:
            return [Bacteria(x, y, self.color), Bacteria(x, y + 1, self.color)]

# Forma: Píldora (2 bloques blancos, elimina a la derecha)
class Pildora(GrupoBacterias):
    def __init__(self, x, y):
        super().__init__(x, y)
        self.color = COLOR_PILDORA

    def update_bacterias(self):
        if self.orientacion in [0, 2]:
            self.bacterias = [
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x + 1, self.y, self.color)
            ]
        elif self.orientacion in [1, 3]:
            self.bacterias = [
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color)
            ]

    def _get_bacterias_posicion(self, x, y, orientacion):
        if orientacion in [0, 2]:
            return [Bacteria(x, y, self.color), Bacteria(x + 1, y, self.color)]
        elif orientacion in [1, 3]:
            return [Bacteria(x, y, self.color), Bacteria(x, y + 1, self.color)]

    def mover_izquierda_auto(self, bacterias_fijas):
        nuevo_x = self.x + 1
        if nuevo_x >= COLUMNAS - 1:  # Fijarse al llegar al final
            return False
        temp_bacterias = self._get_bacterias_posicion(nuevo_x, self.y, self.orientacion)
        colision = False
        for b in temp_bacterias:
            if b.x >= COLUMNAS:
                colision = True
                break
            for fija in bacterias_fijas:
                if b.x == fija.x and b.y == fija.y:
                    colision = True
                    break
        if colision:
            if self.orientacion in [0, 2]:
                bacterias_fijas[:] = [b for b in bacterias_fijas if b.y != self.y or b.x < self.x]
            else:
                bacterias_fijas[:] = [b for b in bacterias_fijas if b.y not in [self.y, self.y + 1] or b.x < self.x]
            return False
        self.x = nuevo_x
        self.update_bacterias()
        return True

# Forma: Línea de 3 bacterias
class Linea(GrupoBacterias):
    def update_bacterias(self):
        if self.orientacion == 0:
            self.bacterias = [
                Bacteria(self.x, self.y - 1, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color)
            ]
        elif self.orientacion == 1:
            self.bacterias = [
                Bacteria(self.x - 1, self.y, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x + 1, self.y, self.color)
            ]
        elif self.orientacion == 2:
            self.bacterias = [
                Bacteria(self.x, self.y + 1, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y - 1, self.color)
            ]
        elif self.orientacion == 3:
            self.bacterias = [
                Bacteria(self.x + 1, self.y, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x - 1, self.y, self.color)
            ]

    def _get_bacterias_posicion(self, x, y, orientacion):
        if orientacion == 0:
            return [Bacteria(x, y - 1, self.color), Bacteria(x, y, self.color), Bacteria(x, y + 1, self.color)]
        elif orientacion == 1:
            return [Bacteria(x - 1, y, self.color), Bacteria(x, y, self.color), Bacteria(x + 1, y, self.color)]
        elif orientacion == 2:
            return [Bacteria(x, y + 1, self.color), Bacteria(x, y, self.color), Bacteria(x, y - 1, self.color)]
        elif orientacion == 3:
            return [Bacteria(x + 1, y, self.color), Bacteria(x, y, self.color), Bacteria(x - 1, y, self.color)]

# Forma: L (3 bloques en línea, 1 al lado)
class FormaL(GrupoBacterias):
    def update_bacterias(self):
        if self.orientacion == 0:
            self.bacterias = [
                Bacteria(self.x, self.y - 1, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x + 1, self.y, self.color)
            ]
        elif self.orientacion == 1:
            self.bacterias = [
                Bacteria(self.x - 1, self.y, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color)
            ]
        elif self.orientacion == 2:
            self.bacterias = [
                Bacteria(self.x - 1, self.y, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y - 1, self.color)
            ]
        elif self.orientacion == 3:
            self.bacterias = [
                Bacteria(self.x, self.y - 1, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x + 1, self.y - 1, self.color)
            ]

    def _get_bacterias_posicion(self, x, y, orientacion):
        if orientacion == 0:
            return [Bacteria(x, y - 1, self.color), Bacteria(x, y, self.color), Bacteria(x + 1, y, self.color)]
        elif orientacion == 1:
            return [Bacteria(x - 1, y, self.color), Bacteria(x, y, self.color), Bacteria(x, y + 1, self.color)]
        elif orientacion == 2:
            return [Bacteria(x - 1, y, self.color), Bacteria(x, y, self.color), Bacteria(x, y - 1, self.color)]
        elif orientacion == 3:
            return [Bacteria(x, y - 1, self.color), Bacteria(x, y, self.color), Bacteria(x + 1, y - 1, self.color)]

# Forma: J (espejo de L)
class FormaJ(GrupoBacterias):
    def update_bacterias(self):
        if self.orientacion == 0:
            self.bacterias = [
                Bacteria(self.x, self.y - 1, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x - 1, self.y, self.color)
            ]
        elif self.orientacion == 1:
            self.bacterias = [
                Bacteria(self.x + 1, self.y, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color)
            ]
        elif self.orientacion == 2:
            self.bacterias = [
                Bacteria(self.x + 1, self.y, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y - 1, self.color)
            ]
        elif self.orientacion == 3:
            self.bacterias = [
                Bacteria(self.x, self.y - 1, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x - 1, self.y - 1, self.color)
            ]

    def _get_bacterias_posicion(self, x, y, orientacion):
        if orientacion == 0:
            return [Bacteria(x, y - 1, self.color), Bacteria(x, y, self.color), Bacteria(x - 1, y, self.color)]
        elif orientacion == 1:
            return [Bacteria(x + 1, y, self.color), Bacteria(x, y, self.color), Bacteria(x, y + 1, self.color)]
        elif orientacion == 2:
            return [Bacteria(x + 1, y, self.color), Bacteria(x, y, self.color), Bacteria(x, y - 1, self.color)]
        elif orientacion == 3:
            return [Bacteria(x, y - 1, self.color), Bacteria(x, y, self.color), Bacteria(x - 1, y - 1, self.color)]

# Forma: T mayúscula (5 bacterias)
class FormaT(GrupoBacterias):
    def update_bacterias(self):
        if self.orientacion == 0:
            self.bacterias = [
                Bacteria(self.x - 1, self.y, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x + 1, self.y, self.color),
                Bacteria(self.x, self.y - 1, self.color),
                Bacteria(self.x, self.y + 1, self.color)
            ]
        elif self.orientacion == 1:
            self.bacterias = [
                Bacteria(self.x, self.y - 1, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color),
                Bacteria(self.x + 1, self.y, self.color),
                Bacteria(self.x - 1, self.y, self.color)
            ]
        elif self.orientacion == 2:
            self.bacterias = [
                Bacteria(self.x - 1, self.y, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x + 1, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color),
                Bacteria(self.x, self.y - 1, self.color)
            ]
        elif self.orientacion == 3:
            self.bacterias = [
                Bacteria(self.x, self.y - 1, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color),
                Bacteria(self.x - 1, self.y, self.color),
                Bacteria(self.x + 1, self.y, self.color)
            ]

    def _get_bacterias_posicion(self, x, y, orientacion):
        if orientacion == 0:
            return [
                Bacteria(x - 1, y, self.color),
                Bacteria(x, y, self.color),
                Bacteria(x + 1, y, self.color),
                Bacteria(x, y - 1, self.color),
                Bacteria(x, y + 1, self.color)
            ]
        elif orientacion == 1:
            return [
                Bacteria(x, y - 1, self.color),
                Bacteria(x, y, self.color),
                Bacteria(x, y + 1, self.color),
                Bacteria(x + 1, y, self.color),
                Bacteria(x - 1, y, self.color)
            ]
        elif orientacion == 2:
            return [
                Bacteria(x - 1, y, self.color),
                Bacteria(x, y, self.color),
                Bacteria(x + 1, y, self.color),
                Bacteria(x, y + 1, self.color),
                Bacteria(x, y - 1, self.color)
            ]
        elif orientacion == 3:
            return [
                Bacteria(x, y - 1, self.color),
                Bacteria(x, y, self.color),
                Bacteria(x, y + 1, self.color),
                Bacteria(x - 1, y, self.color),
                Bacteria(x + 1, y, self.color)
            ]

# Forma: Cuadrado (2x2)
class Cuadrado(GrupoBacterias):
    def update_bacterias(self):
        self.bacterias = [
            Bacteria(self.x, self.y, self.color),
            Bacteria(self.x + 1, self.y, self.color),
            Bacteria(self.x, self.y + 1, self.color),
            Bacteria(self.x + 1, self.y + 1, self.color)
        ]

    def _get_bacterias_posicion(self, x, y, orientacion):
        return [
            Bacteria(x, y, self.color),
            Bacteria(x + 1, y, self.color),
            Bacteria(x, y + 1, self.color),
            Bacteria(x + 1, y + 1, self.color)
        ]

# Forma: T invertida
class TInvertida(GrupoBacterias):
    def update_bacterias(self):
        if self.orientacion == 0:
            self.bacterias = [
                Bacteria(self.x - 1, self.y, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x + 1, self.y, self.color),
                Bacteria(self.x, self.y - 1, self.color)
            ]
        elif self.orientacion == 1:
            self.bacterias = [
                Bacteria(self.x, self.y - 1, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color),
                Bacteria(self.x + 1, self.y, self.color)
            ]
        elif self.orientacion == 2:
            self.bacterias = [
                Bacteria(self.x - 1, self.y, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x + 1, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color)
            ]
        elif self.orientacion == 3:
            self.bacterias = [
                Bacteria(self.x, self.y - 1, self.color),
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color),
                Bacteria(self.x - 1, self.y, self.color)
            ]

    def _get_bacterias_posicion(self, x, y, orientacion):
        if orientacion == 0:
            return [Bacteria(x - 1, y, self.color), Bacteria(x, y, self.color), Bacteria(x + 1, y, self.color), Bacteria(x, y - 1, self.color)]
        elif orientacion == 1:
            return [Bacteria(x, y - 1, self.color), Bacteria(x, y, self.color), Bacteria(x, y + 1, self.color), Bacteria(x + 1, y, self.color)]
        elif orientacion == 2:
            return [Bacteria(x - 1, y, self.color), Bacteria(x, y, self.color), Bacteria(x + 1, y, self.color), Bacteria(x, y + 1, self.color)]
        elif orientacion == 3:
            return [Bacteria(x, y - 1, self.color), Bacteria(x, y, self.color), Bacteria(x, y + 1, self.color), Bacteria(x - 1, y, self.color)]

# Forma: Z
class FormaZ(GrupoBacterias):
    def update_bacterias(self):
        if self.orientacion == 0:
            self.bacterias = [
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x + 1, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color),
                Bacteria(self.x - 1, self.y + 1, self.color)
            ]
        elif self.orientacion == 1:
            self.bacterias = [
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y - 1, self.color),
                Bacteria(self.x + 1, self.y, self.color),
                Bacteria(self.x + 1, self.y + 1, self.color)
            ]
        elif self.orientacion == 2:
            self.bacterias = [
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x - 1, self.y, self.color),
                Bacteria(self.x, self.y - 1, self.color),
                Bacteria(self.x + 1, self.y - 1, self.color)
            ]
        elif self.orientacion == 3:
            self.bacterias = [
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color),
                Bacteria(self.x - 1, self.y, self.color),
                Bacteria(self.x - 1, self.y - 1, self.color)
            ]

    def _get_bacterias_posicion(self, x, y, orientacion):
        if orientacion == 0:
            return [
                Bacteria(x, y, self.color),
                Bacteria(x + 1, y, self.color),
                Bacteria(x, y + 1, self.color),
                Bacteria(x - 1, y + 1, self.color)
            ]
        elif orientacion == 1:
            return [
                Bacteria(x, y, self.color),
                Bacteria(x, y - 1, self.color),
                Bacteria(x + 1, y, self.color),
                Bacteria(x + 1, y + 1, self.color)
            ]
        elif orientacion == 2:
            return [
                Bacteria(x, y, self.color),
                Bacteria(x - 1, y, self.color),
                Bacteria(x, y - 1, self.color),
                Bacteria(x + 1, y - 1, self.color)
            ]
        elif orientacion == 3:
            return [
                Bacteria(x, y, self.color),
                Bacteria(x, y + 1, self.color),
                Bacteria(x - 1, y, self.color),
                Bacteria(x - 1, y - 1, self.color)
            ]

# Forma: S
class FormaS(GrupoBacterias):
    def update_bacterias(self):
        if self.orientacion == 0:
            self.bacterias = [
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x - 1, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color),
                Bacteria(self.x + 1, self.y + 1, self.color)
            ]
        elif self.orientacion == 1:
            self.bacterias = [
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y + 1, self.color),
                Bacteria(self.x + 1, self.y, self.color),
                Bacteria(self.x + 1, self.y - 1, self.color)
            ]
        elif self.orientacion == 2:
            self.bacterias = [
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x + 1, self.y, self.color),
                Bacteria(self.x, self.y - 1, self.color),
                Bacteria(self.x - 1, self.y - 1, self.color)
            ]
        elif self.orientacion == 3:
            self.bacterias = [
                Bacteria(self.x, self.y, self.color),
                Bacteria(self.x, self.y - 1, self.color),
                Bacteria(self.x - 1, self.y, self.color),
                Bacteria(self.x - 1, self.y + 1, self.color)
            ]

    def _get_bacterias_posicion(self, x, y, orientacion):
        if orientacion == 0:
            return [
                Bacteria(x, y, self.color),
                Bacteria(x - 1, y, self.color),
                Bacteria(x, y + 1, self.color),
                Bacteria(x + 1, y + 1, self.color)
            ]
        elif orientacion == 1:
            return [
                Bacteria(x, y, self.color),
                Bacteria(x, y + 1, self.color),
                Bacteria(x + 1, y, self.color),
                Bacteria(x + 1, y - 1, self.color)
            ]
        elif orientacion == 2:
            return [
                Bacteria(x, y, self.color),
                Bacteria(x + 1, y, self.color),
                Bacteria(x, y - 1, self.color),
                Bacteria(x - 1, y - 1, self.color)
            ]
        elif orientacion == 3:
            return [
                Bacteria(x, y, self.color),
                Bacteria(x, y - 1, self.color),
                Bacteria(x - 1, y, self.color),
                Bacteria(x - 1, y + 1, self.color)
            ]

# Lista para almacenar bacterias fijas
bacterias_fijas = []
grupo_activo = None
ultimo_movimiento = pygame.time.get_ticks()
VELOCIDAD_BASE = 600  # Más rápido
VELOCIDAD_MINIMA = 200
VELOCIDAD_INCREMENTO = 400  # Más agresivo
TIEMPO_ESPERA_INICIAL = 50  # Más rápido
FORMAS = [Unidad, Par, Pildora, Linea, FormaL, FormaJ, FormaT, Cuadrado, TInvertida, FormaZ, FormaS]
FORMAS_PONDERADAS = FORMAS * 5 + [FormaT] * 2 + [Pildora] * 1  # FormaT ~10%, Píldora ~5%
puntuacion = 0
ultima_presion_derecha = 0
contador_presion_derecha = 0
DOBLE_PRESION_INTERVALO = 300
izquierda_inicio = None
IZQUIERDA_MANTENER = 2000
tiempo_derecha_presionada = 0
derecha_inicio = None
game_over = False
game_over_time = 0
GAME_OVER_DELAY = 2000  # 2 segundos

# Verificar espacio vacío para Unidad
def espacio_vacio_unidad(x, y, bacterias_fijas):
    if x < 0 or x >= COLUMNAS or y <= 0 or y >= FILAS - 1:
        return False
    for fija in bacterias_fijas:
        if fija.x == x and fija.y == y:
            return False
    vecinos = 0
    for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        nx, ny = x + dx, y + dy
        if nx < 0 or nx >= COLUMNAS or ny <= 0 or ny >= FILAS - 1:
            vecinos += 1
            continue
        for fija in bacterias_fijas:
            if fija.x == nx and fija.y == ny:
                vecinos += 1
                break
    return vecinos >= 3

# Mover bacterias sostenidas
def mover_derecha_sostenidas(bacterias_fijas, x_eliminada):
    for b in bacterias_fijas:
        if b.x < x_eliminada:
            b.x += 1

# Eliminar columnas completas
def eliminar_columnas_completas(bacterias_fijas):
    global puntuacion
    max_x = max((b.x for b in bacterias_fijas), default=-1)
    if max_x < 0:
        return False
    lineas_eliminadas = 0
    bloques_por_linea = FILAS - 2
    for x in range(max_x, -1, -1):
        bacterias_en_columna = [b for b in bacterias_fijas if b.x == x]
        if len(bacterias_en_columna) == bloques_por_linea and all(1 <= b.y <= FILAS - 2 for b in bacterias_en_columna):
            bacterias_fijas[:] = [b for b in bacterias_fijas if b.x != x]
            lineas_eliminadas += 1
            mover_derecha_sostenidas(bacterias_fijas, x)
    if lineas_eliminadas > 0:
        puntos_por_bloque = 3 ** lineas_eliminadas
        puntuacion += puntos_por_bloque * bloques_por_linea * lineas_eliminadas
        return True
    return False

# Bucle principal
corriendo = True
while corriendo:
    tiempo_actual = pygame.time.get_ticks()

    if game_over:
        if tiempo_actual - game_over_time > GAME_OVER_DELAY:
            corriendo = False
        for evento in pygame.event.get():
            if evento.type == pygame.QUIT:
                corriendo = False
    else:
        # Generar nuevo grupo
        if grupo_activo is None:
            x_inicial = 4  # Más a la derecha por barra izquierda
            y_inicial = random.randint(3, FILAS - 4)
            forma = random.choice(FORMAS_PONDERADAS)
            if forma == Unidad:
                # Buscar posición válida para Unidad
                posiciones_validas = [
                    (x, y) for x in range(2, COLUMNAS - 2) for y in range(2, FILAS - 2)
                    if espacio_vacio_unidad(x, y, bacterias_fijas)
                ]
                if posiciones_validas:
                    x_inicial, y_inicial = random.choice(posiciones_validas)
                else:
                    forma = Par  # Fallback
            grupo_activo = forma(x_inicial, y_inicial)
            colision = False
            for b in grupo_activo.bacterias:
                if b.x < 0 or b.x >= COLUMNAS or b.y <= 0 or b.y >= FILAS - 1:
                    colision = True
                    break
                for fija in bacterias_fijas:
                    if b.x == fija.x and b.y == fija.y:
                        colision = True
                        break
            if colision:
                game_over = True
                game_over_time = tiempo_actual

        # Velocidad gradual
        velocidad_actual = VELOCIDAD_BASE
        if pygame.key.get_pressed()[pygame.K_RIGHT]:
            if derecha_inicio is None:
                derecha_inicio = tiempo_actual
            tiempo_derecha_presionada = tiempo_actual - derecha_inicio
            if tiempo_derecha_presionada >= TIEMPO_ESPERA_INICIAL:
                reduccion = ((tiempo_derecha_presionada - TIEMPO_ESPERA_INICIAL) // 1000) * VELOCIDAD_INCREMENTO
                velocidad_actual = max(VELOCIDAD_MINIMA, VELOCIDAD_BASE - reduccion)
        else:
            derecha_inicio = None
            tiempo_derecha_presionada = 0

        # Presión prolongada Izquierda
        if pygame.key.get_pressed()[pygame.K_LEFT]:
            if izquierda_inicio is None:
                izquierda_inicio = tiempo_actual
            if tiempo_actual - izquierda_inicio >= IZQUIERDA_MANTENER and grupo_activo:
                nueva_forma = None
                if isinstance(grupo_activo, FormaL):
                    nueva_forma = FormaJ(grupo_activo.x, grupo_activo.y)
                elif isinstance(grupo_activo, FormaJ):
                    nueva_forma = FormaL(grupo_activo.x, grupo_activo.y)
                elif isinstance(grupo_activo, FormaZ):
                    nueva_forma = FormaS(grupo_activo.x, grupo_activo.y)
                elif isinstance(grupo_activo, FormaS):
                    nueva_forma = FormaZ(grupo_activo.x, grupo_activo.y)
                if nueva_forma:
                    nueva_forma.color = grupo_activo.color
                    nueva_forma.orientacion = grupo_activo.orientacion
                    nueva_forma.update_bacterias()
                    colision = False
                    for b in nueva_forma.bacterias:
                        if b.x < 0 or b.x >= COLUMNAS or b.y <= 0 or b.y >= FILAS - 1:
                            colision = True
                            break
                        for fija in bacterias_fijas:
                            if b.x == fija.x and b.y == fija.y:
                                colision = True
                                break
                    if not colision:
                        grupo_activo = nueva_forma
                        izquierda_inicio = None
        else:
            izquierda_inicio = None

        # Eventos
        for evento in pygame.event.get():
            if evento.type == pygame.QUIT:
                corriendo = False
            elif evento.type == pygame.KEYDOWN and grupo_activo:
                if evento.key == pygame.K_UP:
                    grupo_activo.mover_arriba(bacterias_fijas)
                elif evento.key == pygame.K_DOWN:
                    grupo_activo.mover_abajo(bacterias_fijas)
                elif evento.key == pygame.K_RIGHT:
                    if tiempo_actual - ultima_presion_derecha < DOBLE_PRESION_INTERVALO:
                        contador_presion_derecha += 1
                        if contador_presion_derecha >= 2:
                            grupo_activo.mover_derecha(bacterias_fijas)
                            contador_presion_derecha = 0
                    else:
                        contador_presion_derecha = 1
                    ultima_presion_derecha = tiempo_actual
                elif evento.key == pygame.K_LEFT:
                    grupo_activo.rotar(bacterias_fijas)
                elif evento.key == pygame.K_SPACE:
                    bacterias_fijas.extend(grupo_activo.bacterias)
                    grupo_activo = None
                    eliminar_columnas_completas(bacterias_fijas)

        # Mover grupo activo
        if tiempo_actual - ultimo_movimiento > velocidad_actual and grupo_activo:
            if not grupo_activo.mover_izquierda_auto(bacterias_fijas):
                if isinstance(grupo_activo, Pildora):
                    if grupo_activo.x >= COLUMNAS - 2:  # Fijarse
                        bacterias_fijas.extend(grupo_activo.bacterias)
                    grupo_activo = None
                else:
                    bacterias_fijas.extend(grupo_activo.bacterias)
                    grupo_activo = None
                eliminar_columnas_completas(bacterias_fijas)
            ultimo_movimiento = tiempo_actual

    # Dibujar
    pantalla.fill(COLOR_FONDO)
    pygame.draw.rect(pantalla, COLOR_PARED_ROJO, (0, 0, ANCHO, TAMANO_CELDA))
    pygame.draw.rect(pantalla, COLOR_PARED_CARNE, (0, ALTO - TAMANO_CELDA, ANCHO, TAMANO_CELDA))
    for bacteria in bacterias_fijas + (grupo_activo.bacterias if grupo_activo else []):
        pygame.draw.rect(pantalla, bacteria.color,
                         (bacteria.x * TAMANO_CELDA, bacteria.y * TAMANO_CELDA,
                          TAMANO_CELDA - 2, TAMANO_CELDA - 2),
                         border_radius=10)
        pygame.draw.rect(pantalla, (255, 255, 255, 100),
                         (bacteria.x * TAMANO_CELDA, bacteria.y * TAMANO_CELDA,
                          TAMANO_CELDA - 2, TAMANO_CELDA - 2),
                         border_radius=10, width=2)
    texto_puntaje = fuente.render(f'Puntos: {puntuacion}', True, COLOR_TEXTO)
    pantalla.blit(texto_puntaje, (10, 10))
    if game_over:
        texto_game_over = fuente_game_over.render('Game Over', True, COLOR_TEXTO)
        pantalla.blit(texto_game_over, (ANCHO // 2 - 100, ALTO // 2 - 24))

    pygame.display.flip()
    reloj.tick(60)

pygame.quit()