Esempio n. 1
0
def nivel(grilla, accion=None):
    '''Recibe un nivel en forma de grilla (lista de listas), y dibuja en la ventana
    la representacion de esa grilla.
    '''

    tamanio_x, tamanio_y = soko.dimensiones(grilla)

    for x in range(tamanio_x):
        for y in range(tamanio_y):

            px_x = (x * TAMANIO_CASILLA
                    ) + 2  # Lo desplazo 2 pixeles, porque es lo que ocupan
            px_y = (y * TAMANIO_CASILLA) + 2  # los bordes de la ventana.

            gl.draw_image(IMG_SUELO, px_x, px_y)

            if soko.hay_jugador(grilla, x, y):
                gl.draw_image(IMG_JUGADOR.get(accion, "src/player_s.gif"),
                              px_x, px_y)

            elif soko.hay_caja(grilla, x, y):
                gl.draw_image(IMG_CAJA, px_x, px_y)

            elif soko.hay_pared(grilla, x, y):
                gl.draw_image(IMG_PARED, px_x, px_y)

            if soko.hay_objetivo(grilla, x, y):
                gl.draw_image(IMG_OBJETIVO, px_x, px_y)
Esempio n. 2
0
def main():
    # Inicializar el estado del juego
    niveles = archivos.cargar_niveles('niveles.txt')
    acciones = archivos.cargar_accion_de_tecla('teclas.txt')

    grilla, nivel = juego_inicializar(niveles)

    x, y = soko.dimensiones(grilla)
    gamelib.resize(x * PIXELES_GIF, y * PIXELES_GIF)

    while gamelib.is_alive():

        gamelib.draw_begin()
        mostrar_interfaz(grilla)
        gamelib.draw_end()

        ev = gamelib.wait(gamelib.EventType.KeyPress)
        if not ev:
            break

        tecla = ev.key
        accion = procesar_tecla_presionada(tecla, acciones)
        grilla = juego_actualizar(grilla, nivel, niveles, accion)

        if not grilla:
            break

        if soko.juego_ganado(grilla):
            nivel, grilla = juego_pasar_nivel(nivel, niveles)
            if not grilla:
                break
Esempio n. 3
0
def juego_pasar_nivel(nivel, niveles):
    '''Prosigue al siguiente nivel, devulve None con respecto a la grilla si supera el ultimo nivel'''
    nivel += 1
    if nivel == len(niveles):
        gamelib.say('Felicidades, compleataste todos los niveles')
        return nivel, None
    grilla = juego_crear(nivel, niveles)
    x, y = soko.dimensiones(grilla)
    gamelib.resize(x * PIXELES_GIF, y * PIXELES_GIF)
    return nivel, grilla
Esempio n. 4
0
def cambiar_tamanio_ventana(grilla):
    '''Recibe un nivel en forma de grilla (lista de listas) y ajusta el tamaño de la ventana,
    acorde a las dimensiones del nivel. Devuelve el tamaño de la ventana en pixeles (x, y)'''
    x, y = soko.dimensiones(grilla)

    x *= TAMANIO_CASILLA
    y *= TAMANIO_CASILLA

    gl.resize(x, y)

    return x, y
Esempio n. 5
0
def verificar_estado(desc, grilla):
    x = None
    y = None
    try:
        w, h = soko.dimensiones(grilla)
        assert (w, h) == (len(desc[0]), len(desc))
        for y in range(h):
            for x in range(w):
                c = desc[y][x]
                if c == '#':
                    assert soko.hay_pared(grilla, x, y)
                    assert not soko.hay_objetivo(grilla, x, y)
                    assert not soko.hay_jugador(grilla, x, y)
                    assert not soko.hay_caja(grilla, x, y)
                elif c == '.':
                    assert not soko.hay_pared(grilla, x, y)
                    assert soko.hay_objetivo(grilla, x, y)
                    assert not soko.hay_jugador(grilla, x, y)
                    assert not soko.hay_caja(grilla, x, y)
                elif c == '$':
                    assert not soko.hay_pared(grilla, x, y)
                    assert not soko.hay_objetivo(grilla, x, y)
                    assert not soko.hay_jugador(grilla, x, y)
                    assert soko.hay_caja(grilla, x, y)
                elif c == '@':
                    assert not soko.hay_pared(grilla, x, y)
                    assert not soko.hay_objetivo(grilla, x, y)
                    assert soko.hay_jugador(grilla, x, y)
                    assert not soko.hay_caja(grilla, x, y)
                elif c == '*':
                    assert not soko.hay_pared(grilla, x, y)
                    assert soko.hay_objetivo(grilla, x, y)
                    assert not soko.hay_jugador(grilla, x, y)
                    assert soko.hay_caja(grilla, x, y)
                elif c == '+':
                    assert not soko.hay_pared(grilla, x, y)
                    assert soko.hay_objetivo(grilla, x, y)
                    assert soko.hay_jugador(grilla, x, y)
                    assert not soko.hay_caja(grilla, x, y)
    except AssertionError as e:
        print('Estado esperado:')
        print('\n'.join(desc))
        print()
        print('Estado actual:')
        pprint.pprint(grilla)
        print()
        if x is not None and y is not None:
            print(f'Error en columna = {x}, fila = {y}:')
            print()
        raise
Esempio n. 6
0
def dibujar(juego):
    alto, ancho = soko.dimensiones(juego)
    pintar_piso(ancho, alto)
    for j in range(len(juego)):
        for i in range(len(juego[j])):
            x = TAMAÑO_IMAGEN*i
            y = TAMAÑO_IMAGEN*j
            if juego[j][i] == "*":
                gamelib.draw_image("img/goal.gif", x, y)
                gamelib.draw_image("img/box.gif", x, y)
            elif juego[j][i] == "+":
                gamelib.draw_image("img/goal.gif", x, y)
                gamelib.draw_image("img/player.gif", x, y)
            else:
                gamelib.draw_image("img/"+IMAGENES[juego[j][i]], x, y)
