From 778c373809932c2c6b0c9b2b2b529b43726e70b2 Mon Sep 17 00:00:00 2001 From: nickedyer Date: Sun, 19 Feb 2023 23:55:41 -0500 Subject: [PATCH] added a new dial --- thermopi/main.py | 24 ++++++++++---------- thermopi/surfaces/dial.py | 46 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/thermopi/main.py b/thermopi/main.py index 2137698..d0805ce 100644 --- a/thermopi/main.py +++ b/thermopi/main.py @@ -2,18 +2,21 @@ import pygame import os import thermopi.surfaces as surfaces +# Global variables SCREEN_SIZE = (720, 720) -DIAL_SIZE = int(min(SCREEN_SIZE[0], SCREEN_SIZE[1]) * 0.9) FPS = 60 PI = 3.14159265 VALID_SURFACES = [ 'dial' ] - -script_dir = os.path.dirname(os.path.abspath(__file__)) +SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) -def main(windowed_mode): +def main(windowed_mode=False): + """ + Main looping method. + :param windowed_mode: boolean to start the game in windowed mode + """ pygame.init() clock = pygame.time.Clock() if windowed_mode: @@ -21,24 +24,23 @@ def main(windowed_mode): else: window = pygame.display.set_mode(SCREEN_SIZE, pygame.FULLSCREEN) + # Starts the program with the surface 'dial' as the default surface = getattr(globals()['surfaces'], 'dial').Surface(SCREEN_SIZE) running = True while running: clock.tick(FPS) - - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_ESCAPE: - running = False + surface.update() window.blit(surface, surface.get_rect()) if not surface.running: + if surface.quit: + break + # Switch the surface to a new surface next_surface = surface.next_surface if next_surface not in VALID_SURFACES: raise Exception('Given surface is not a valid surface!') surface = getattr(globals()['surfaces'], next_surface).Surface(SCREEN_SIZE) + pygame.display.flip() pygame.quit() diff --git a/thermopi/surfaces/dial.py b/thermopi/surfaces/dial.py index 430d09e..711ec0d 100644 --- a/thermopi/surfaces/dial.py +++ b/thermopi/surfaces/dial.py @@ -1,11 +1,55 @@ +import math + import pygame +DIAL_SCALING = 0.9 + + +class Dial(pygame.Surface): + def __init__(self, dial_size): + super().__init__((dial_size, dial_size), pygame.SRCALPHA) + self.rect = self.get_rect() + self.dial_size = dial_size + self.center = (self.dial_size / 2, self.dial_size / 2) + + self.setting = 1 + + def update(self): + self.fill((0, 0, 0, 0)) + + pygame.draw.circle(self, (0, 255, 0), self.center, self.dial_size / 2, 5) + pygame.draw.circle(self, (64, 64, 64), self.center, self.dial_size / 2 * 0.95) + + angle = (1.25 - (1.5 * self.setting)) * math.pi + x_1 = (math.cos(angle) * self.dial_size / 2 * 0.8) + (self.dial_size / 2) + y_1 = (-1 * math.sin(angle) * self.dial_size / 2 * 0.8) + (self.dial_size / 2) + x_2 = (math.cos(angle) * self.dial_size / 2 * 0.99) + (self.dial_size / 2) + y_2 = (-1 * math.sin(angle) * self.dial_size / 2 * 0.99) + (self.dial_size / 2) + pygame.draw.line(self, (0, 255, 0), (x_1, y_1), (x_2, y_2), 10) + + def set_setting(self, new_setting): + self.setting = max(0, min(new_setting, 1)) class Surface(pygame.Surface): def __init__(self, window_size): super().__init__(window_size, pygame.SRCALPHA) self.running = True + self.quit = False self.next_surface = '' + dial_size = int(min(window_size[0], window_size[1]) * 0.9) + self.dial = Dial(dial_size) + self.dial.rect.center = (window_size[0] / 2, window_size[1] / 2) + def update(self): - self.fill((0, 0, 0)) + self.fill((32, 32, 32)) + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.running = False + self.quit = True + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: + self.quit = True + + self.blit(self.dial, self.dial.rect) + self.dial.update()