From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Thu, 23 Nov 2023 22:53:22 +0000 (-0500) Subject: draggable point particles X-Git-Url: http://git.skullheadx.com/sitemap.xml?a=commitdiff_plain;h=52a27ed935c713c2e0525181ba0e6a6312bbc449;p=Electric-Dipoles.git draggable point particles --- diff --git a/main.py b/main.py index e1cf1df..2bc367d 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,28 @@ import pygame +from particle import PointParticle + SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Electric Dipoles") +is_running = True +clock = pygame.Clock() +delta = 0 + +particles = [PointParticle((SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2))] + +while is_running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + is_running = False + + for particle in particles: + particle.update(delta) + + screen.fill((255, 255, 255)) + for particle in particles: + particle.draw(screen) + + pygame.display.flip() + delta = clock.tick() diff --git a/particle.py b/particle.py new file mode 100644 index 0000000..f4dfa35 --- /dev/null +++ b/particle.py @@ -0,0 +1,26 @@ +import pygame + + +class PointParticle: + + def __init__(self, position, charge=1.6e-19): + self.position = pygame.Vector2(position) + self.mouse_offset = pygame.Vector2(0, 0) + self.charge = charge + self.radius = 50 + self.dragging = False + self.colour = (255, 0, 0) if self.charge < 0 else (0, 0, 255) + + def update(self, delta): + if pygame.mouse.get_pressed(3)[0]: + if not self.dragging and self.position.distance_to(pygame.mouse.get_pos()) < self.radius: + self.mouse_offset = pygame.Vector2(pygame.mouse.get_pos()) - self.position + self.dragging = True + if self.dragging: + self.position = pygame.Vector2(pygame.mouse.get_pos()) - self.mouse_offset + elif self.dragging: + self.dragging = False + + def draw(self, surf): + pygame.draw.circle(surf, self.colour, self.position, self.radius) + pygame.draw.circle(surf, (0, 0, 0), self.position, self.radius, 3)