示例#1
0
def test_eliminar_filas_superiores():
    pieza_t = tetris.generar_pieza(tetris.T)
    juego = tetris.crear_juego(pieza_t)

    for _ in range(tetris.dimensiones(juego)[1]):
        juego = tetris.mover(juego, IZQUIERDA)

    juego, cambiar_pieza = tetris.avanzar(juego, pieza_t)
    while not cambiar_pieza and not tetris.terminado(juego):
        juego, cambiar_pieza = tetris.avanzar(juego, pieza_t)

    for _ in range(tetris.dimensiones(juego)[1]):
        juego = tetris.mover(juego, DERECHA)

    juego, cambiar_pieza = tetris.avanzar(juego, pieza_t)
    while not cambiar_pieza and not tetris.terminado(juego):
        juego, cambiar_pieza = tetris.avanzar(juego, pieza_t)

    # Se consolida la siguiente superficie:
    #
    #    T T T T T T T T T
    #      T     T     T

    # La cual al eliminar las filas debería resultar en
    #
    #
    #      T     T     T

    juego = tetris.mover(juego, IZQUIERDA)
    juego, cambiar_pieza = tetris.avanzar(juego, pieza_t)
    while not cambiar_pieza and not tetris.terminado(juego):
        juego, cambiar_pieza = tetris.avanzar(juego, pieza_t)

    juego, cambiar_pieza = tetris.avanzar(juego, pieza_t)

    ancho, alto = tetris.dimensiones(juego)
    superficies_esperadas = [(1, 17), (4, 17), (7, 17)]
    for j in range(alto):
        for i in range(ancho):

            # Superficie esperada no se encuenta en el juego
            if (i, j) in superficies_esperadas:
                if not tetris.hay_superficie(juego, i, j):
                    return False

            # Se encontro una superficie no esperada en el juego
            elif tetris.hay_superficie(juego, i, j):
                return False

    return True
def dibujar_grilla(juego):
    """
    Recibe el juego y dibuja el la grilla
    """
    ancho, alto = tetris.dimensiones(juego)
    proporcio = alto / ancho
    grilla_ancho = RES / proporcio
    columna_ancho = grilla_ancho / ancho
    panel_ancho = RES - grilla_ancho
    margen = RES / 10

    for columna in range(ancho):
        color = "#fff"
        if columna % 2 == 0:
            color = "#f4efe9"
        gamelib.draw_rectangle(
            (columna * columna_ancho),
            0,
            ((columna + 1) * columna_ancho),
            RES,
            fill=color,
            outline=color,
        )
    # Panel
    gamelib.draw_rectangle(
        grilla_ancho, 0, RES, RES, fill="#f4eee2", outline="#ecdbc4", width=2
    )
    # Texto
    gamelib.draw_text(
        "TETRIS", grilla_ancho + (panel_ancho / 2), margen, fill="#4f6dae", size=30
    )
示例#3
0
def test_eliminar_lineas_baja_las_lineas_superiores():
    pieza_i = tetris.generar_pieza(pieza=tetris.I)
    juego = tetris.crear_juego(pieza_i)

    # Generar esta superficie:
    #   T
    # T T T
    # I I I I I I I
    # I I I I I I I
    # I I I I I I I
    # I I I I I I I
    #
    # Y verificar que al poner la última I queda así:
    #   T
    # T T T
    pieza_t = tetris.generar_pieza(pieza=tetris.T)
    juego = _ubicar_piezas_I(juego, -4, -1, pieza_t)
    if not juego:
        return False

    # Ubicar la T
    for _ in range(4):
        juego = tetris.mover(juego, IZQUIERDA)

    # Bajar la T hasta consolidarla
    juego, cambiar_pieza = tetris.avanzar(juego, pieza_i)
    while not cambiar_pieza and not tetris.terminado(juego):
        juego, cambiar_pieza = tetris.avanzar(juego, pieza_i)

    if tetris.terminado(juego):
        return False

    # Ubicar el resto de las I
    juego = _ubicar_piezas_I(juego, -1, 5, pieza_i)
    if not juego:
        return False

    # Revisar que no haya superficie en ningún lado excepto en
    # donde está la T
    ancho, alto = tetris.dimensiones(juego)

    pieza_t = tetris.generar_pieza(pieza=tetris.T)
    pieza_t = tetris.trasladar_pieza(pieza_t, 0, alto - 2)

    for x in range(ancho):
        for y in range(alto):
            if tetris.hay_superficie(juego, x, y) and not (x, y) in pieza_t:
                print(f"f en {(x, y)}")
                return False

    for x, y in pieza_t:
        if not tetris.hay_superficie(juego, x, y):
            print("no pieza t")
            return False

    return True
