can now click and drag to move dial

This commit is contained in:
Nicholas Dyer 2023-02-19 20:48:28 -05:00
parent 36ca8638f6
commit 3e5c5cc896
No known key found for this signature in database
GPG Key ID: E4E6388793FA2105

View File

@ -1,5 +1,4 @@
import math import math
import pygame import pygame
import os import os
@ -23,24 +22,20 @@ class Dial(pygame.sprite.Sprite):
self.test = True self.test = True
def update(self): def update(self):
if self.test:
if self.test_cw:
self.position += 0.01
else:
self.position -= 0.01
if self.position > 1:
self.position = 1
self.test_cw = not self.test_cw
if self.position < 0:
self.position = 0
self.test_cw = not self.test_cw
self.rotation = 77 - (2 * 77.5 * self.position) self.rotation = 77 - (2 * 77.5 * self.position)
self.image = pygame.transform.rotate(self._base_image, self.rotation) self.image = pygame.transform.rotate(self._base_image, self.rotation)
self.rect = self.image.get_rect(center=self.rect.center) self.rect = self.image.get_rect(center=self.rect.center)
self.rect.center = (math.sin(math.radians(self.rotation)) * -300 + 360, self.rect.center = (math.sin(math.radians(self.rotation)) * -300 + 360,
math.cos(math.radians(self.rotation + 180)) * 300 + 360) math.cos(math.radians(self.rotation + 180)) * 300 + 360)
def set_test(self, test_value):
self.test = test_value def move_dial(self, movement_ammount):
self.position += movement_ammount
if self.position > 1:
self.position = 1
if self.position < 0:
self.position = 0
def main(windowed_mode): def main(windowed_mode):
pygame.init() pygame.init()
@ -66,10 +61,15 @@ def main(windowed_mode):
pygame.display.flip() pygame.display.flip()
running = True running = True
prev_mouse_pos = (0, 0)
mouse_pos = (0, 0)
get_mouse_speed = False
while running: while running:
clock.tick(FPS) clock.tick(FPS)
# pygame.mouse.set_visible(False) # pygame.mouse.set_visible(False)
prev_mouse_pos = mouse_pos
mouse_pos = pygame.mouse.get_pos()
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
@ -78,13 +78,19 @@ def main(windowed_mode):
if event.key == pygame.K_ESCAPE: if event.key == pygame.K_ESCAPE:
running = False running = False
if event.type == pygame.MOUSEBUTTONDOWN: if event.type == pygame.MOUSEBUTTONDOWN:
dial.set_test(True) get_mouse_speed = True
if event.type == pygame.MOUSEBUTTONUP: if event.type == pygame.MOUSEBUTTONUP:
dial.set_test(False) get_mouse_speed = False
mouse_pos = pygame.mouse.get_pos()
draw() draw()
if get_mouse_speed:
mouse_diff = (prev_mouse_pos[0] - mouse_pos[0], prev_mouse_pos[1] - mouse_pos[1])
movement_speed = math.sqrt((mouse_diff[0] ** 2) + (mouse_diff[1] ** 2))
mouse_angle = math.atan2(mouse_pos[0] - 360, mouse_pos[1] - 360)
diff_angle = math.atan2(mouse_diff[0], mouse_diff[1])
speed_coeff = math.cos((mouse_angle - (0.5 * PI)) - diff_angle)
final_speed = movement_speed * speed_coeff
dial.move_dial(final_speed / -5000.0)
pygame.quit() pygame.quit()