Esempio n. 7
0
def main():
    # Inicializar el estado del juego
    niveles = archivos.cargar_niveles('niveles.txt')
    acciones = archivos.cargar_accion_de_tecla('teclas.txt')
    estados = Pila()
    pistas = None

    grilla, nivel = juego_inicializar(niveles)

    x, y = soko.dimensiones(grilla)
    gamelib.resize(x * PIXELES_GIF, y * PIXELES_GIF)

    while gamelib.is_alive():

        gamelib.draw_begin()
        mostrar_interfaz(grilla)
        if pistas != None:
            gamelib.draw_text('Pista encontrada',
                              PIXELES_GIF,
                              PIXELES_GIF // 2,
                              size=12,
                              fill='#00FF00')
        gamelib.draw_end()

        ev = gamelib.wait(gamelib.EventType.KeyPress)
        if not ev:
            break

        tecla = ev.key
        accion = procesar_tecla_presionada(tecla, acciones)

        grilla, estados, pistas = juego_actualizar(grilla, nivel, niveles,
                                                   accion, estados, pistas)

        if not grilla:
            break

        if soko.juego_ganado(grilla):
            while not estados.esta_vacia():
                estados.desapilar()
            pistas = None
            nivel, grilla = juego_pasar_nivel(nivel, niveles)
            if not grilla:
                break
Esempio n. 8
0
def mostrar_interfaz(grilla):
    '''Muestra la interfaz a partir de la grilla, en su estado actual, dependiendo de su simbolo se mostrara en la interfaz
    el gif correspondiente'''
    columnas, filas = soko.dimensiones(grilla)
    for fila in range(filas):
        for columna in range(columnas):
            gamelib.draw_image('img/ground.gif', columna * PIXELES_GIF,
                               fila * PIXELES_GIF)
            if soko.hay_caja(grilla, columna, fila):
                gamelib.draw_image('img/box.gif', columna * PIXELES_GIF,
                                   fila * PIXELES_GIF)
            if soko.hay_objetivo(grilla, columna, fila):
                gamelib.draw_image('img/goal.gif', columna * PIXELES_GIF,
                                   fila * PIXELES_GIF)
            if soko.hay_jugador(grilla, columna, fila):
                gamelib.draw_image('img/player.gif', columna * PIXELES_GIF,
                                   fila * PIXELES_GIF)
            if soko.hay_pared(grilla, columna, fila):
                gamelib.draw_image('img/wall.gif', columna * PIXELES_GIF,
                                   fila * PIXELES_GIF)
Esempio n. 9
0
def resize(juego):
    ancho, largo = soko.dimensiones(juego)

    gamelib.resize(ancho*TAMAÑO_IMAGEN, largo*TAMAÑO_IMAGEN)
Esempio n. 10
0
def main():
    # Inicializar el estado del juego
    deshacer = Pila()
    pistas = []

    teclas = archivo_teclas(RUTA_TECLAS)
    niveles = archivo_niveles(RUTA_NIVELES)

    contador = es_nivel(gamelib.input("Eliga un nivel:"), niveles)
    juego = emparejar(niveles[contador])  #lista de cadenas

    c, f = soko.dimensiones(juego)
    gamelib.resize(c * DIM_CELDA, f * DIM_CELDA)

    juego = soko.crear_grilla(juego)  #lista de listas
    dibujar_juego(contador, juego, 1)

    while gamelib.is_alive():
        ev = gamelib.wait(gamelib.EventType.KeyPress)
        if not ev:
            break
        # Actualizar el estado del juego, según la `tecla` presionada
        tecla = ev.key

        if es_tecla(tecla, teclas) == None:
            continue

        if tecla == 'Escape':
            gamelib.say("Gracias por jugar Sokoban :)")
            break

        if tecla == 'h':
            if len(pistas) == 0:
                pistas = backtraking(contador, juego)
                if pistas == None:
                    pistas = []
            else:
                pista = pistas.pop()
                juego = soko.mover(juego, pista)
                deshacer.apilar(juego)
                dibujar_juego(contador, juego, 2)  #pista disponible

        if soko.juego_ganado(juego):
            contador += 1
            while not deshacer.esta_vacia():
                deshacer.desapilar()
            gamelib.say("Pasaste al siguiente nivel :)")

            juego = emparejar(niveles[contador])
            c, f = soko.dimensiones(juego)
            gamelib.resize(c * DIM_CELDA, f * DIM_CELDA)
            juego = soko.crear_grilla(juego)
            dibujar_juego(contador, juego, 1)

        if tecla == 'r':
            if len(pistas) != 0:
                pistas = []
            juego = emparejar(niveles[contador])
            c, f = soko.dimensiones(juego)
            gamelib.resize(c * DIM_CELDA, f * DIM_CELDA)
            juego = soko.crear_grilla(juego)
            dibujar_juego(contador, juego, 1)

        if tecla == 'Control_L':
            if not deshacer.esta_vacia():
                juego = deshacer.desapilar()
            dibujar_juego(contador, juego, 1)

        if teclas[tecla] in DIRECCIONES:
            deshacer.apilar(juego)
            juego = soko.mover(juego, DIRECCIONES[teclas[tecla]])
            dibujar_juego(contador, juego, 1)

        if tecla != 'h':  #vaciar la lista
            if len(pistas) != 0:
                pistas = []