Updated Example

This commit is contained in:
OusmBlueNinja 2024-09-18 21:31:49 -05:00 committed by GitHub
parent f317473664
commit b76da75ea3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

37
main.py
View File

@ -20,16 +20,17 @@ slider_y = pygui.Slider((20, 100), (200, 20), min_value=0, max_value=HEIGHT, sta
label_x = pygui.Label("X: ", (20, 35), 15) label_x = pygui.Label("X: ", (20, 35), 15)
label_y = pygui.Label("Y: ", (20, 85), 15) label_y = pygui.Label("Y: ", (20, 85), 15)
knob = pygui.Knob((25,150), 40, 0, 105.5, 1, 1) # Add a button to reset the position
reset_button = pygui.Button("Reset Position", (20, 150), (150, 40))
knob = pygui.Knob((25, 200), 40, 0, 255, 1, 1)
fps_label = pygui.Label("FPS: ", (5, 50), 20) fps_label = pygui.Label("FPS: ", (5, 50), 20)
position_label = pygui.Label("POS: ", (5, 70), 20) position_label = pygui.Label("POS: ", (5, 70), 20)
color_label = pygui.Label("COL: ", (5, 90), 20) color_label = pygui.Label("COL: ", (5, 90), 20)
# Create windows and add them to the window stack # Create windows and add them to the window stack
window = pygui.Window("Move Object", (600, 50), (250, 150), elements=[label_x, label_y, slider_x, slider_y]) window = pygui.Window("Move Object", (600, 50), (250, 200), elements=[label_x, label_y, slider_x, slider_y, reset_button])
debug_window = pygui.Window("Info", (0, 0), (150, HEIGHT), elements=[fps_label, position_label, color_label, knob], fixed=True) debug_window = pygui.Window("Info", (0, 0), (150, HEIGHT), elements=[fps_label, position_label, color_label, knob], fixed=True)
# Add windows to the global window stack # Add windows to the global window stack
@ -40,7 +41,6 @@ window_stack.append(debug_window)
object_pos = [WIDTH // 2, HEIGHT // 2] object_pos = [WIDTH // 2, HEIGHT // 2]
object_size = (50, 50) object_size = (50, 50)
DEBUG = True DEBUG = True
def handle_window_focus(): def handle_window_focus():
@ -72,14 +72,24 @@ def draw_gui():
object_pos[0] = int(slider_x.value) # Update x position based on the x-slider object_pos[0] = int(slider_x.value) # Update x position based on the x-slider
object_pos[1] = int(slider_y.value) # Update y position based on the y-slider object_pos[1] = int(slider_y.value) # Update y position based on the y-slider
# Check if the reset button is clicked
if reset_button.is_clicked:
# Reset the object position
object_pos[0] = WIDTH // 2 # Reset X position
object_pos[1] = HEIGHT // 2 # Reset Y position
# Reset the sliders to the center position as well
slider_x.set_value(WIDTH // 2)
slider_y.set_value(HEIGHT // 2)
# Update and display FPS # Update and display FPS
fps_label.text = "FPS: " + str(round(clock.get_fps(), 1)) fps_label.text = "FPS: " + str(round(clock.get_fps(), 1))
position_label.text = "POS: " + str(object_pos) position_label.text = "POS: " + str(object_pos)
color_label.text = "COL: " + str(((20 * knob.angle/ 10), 255-(20 * knob.angle/ 10), 0)) color_label.text = "COL: " + str(((knob.angle), 255-(knob.angle), 0))
while True: while True:
screen.fill((30, 30, 30)) # Clear screen with a dark gray color screen.fill((0, 0, 0)) # Clear screen with a dark gray color
event_list = pygame.event.get() event_list = pygame.event.get()
for event in event_list: for event in event_list:
@ -90,18 +100,13 @@ while True:
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_F3: if event.key == pygame.K_F3:
DEBUG = not DEBUG DEBUG = not DEBUG
# Draw the object (rectangle) # Draw the object (rectangle)
pygame.draw.rect(screen, ((20 * knob.angle/ 10), 255-(20 * knob.angle/ 10), 0), (*object_pos, *object_size)) pygame.draw.rect(screen, ((knob.angle), (255-knob.angle), 0), (*object_pos, *object_size))
# Draw the GUI # Draw the GUI
if DEBUG: if DEBUG:
draw_gui() draw_gui()
pygame.display.flip() pygame.display.flip()
clock.tick() clock.tick(60)