added a new dial

This commit is contained in:
Nicholas Dyer 2023-02-19 23:55:41 -05:00
parent ec3c3b66ed
commit 778c373809
No known key found for this signature in database
GPG Key ID: E4E6388793FA2105
2 changed files with 58 additions and 12 deletions

View File

@ -2,18 +2,21 @@ import pygame
import os import os
import thermopi.surfaces as surfaces import thermopi.surfaces as surfaces
# Global variables
SCREEN_SIZE = (720, 720) SCREEN_SIZE = (720, 720)
DIAL_SIZE = int(min(SCREEN_SIZE[0], SCREEN_SIZE[1]) * 0.9)
FPS = 60 FPS = 60
PI = 3.14159265 PI = 3.14159265
VALID_SURFACES = [ VALID_SURFACES = [
'dial' '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() pygame.init()
clock = pygame.time.Clock() clock = pygame.time.Clock()
if windowed_mode: if windowed_mode:
@ -21,24 +24,23 @@ def main(windowed_mode):
else: else:
window = pygame.display.set_mode(SCREEN_SIZE, pygame.FULLSCREEN) 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) surface = getattr(globals()['surfaces'], 'dial').Surface(SCREEN_SIZE)
running = True running = True
while running: while running:
clock.tick(FPS) clock.tick(FPS)
surface.update()
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
window.blit(surface, surface.get_rect()) window.blit(surface, surface.get_rect())
if not surface.running: if not surface.running:
if surface.quit:
break
# Switch the surface to a new surface
next_surface = surface.next_surface next_surface = surface.next_surface
if next_surface not in VALID_SURFACES: if next_surface not in VALID_SURFACES:
raise Exception('Given surface is not a valid surface!') raise Exception('Given surface is not a valid surface!')
surface = getattr(globals()['surfaces'], next_surface).Surface(SCREEN_SIZE) surface = getattr(globals()['surfaces'], next_surface).Surface(SCREEN_SIZE)
pygame.display.flip()
pygame.quit() pygame.quit()

View File

@ -1,11 +1,55 @@
import math
import pygame 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): class Surface(pygame.Surface):
def __init__(self, window_size): def __init__(self, window_size):
super().__init__(window_size, pygame.SRCALPHA) super().__init__(window_size, pygame.SRCALPHA)
self.running = True self.running = True
self.quit = False
self.next_surface = '' 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): 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()