GD_Pygame/gd_pygame.py

108 lines
3.4 KiB
Python
Raw Permalink Normal View History

2024-02-27 18:06:14 +01:00
#!/usr/bin/python3
import pygame
from pygame._sdl2 import Window
from dataclasses import dataclass
from wobbl_tools.data_file import load_dataclass_json, save_dataclass_json
2024-02-27 20:12:13 +01:00
from ui import UI
2024-02-27 18:06:14 +01:00
@dataclass
class Settings:
fps: int = 60
window_size: tuple = (1000, 600)
class GDPygame:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((1000, 600), pygame.RESIZABLE)
pygame.display.set_caption("Game")
self.window = Window.from_display_module()
self.loading_surface = self.generate_loading_surface()
self.screen.blit(self.loading_surface, (0, 0))
pygame.display.update()
self.settings = load_dataclass_json(Settings, "settings.json")
setattr(self.settings, "save", lambda: save_dataclass_json(self.settings, "settings.json"))
self.fps = self.settings.fps
2024-02-27 20:12:13 +01:00
self.ui = UI(self)
2024-02-27 18:06:14 +01:00
# colors
self.gray = (20, 20, 20)
2024-02-27 20:12:13 +01:00
self.blue = (10, 10, 140)
2024-02-27 18:06:14 +01:00
# pygame objects
self.clock = pygame.time.Clock()
self.mouse = pygame.mouse
self.pusab = pygame.font.Font("PUSAB.otf")
2024-02-27 20:35:45 +01:00
self.floor_texture = pygame.image.load("assets/textures/floors/floor_01.png") # textures
self.floor_texture = pygame.transform.scale(self.floor_texture, (self.floor_texture.get_width() * 4, self.floor_texture.get_height() * 4))
self.background_texture = pygame.image.load("assets/textures/backgrounds/background_01.png")
2024-02-27 20:38:15 +01:00
self.background_texture = pygame.transform.scale(self.background_texture, (self.background_texture.get_width() * ((self.screen.get_height() - self.floor_texture.get_height()) // self.background_texture.get_height()), self.screen.get_height() - self.floor_texture.get_height())) # properly scale the background
2024-02-27 20:35:45 +01:00
2024-02-27 18:06:14 +01:00
# loading finished
self.window.size = self.settings.window_size
self.running = True
def generate_loading_surface(self):
default_font = pygame.font.Font("PUSAB.otf", 32)
loading_text = default_font.render("Loading...", True, (240, 240, 240))
w, h = self.screen.get_size()
loading_surface = pygame.Surface((w, h), flags=pygame.SRCALPHA)
loading_surface.fill((40, 40, 40))
loading_surface.blit(loading_text, (w // 2 - loading_text.get_width() // 2, h // 2 - loading_text.get_height() // 2))
return loading_surface
def loop(self):
self.screen.fill(self.gray)
2024-02-27 20:12:13 +01:00
self.ui.draw()
2024-02-27 18:06:14 +01:00
self.get_events()
if not self.running:
return
2024-02-27 20:38:15 +01:00
if self.loading_surface.get_alpha() > 0: # make the loading screen fade out
2024-02-27 18:06:14 +01:00
self.screen.blit(self.loading_surface, (0, 0))
self.loading_surface.set_alpha(self.loading_surface.get_alpha() - 4)
pygame.display.update()
self.clock.tick(self.fps)
def get_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.exit()
return
2024-02-27 20:39:19 +01:00
elif event.type == pygame.VIDEORESIZE: # when the window gets resized
2024-02-27 18:06:14 +01:00
self.window_update(event.size)
def exit(self):
self.running = False
self.settings.save()
print("Bye!")
def window_update(self, size):
self.settings.window_size = size
self.ui.update()
2024-02-27 18:06:14 +01:00
if __name__ == "__main__":
geometry_dash = GDPygame()
while geometry_dash.running:
geometry_dash.loop()