示例#4
0
def main():
    # Inicializar el estado del juego
    #siguiente_pieza = tetris.generar_pieza()
    siguiente = tetris.generar_pieza()
    t1= time.time()
    juego = tetris.crear_juego(tetris.generar_pieza())
    ancho, alto = tetris.dimensiones(juego)
    lista_teclas = leer_teclas()  
    gamelib.resize(800, 900)
    timer_bajar = ESPERA_DESCENDER
    while gamelib.loop(fps=30):
        gamelib.draw_begin()
        dibujar_superficie(juego)
        dibujar_pieza(juego)
        dibujar_siguiente(juego, siguiente)
        gamelib.draw_end()  
        for event in gamelib.get_events():
          if not event:
              break
          if event.type == gamelib.EventType.KeyPress:
              tecla = event.key
              if tecla == lista_teclas[1]:
                juego = tetris.mover(juego, -1)
              if tecla == lista_teclas[3]:
                juego = tetris.mover(juego, 1) 
              if tecla == lista_teclas[5]:
                juego = tetris.mover(juego, 2)
              if tecla == lista_teclas[0]:
                return
              if tecla == lista_teclas[6]:
                juego = tetris.rotar(juego)    
              if tecla == lista_teclas[4]:
                guardar_partida(juego)
              if tecla == lista_teclas[2]:
                juego = recuperar_partida()                           
              # Actualizar el juego, según la tecla presionada
        timer_bajar -= 1
        if timer_bajar == 0:  
            timer_bajar = ESPERA_DESCENDER 
            juego, siguiente_pieza = tetris.avanzar(juego, siguiente)
            if siguiente_pieza:
              siguiente = tetris.generar_pieza()
            if tetris.terminado(juego):
              gamelib.draw_image('img/perdiste.gif', 50, 200)
              t2 = time.time()
              tiempo_final = t2- t1
              gamelib.draw_rectangle(0, 0, 595, 60, outline='white', fill='salmon')
              gamelib.draw_text('Tu tiempo fue de {} segundos'.format(tiempo_final), 10, 17, fill='#000', size=18, anchor='nw')
              puntuaciones(tiempo_final)
              break

    while gamelib.is_alive():
      ordenar_puntuaciones()
      event = gamelib.wait(gamelib.EventType.KeyPress)
示例#5
0
def test_grilla_esta_vacia_al_iniciar():
    pieza_inicial = tetris.generar_pieza(tetris.T)
    juego = tetris.crear_juego(pieza_inicial)

    ancho, alto = tetris.dimensiones(juego)
    for y in range(alto):
        for x in range(ancho):
            if tetris.hay_superficie(juego, x, y):
                return False

    return True
示例#6
0
def dibujar_superficie(juego):
  '''Se dibujara la grilla del tetris'''
  gamelib.draw_image('img/fondo6.gif', -80, 10)
  ancho, alto = tetris.dimensiones(juego)
  gamelib.draw_line(600, 250, 900, 250, fill='white', width=3)
  gamelib.draw_text('PRÓXIMA PIEZA:', 630, 10, fill='white', anchor='nw')
  for x in range (0, 600, 66):
    gamelib.draw_line(x, 0, x, 900, fill='white', width=2) 
  for y in range (0, 900, 50):
    gamelib.draw_line(0, y, 595, y, fill='white', width=2)  
  for y in range(alto):
        for x in range(ancho):
            if tetris.hay_superficie(juego, x, y):  
              gamelib.draw_rectangle(x*66, y*50, (x+1)*66, (y+1)*50, outline='white', fill='purple')
