feat(save_level): now displays a alert with filename #6
40
alert.py
Normal file
40
alert.py
Normal 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()
|
@ -43,9 +43,11 @@ def save_level(level):
|
||||
level_save = save_level_to_string(level)
|
||||
|
||||
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.close()
|
||||
return filename
|
||||
|
||||
def open_level(path):
|
||||
save = open(path, "r")
|
||||
|
21
main.py
21
main.py
@ -2,7 +2,6 @@
|
||||
from numba import jit
|
||||
|
||||
from math import copysign, pi, acos
|
||||
from time import time
|
||||
|
||||
import pygame
|
||||
import pygame.freetype
|
||||
@ -12,6 +11,7 @@ from gyro import *
|
||||
from levels import *
|
||||
from constants import *
|
||||
from draw import draw
|
||||
from alert import Alert
|
||||
|
||||
bCap = True
|
||||
|
||||
@ -39,8 +39,6 @@ def renderDebugInfo(gPlayer, clock, fontSize = 18):
|
||||
|
||||
return fps_surf, cPos_surf, cRot_surf
|
||||
|
||||
|
||||
|
||||
def mainLoop():
|
||||
gPlayer = GyroVector(0,1)
|
||||
display = pygame.display.set_mode((1280,720))
|
||||
@ -48,6 +46,14 @@ def mainLoop():
|
||||
fontSize = 18
|
||||
debugInfo = True
|
||||
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:
|
||||
for event in pygame.event.get():
|
||||
@ -69,7 +75,11 @@ def mainLoop():
|
||||
else:
|
||||
wall_buffer.append(gPlayer.cPos)
|
||||
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:
|
||||
# TODO: @bedohswe make open file dialog
|
||||
# open_level(path)
|
||||
@ -112,6 +122,9 @@ def mainLoop():
|
||||
c = complex(Poincare2Klein(gPlayer.cPos))
|
||||
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()
|
||||
if bCap:
|
||||
clock.tick(60)
|
||||
|
Loading…
Reference in New Issue
Block a user