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

97 lines
2.9 KiB
Python
Raw Normal View History

2022-11-30 12:57:18 +00:00
import curses
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-30 15:57:28 +00:00
"""Saves the maze to a binary file"""
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):
2022-11-30 15:57:28 +00:00
"""Loads a maze and returns maze, coords and time"""
2022-11-03 19:35:23 +00:00
y, x = screen.getmaxyx()
screen.clear()
screen.refresh()
screen.border()
screen.addstr(2, x // 2 - 4, "LOAD MAZE", curses.color_pair(3) | curses.A_BOLD)
2022-11-03 19:35:23 +00:00
mazes = os.listdir("saves")
sy = 5
2022-11-03 19:35:23 +00:00
for i in range(len(mazes)):
msg = f"{str(i + 1)}. Maze {((mazes[i].replace('.maze', '')).split('_'))[1]} - {((mazes[i].replace('.maze', '')).split('_'))[2]}"
2022-11-30 12:57:18 +00:00
screen.addstr(sy, x // 2 - len(msg) // 2, msg)
2022-11-03 19:35:23 +00:00
sy += 1
while True:
screen.addstr(y // 2 + 5, x // 2 - 15, "Enter preferred maze number: ")
num = input(y // 2 + 5, x // 2 + 14, 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:
2022-11-30 12:57:18 +00:00
screen.addstr(
y - 5,
x // 2 - 23,
"Entered maze doesn't exist. Please try again.",
curses.color_pair(1),
)
2022-11-03 19:35:23 +00:00
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