-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_wave.py
136 lines (112 loc) · 4.08 KB
/
py_wave.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import pygame
import pygame_gui
import sys
import os
import importlib
from draw_context import DrawContext
# Initialize Pygame
pygame.init()
# Set up the display
screen_width, screen_height = 1280, 960
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Py-Wave')
# Define the text and font
font_size = 100
font = pygame.font.SysFont(None, font_size)
menu_font = pygame.font.SysFont(None, 50)
debug_font = pygame.font.SysFont(None, 30)
# Load designs
designs = []
design_dir = 'designs'
for filename in os.listdir(design_dir):
if filename.endswith('.py'):
module_name = filename[:-3]
module = importlib.import_module(f'{design_dir}.{module_name}')
design_name, design_instance = module.register_design()
designs.append((design_name, design_instance))
# Sort designs by name
designs.sort(key=lambda x: x[0])
# Menu options
menu_options = [design[0] for design in designs]
selected_option = 0
def draw_menu():
screen.fill((0, 0, 0))
for i, option in enumerate(menu_options):
color = (255, 255, 255) if i == selected_option else (100, 100, 100)
text = menu_font.render(option, True, color)
text_rect = text.get_rect(center=(screen_width // 2, screen_height // 2 + i * 60))
screen.blit(text, text_rect)
pygame.display.flip()
# Initialize the clock
clock = pygame.time.Clock()
# Initialize pygame_gui
manager = pygame_gui.UIManager((screen_width, screen_height))
# Main loop
running = True
current_frame = 0
total_frames = 1000 # Example total frames
in_menu = True
selected_option = 0
menu_options = [design[0] for design in designs]
paused = False
show_debug = True
while running:
time_delta = clock.tick(60) / 1000.0 # Time in seconds since last frame
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if in_menu:
if event.key == pygame.K_UP:
selected_option = (selected_option - 1) % len(menu_options)
elif event.key == pygame.K_DOWN:
selected_option = (selected_option + 1) % len(menu_options)
elif event.key == pygame.K_RETURN:
in_menu = False
# Initialize the selected design
designs[selected_option][1].init(manager)
else:
if event.key == pygame.K_ESCAPE:
in_menu = True
# Uninitialize the selected design
designs[selected_option][1].uninit()
elif event.key == pygame.K_LEFT:
current_frame = max(0, current_frame - 1)
elif event.key == pygame.K_RIGHT:
current_frame = min(total_frames - 1, current_frame + 1)
elif event.key == pygame.K_SPACE:
paused = not paused
elif event.key == pygame.K_d:
show_debug = not show_debug
if not in_menu:
manager.process_events(event)
designs[selected_option][1].process_event(event, manager)
keys = pygame.key.get_pressed()
if not in_menu and not paused:
if keys[pygame.K_LEFT]:
current_frame = max(0, current_frame - 1)
elif keys[pygame.K_RIGHT]:
current_frame = min(total_frames - 1, current_frame + 1)
if not in_menu:
manager.update(time_delta)
context = DrawContext(screen, font, current_frame, total_frames, screen_width, screen_height)
if in_menu:
draw_menu()
else:
designs[selected_option][1].draw_design(context)
if not paused:
current_frame += 1
# Calculate FPS
fps = clock.get_fps()
# Render FPS if debug information is enabled
if show_debug:
fps_text = debug_font.render(f"FPS: {fps:.2f}", True, (255, 255, 255))
screen.blit(fps_text, (10, 10))
if not in_menu:
# Render the UI
manager.draw_ui(screen)
# Update the display
pygame.display.flip()
# Quit Pygame
pygame.quit()
sys.exit()