def dibujar_puntuaje(juego, puntuaje):
    """
    Recibe el estado del juego y el puntuaje actual
    Dibuja en el panel los puntos
    """
    ancho, alto = tetris.dimensiones(juego)
    proporcio = alto / ancho
    grilla_ancho = RES / proporcio
    panel_ancho = RES - grilla_ancho
    margen = RES * 2 / 10
    gamelib.draw_text(
        f"Puntos: {int(puntuaje)}",
        grilla_ancho + (panel_ancho / 2),
        RES - margen,
        fill="#4f6dae",
        size=20,
    )
def dibujar_superficie(juego):
    """
    Recibe el juego y dibuja la superficie
    """
    ancho, alto = tetris.dimensiones(juego)
    proporcio = alto / ancho
    grilla_ancho = RES / proporcio
    lado_bloque = grilla_ancho / ancho
    for y in range(alto):
        for x in range(ancho):
            if tetris.hay_superficie(juego,x,y):
                gamelib.draw_rectangle(
                    x * lado_bloque,
                    y * lado_bloque,
                    (x + 1) * lado_bloque,
                    (y + 1) * lado_bloque,
                    fill="gray",
                )
def dibujar_pieza(juego):
    """
    Recibe el juego y dibuja la pieza actual
    """
    ancho, alto = tetris.dimensiones(juego)
    proporcio = alto / ancho
    grilla_ancho = RES / proporcio
    lado_bloque = grilla_ancho / ancho
    posiciones = tetris.pieza_actual(juego)
    for posicion in posiciones:
        x, y = posicion
        gamelib.draw_rectangle(
            x * lado_bloque,
            y * lado_bloque,
            (x + 1) * lado_bloque,
            (y + 1) * lado_bloque,
            fill="red",
        )
示例#10
0
def test_eliminar_todas_las_lineas():
    pieza_i = tetris.generar_pieza(pieza=tetris.I)
    juego = tetris.crear_juego(pieza_i)

    # Acomodar todas piezas I y verificar que se eliminan todas
    # las líneas
    juego = _ubicar_piezas_I(juego, -4, 5, pieza_i)

    if not juego:  # El juego terminó antes de poder ubicar las I.
        return False

    # Revisar que no haya superficie en ningún lado
    ancho, alto = tetris.dimensiones(juego)
    for x in range(ancho):
        for y in range(alto):
            if tetris.hay_superficie(juego, x, y):
                return False

    return True
def dibujar_siguiente(juego, pieza):
    """
    Recibe el juego y la siguiente pieza
    Dibuja un panel para mostrarsela al usuario
    """
    ancho, alto = tetris.dimensiones(juego)
    proporcio = alto / ancho
    grilla_ancho = RES / proporcio
    lado_bloque = grilla_ancho / ancho
    panel_ancho = RES - grilla_ancho
    eje_y_pieza = list(zip(*pieza))[0]
    bloques_fila = max(eje_y_pieza) + 1
    centro_panel = grilla_ancho + panel_ancho / 2 - (bloques_fila * lado_bloque) / 2
    margen = RES * 2 / 10
    for posicion in pieza:
        x, y = posicion
        gamelib.draw_rectangle(
            (x * lado_bloque) + centro_panel,
            (y * lado_bloque) + margen,
            ((x + 1) * lado_bloque) + centro_panel,
            ((y + 1) * lado_bloque) + margen,
            fill="red",
        )
示例#12
0
def test_dimensiones_correctas():
    pieza_inicial = tetris.generar_pieza(tetris.T)
    juego = tetris.crear_juego(pieza_inicial)

    ancho, alto = tetris.dimensiones(juego)
    return ancho == ANCHO_JUEGO and alto == ALTO_JUEGO