labyrinth-cs-proj/maze/modules/maze_saveandload.py

91 lines
2.6 KiB
Python
Raw Normal View History

2022-11-03 19:35:23 +00:00
import os
import pickle
from time import sleep
2022-11-24 07:46:04 +00:00
import maze.menu as m
2022-11-04 11:25:22 +00:00
from .PlayerBase_func import input, screenwipe
2022-11-04 11:25:22 +00:00
2022-11-22 10:35:23 +00:00
def save(screen, maze, coords, elapsed):
2022-11-03 19:35:23 +00:00
y, x = screen.getmaxyx()
if "saves" not in os.listdir():
os.mkdir("saves")
names = os.listdir("saves")
screen.addstr(17, x - 17, "Enter filename: ")
name = input(18, x - 17, screen)
2022-11-03 19:35:23 +00:00
if names:
for i in range(len(names)):
names[i] = names[i].replace(".maze", "")
for i in range(len(names)):
for j in range(len(names) - i - 1):
if int(names[j].split("_")[1]) > int(names[j + 1].split("_")[1]):
names[j], names[j + 1] = names[j + 1], names[j]
num = int(names[-1].split("_")[1]) + 1
2022-11-03 19:35:23 +00:00
if not name:
name = "default"
filename = f"maze_{str(num)}_{name}.maze"
else:
filename = f"maze_0_{name}.maze"
f = open("saves//" + filename, "wb")
pickle.dump(maze, f)
pickle.dump(coords, f)
2022-11-22 10:35:23 +00:00
pickle.dump(elapsed, f)
2022-11-03 19:35:23 +00:00
f.close()
screen.addstr(19, x - 17, "Maze saved!")
screen.refresh()
sleep(3)
screen.addstr(17, x - 17, " " * 16)
screen.addstr(18, x - 17, " " * 16)
screen.addstr(19, x - 17, " " * 12)
screen.refresh()
2022-11-03 19:35:23 +00:00
def check():
if len(os.listdir("saves")):
return True
2022-11-03 19:35:23 +00:00
def load(screen):
y, x = screen.getmaxyx()
screen.clear()
screen.refresh()
screen.addstr(2, x // 2 - 4, "LOAD MAZE")
mazes = os.listdir("saves")
sy = 4
for i in range(len(mazes)):
screen.addstr(
sy,
10,
f"{str(i + 1)}. Maze {((mazes[i].replace('.maze', '')).split('_'))[1]} - {((mazes[i].replace('.maze', '')).split('_'))[2]}",
)
2022-11-03 19:35:23 +00:00
sy += 1
while True:
screen.addstr(y // 2 + 5, 0, "Enter preferred maze number: ")
num = input(y // 2 + 5, 30, screen)
2022-11-03 19:35:23 +00:00
if num and type(int(num)) == type(0):
num = int(num) - 1
else:
screen.clear()
screen.refresh()
m.menu(screen)
return
if num < len(mazes):
break
else:
screen.addstr(y - 1, 0, "Entered maze doesn't exist. Please try again.")
while True:
key = screen.getch()
if key == 10:
screen.addstr(y - 1, 0, " " * (x - 5))
screenwipe(screen, 30, y // 2 + 5)
screen.refresh()
break
continue
f = open("saves//" + mazes[num], "rb")
2022-11-03 19:35:23 +00:00
maze = pickle.load(f)
coords = pickle.load(f)
2022-11-22 10:35:23 +00:00
elapsed = pickle.load(f)
2022-11-03 19:35:23 +00:00
f.close()
2022-11-22 10:35:23 +00:00
return maze, coords, elapsed