Adding binary file save/load functions

This commit is contained in:
adithyagenie 2022-11-04 01:05:23 +05:30
parent 84ceb22e1e
commit 1022ba6f23
3 changed files with 115 additions and 21 deletions

3
.gitignore vendored
View file

@ -3,4 +3,5 @@ maze.zip
PlayerBase_func.pybak
__pycache__
maze/__pycache__
maze/modules/__pycache__
maze/modules/__pycache__
saves

View file

@ -6,6 +6,7 @@ from collections import defaultdict
from itertools import tee
import maze.modules.PlayerBase_func as database
from .about import about
import maze.modules.maze_saveandload as sl
WON = 0
PAUSED = False
@ -240,18 +241,18 @@ def pathfinding_demo(maze, screen, start_ts):
global PAUSED
if PAUSED:
start_paused_ts = time.time()
screen.addstr(14, maxx - 17, "PAUSED")
screen.addstr(4, maxx - 17, "PAUSED")
screen.refresh()
while True:
pausekey = screen.getch()
if pausekey == ord("r"):
end_paused_ts = time.time()
screen.addstr(14, maxx - 17, " ")
screen.addstr(4, maxx - 17, " ")
PAUSED = False
break
pause_elapsed = int(end_paused_ts - start_paused_ts)
actual_elapsed = str(int(time.time()-start_ts) - pause_elapsed)
screen.addstr(2, maxx - 17, actual_elapsed + " sec")
screen.addstr(5, maxx - 17, actual_elapsed + " sec")
screen.refresh()
key = screen.getch()
# print("Max=",maxy, maxx, "Current=", current_coords[0], current_coords[1])
@ -260,6 +261,8 @@ def pathfinding_demo(maze, screen, start_ts):
elif key == ord("p"):
PAUSED = True
continue
elif key == ord("m"):
sl.save(screen, maze, current_coords)
elif current_coords[0] == maxy - 3 and current_coords[1] == maxx - 27:
screen.clear()
screen.refresh()
@ -399,10 +402,11 @@ def menu(screen):
screen.refresh()
screen.addstr(10, x // 2 - 2, "MENU")
screen.addstr(13, 0, "space - Play")
screen.addstr(14, 0, "a - Account Settings")
screen.addstr(15, 0, "l - Leaderboard")
screen.addstr(16, 0, "b - About")
screen.addstr(17, 0, "esc - Quit")
screen.addstr(14, 0, "f - Load game from file")
screen.addstr(15, 0, "a - Account Settings")
screen.addstr(16, 0, "l - Leaderboard")
screen.addstr(17, 0, "x - About")
screen.addstr(18, 0, "esc - Quit")
while True:
key = screen.getch()
if key == ord(" "):
@ -415,27 +419,43 @@ def menu(screen):
database.screenhandler(screen)
elif key == ord("l"):
database.leaderboard(screen)
elif key == ord("b"):
elif key == ord("x"):
about(screen)
elif key == ord("f"):
present = sl.check()
if present:
maze = sl.load(screen)
if maze:
play(screen, maze[0])
return
else:
screen.addstr(20, 0, "No saved mazes present.")
while True:
key2 = screen.getch()
if key2 == 10:
screen.addstr(20, 0, " " * 23)
break
def play(screen):
came_out = 0
start_ts = 0
end_ts = 0
def play(screen, loadedmaze = None):
y, x = screen.getmaxyx()
height, width = int((y - 2) / 2), int((x - 2) / 2)
screen.clear()
maze = Maze(height, width)
if not loadedmaze:
maze = Maze(height, width)
else:
maze = loadedmaze
screen.addstr(0, 0, str(maze))
screen.refresh()
screen.addstr(0, x - 23, "LABYRINTH")
screen.addstr(2, x - 23, "Time:")
screen.addstr(5, x - 23, "esc - Quit")
screen.addstr(6, x - 23, "Right - Move right")
screen.addstr(7, x - 23, "Left - Move left")
screen.addstr(8, x - 23, "Up - Move up")
screen.addstr(9, x - 23, "Down - Move down")
screen.addstr(5, x - 23, "Time:")
screen.addstr(8, x - 23, "esc - Quit")
screen.addstr(9, x - 23, "Up - Move up")
screen.addstr(10, x - 23, "Down - Move down")
screen.addstr(11, x - 23, "Left - Move left")
screen.addstr(12, x - 23, "Right - Move right")
screen.addstr(13, x - 23, "p - Pause")
screen.addstr(14, x - 23, "r - Resume")
screen.addstr(15, x - 23, "m - save")
screen.refresh()
start_ts = time.time()
pathfinding_demo(maze, screen, start_ts)

View file

@ -0,0 +1,73 @@
import os
import pickle
from .PlayerBase_func import Input, screenwipe
import maze.modules.maze as m
from time import sleep
def save(screen, maze, coords):
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)
if names:
num = int(((names[-1].replace('.maze', '')).split('_'))[1]) + 1
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)
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()
def check():
if len(os.listdir("saves")):
return True
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]}")
sy += 1
while True:
screen.addstr(y // 2 + 5, 0, "Enter preferred maze number: ")
num = Input(y // 2 + 5, 30, screen)
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")
maze = pickle.load(f)
coords = pickle.load(f)
f.close()
return maze, coords