feat(levels): f5 to save current level #4

Merged
bedohswe merged 2 commits from n3tael/p3he:main into main 2024-04-04 18:44:45 +00:00
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

@ -6,8 +6,11 @@ from cmath import exp, pi
from math import acos, copysign from math import acos, copysign
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
@ -93,6 +96,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]: