From 4208597ed3e6eb7d7cf7d48f03d18594ec033588 Mon Sep 17 00:00:00 2001 From: n3tael Date: Thu, 4 Apr 2024 21:41:10 +0300 Subject: [PATCH] feat(levels): f5 to save current level --- .gitignore | 1 + levels.py | 30 ++++++++++++++++++++++++++++++ main.py | 25 +++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 levels.py diff --git a/.gitignore b/.gitignore index c18dd8d..181ace2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ __pycache__/ +levels/ \ No newline at end of file diff --git a/levels.py b/levels.py new file mode 100644 index 0000000..065788d --- /dev/null +++ b/levels.py @@ -0,0 +1,30 @@ +from main import Segment + +def parse_level_from_string(string): + level = [] + + raw_segments = string.split('|') + + for segment in raw_segments: + raw_segment = segment.split('/') + + level.append( + Segment( + bool(raw_segment[0]), + float(raw_segment[1]), + complex(raw_segment[2]), + tuple(map(int, raw_segment[3][1:len(raw_segment[3])-1].split(', '))) + ) + ) + + return level + +def save_level_to_string(level): + string_level = "" + + for segment in level: + string_level += f'{segment.isFinite}/{segment.pointA}/{segment.pointB}/{segment.color}' + if segment != level[len(level) - 1]: + string_level += '|' + + return string_level \ No newline at end of file diff --git a/main.py b/main.py index c32f2b9..2d1e7bb 100755 --- a/main.py +++ b/main.py @@ -10,8 +10,11 @@ from cmath import exp, pi from math import acos import pygame import pygame.freetype +from os import makedirs +from time import time from gyro import * from lines import * +from levels import * def deg2rad(rA): return rA/180*PI @@ -96,6 +99,22 @@ def make_wall(wall_buffer): Segment(True, wall_buffer[0], wall_buffer[1], (23,74,183)) ) +def save_level(level): + makedirs("./levels/", exist_ok=True) + + level_save = save_level_to_string(level) + + unix_timestamp = int(time()) + save = open(f"./levels/{unix_timestamp}.p3hel", "w") + save.write(level_save) + save.close() + +def open_level(path): + save = open(path, "r") + level = save.read() # don't know it working + + save.close() + def mainLoop(): gPlayer = GyroVector(0,1) display = pygame.display.set_mode((1280,720)) @@ -120,6 +139,12 @@ def mainLoop(): wall_buffer.clear() else: wall_buffer.append(gPlayer.cPos) + if event.key == pygame.K_F5: + save_level(level) + if event.key == pygame.K_F9: + # TODO: @bedohswe make open file dialog + # open_level(path) + pass keys = pygame.key.get_pressed() if keys[pygame.K_d]: