feat(save_level): now displays a alert with filename

This commit is contained in:
n3tael 2024-04-04 23:55:54 +03:00
parent 799165b225
commit fcabed5a19
Signed by: n3tael
GPG Key ID: F305925762F035A8
3 changed files with 60 additions and 5 deletions

40
alert.py Normal file
View File

@ -0,0 +1,40 @@
import pygame
import pygame.freetype
import threading
from time import sleep
class Alert():
def __init__(self, text, display):
self.text = text
font = pygame.freetype.Font(None, 24)
font_fg = (255, 255, 255)
font_bg = (255, 255, 255, 120)
text_image = font.render(text, font_fg)[0]
self.background = pygame.Surface((text_image.get_width() + 12, text_image.get_height() + 12), pygame.SRCALPHA)
self.background.fill(font_bg)
self.rect = self.background.get_rect()
text_rect = text_image.get_rect(center = self.rect.center)
self.background.blit(text_image, text_rect)
self.rect.topleft = (display.get_width() / 2 - self.background.get_width() / 2, display.get_height() - self.background.get_height() - 8)
def draw(self, surface):
surface.blit(self.background, self.rect)
def start_hide_thread(self, alerts, delay):
def hide_delay(delay):
sleep(delay)
try:
alerts.pop(alerts.index(self))
except ValueError:
pass
p = threading.Thread(target=hide_delay, args=(delay,))
p.daemon = True
p.start()

View File

@ -43,9 +43,11 @@ def save_level(level):
level_save = save_level_to_string(level) level_save = save_level_to_string(level)
unix_timestamp = int(time()) unix_timestamp = int(time())
save = open(f"./levels/{unix_timestamp}.p3hel", "w") filename = f'{unix_timestamp}.p3hel'
save = open(f"./levels/{filename}", "w")
save.write(level_save) save.write(level_save)
save.close() save.close()
return filename
def open_level(path): def open_level(path):
save = open(path, "r") save = open(path, "r")

21
main.py
View File

@ -2,7 +2,6 @@
from numba import jit from numba import jit
from math import copysign, pi, acos from math import copysign, pi, acos
from time import time
import pygame import pygame
import pygame.freetype import pygame.freetype
@ -12,6 +11,7 @@ from gyro import *
from levels import * from levels import *
from constants import * from constants import *
from draw import draw from draw import draw
from alert import Alert
bCap = True bCap = True
@ -39,8 +39,6 @@ def renderDebugInfo(gPlayer, clock, fontSize = 18):
return fps_surf, cPos_surf, cRot_surf return fps_surf, cPos_surf, cRot_surf
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))
@ -48,6 +46,14 @@ def mainLoop():
fontSize = 18 fontSize = 18
debugInfo = True debugInfo = True
wall_buffer = [] wall_buffer = []
alerts = []
def alert_append(alert, delay):
if len(alerts) >= 1:
alerts.pop()
alerts.append(alert)
alert.start_hide_thread(alerts, delay)
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
@ -69,7 +75,11 @@ def mainLoop():
else: else:
wall_buffer.append(gPlayer.cPos) wall_buffer.append(gPlayer.cPos)
if event.key == pygame.K_F5: if event.key == pygame.K_F5:
save_level(level) filename = save_level(level)
alert = Alert(f"File saved as {filename}", display)
alert_append(alert, 5)
if event.key == pygame.K_F9: if event.key == pygame.K_F9:
# TODO: @bedohswe make open file dialog # TODO: @bedohswe make open file dialog
# open_level(path) # open_level(path)
@ -112,6 +122,9 @@ def mainLoop():
c = complex(Poincare2Klein(gPlayer.cPos)) c = complex(Poincare2Klein(gPlayer.cPos))
pygame.draw.rect(display, WHITE, (c.real * 100 + 95, (c.imag * 100 + 195), 10, 10)) pygame.draw.rect(display, WHITE, (c.real * 100 + 95, (c.imag * 100 + 195), 10, 10))
for alert in alerts:
alert.draw(display)
pygame.display.update() pygame.display.update()
if bCap: if bCap:
clock.tick(60) clock.tick(60)