feat(levels): f5 to save current level

This commit is contained in:
n3tael 2024-04-04 21:41:10 +03:00
parent 359edf4742
commit 4208597ed3
Signed by: n3tael
GPG Key ID: F305925762F035A8
3 changed files with 56 additions and 0 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
__pycache__/ __pycache__/
levels/

30
levels.py Normal file
View File

@ -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

25
main.py
View File

@ -10,8 +10,11 @@ from cmath import exp, pi
from math import acos from math import acos
import pygame import pygame
import pygame.freetype import pygame.freetype
from os import makedirs
from time import time
from gyro import * from gyro import *
from lines import * from lines import *
from levels import *
def deg2rad(rA): return rA/180*PI 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)) 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(): def mainLoop():
gPlayer = GyroVector(0,1) gPlayer = GyroVector(0,1)
display = pygame.display.set_mode((1280,720)) display = pygame.display.set_mode((1280,720))
@ -120,6 +139,12 @@ def mainLoop():
wall_buffer.clear() wall_buffer.clear()
else: else:
wall_buffer.append(gPlayer.cPos) 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() keys = pygame.key.get_pressed()
if keys[pygame.K_d]: if keys[pygame.K_d]: