-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPI_Extra_Laberinto.py
78 lines (61 loc) · 2.19 KB
/
PI_Extra_Laberinto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os, random, readchar
from dataclasses import dataclass
from typing import List
def leer_mapa(ruta: str):
with open(ruta, "r", encoding="utf-8") as archivo:
next(archivo)
mapa = [list(linea.rstrip()) for linea in archivo]
return mapa
@dataclass
class Juego:
nombre: str
mapa: List[List] = None
pos_inicial: tuple = None
pos_final: tuple = None
def limpiar_pantalla(self):
os.system("cls" if os.name == "nt" else "clear")
def print_mapa(self):
self.limpiar_pantalla()
for row in self.mapa:
print(" ".join(row))
def main_loop(self):
px, py = self.pos_inicial
while (px, py) != self.pos_final:
self.mapa[py][px] = "P"
self.print_mapa()
self.mapa[py][px] = "."
px, py = self.mover(px, py)
print("Fin del Juego!!")
print(f"Lo lograste!! {self.nombre}, \nFelicidades eres el Ganador")
def mover(self, px, py):
new_px, new_py = px, py
tecla = readchar.readkey()
if tecla == readchar.key.UP:
new_py = py - 1
if new_py >= 0 and self.mapa[new_py][px] != "#":
py = new_py
elif tecla == readchar.key.DOWN:
new_py = py + 1
if new_py < len(self.mapa) and self.mapa[new_py][px] != "#":
py = new_py
elif tecla == readchar.key.LEFT:
new_px = px - 1
if new_px >= 0 and self.mapa[py][new_px] != "#":
px = new_px
elif tecla == readchar.key.RIGHT:
new_px = px + 1
if new_px < len(self.mapa[py]) and self.mapa[py][new_px] != "#":
px = new_px
return px, py
def main():
nombre = input("Digite su nombre: ")
print("!Bienvenido a esta nueva Aventura, {}!".format(nombre))
lista_mapas = os.listdir("tableros/")
ruta_mapa = random.choice(lista_mapas)
mapa = leer_mapa("tableros/"+ruta_mapa)
pos_inicial = (0, 0)
pos_final = (19, 20)
juego = Juego(nombre, mapa, pos_inicial, pos_final)
juego.main_loop()
if __name__ == "__main__":
main()