Merge pull request 'feat(save_level): now displays a alert with filename' (#6) from n3tael/p3he:main into main

Reviewed-on: bedohswe/p3he#6
This commit is contained in:
bʰedoh₂ swé 2024-04-04 21:08:47 +00:00
commit 64e48bb0f7
3 changed files with 60 additions and 3 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")

19
main.py
View File

@ -13,6 +13,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
@ -43,7 +44,15 @@ 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)
try: try:
leveltoparse = open_level(sys.argv[1]) leveltoparse = open_level(sys.argv[1])
except IndexError: except IndexError:
@ -70,7 +79,10 @@ 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)
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()
if keys[pygame.K_d]: if keys[pygame.K_d]:
@ -109,6 +121,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)