]> Skullheadx's Git Forge - Electric-Dipoles.git/commitdiff
draggable point particles
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Thu, 23 Nov 2023 22:53:22 +0000 (17:53 -0500)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Thu, 23 Nov 2023 22:53:22 +0000 (17:53 -0500)
main.py
particle.py [new file with mode: 0644]

diff --git a/main.py b/main.py
index e1cf1df0ec6dc8d69701c2c625d0490ecbe106ae..2bc367daf828050215559b82031127666a4ee7eb 100644 (file)
--- 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 (file)
index 0000000..f4dfa35
--- /dev/null
@@ -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)