summaryrefslogtreecommitdiff
path: root/src/sliceitoff/enemies/enemy.py
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-03-30 16:08:03 +0200
committerViljami Ilola <+@hix.fi>2024-03-30 16:08:03 +0200
commit1def24341ce4f3ab1c1de4a9dde27bf0b5fab179 (patch)
treee846c7ea2f4306146f33381c289350c2edf6f093 /src/sliceitoff/enemies/enemy.py
parent5874005e58d0f26b4393845b3b18160658ece160 (diff)
bounching balls and more refactoring
Diffstat (limited to 'src/sliceitoff/enemies/enemy.py')
-rw-r--r--src/sliceitoff/enemies/enemy.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/sliceitoff/enemies/enemy.py b/src/sliceitoff/enemies/enemy.py
new file mode 100644
index 0000000..08d75dd
--- /dev/null
+++ b/src/sliceitoff/enemies/enemy.py
@@ -0,0 +1,32 @@
+""" enemy.enemy - Enemy super class. Wall hitting and other commons """
+import pygame
+
+from display import Scaling
+
+class Enemy(pygame.sprite.Sprite):
+ """ Enemy super class. Just common movements. """
+ def __init__(self):
+ super().__init__()
+ self.position = (0, 0)
+ self.movement = (0, 0)
+ self.rect = None
+
+ def update(self, dt = 0, wall_hit = None):
+ """ hit walls, update position and rect """
+ if wall_hit:
+ if self.rect.x < wall_hit.x:
+ self.movement = (abs(self.movement[0]), self.movement[1])
+ if self.rect.y < wall_hit.y:
+ self.movement = (self.movement[0], abs(self.movement[1]))
+ if self.rect.x + self.rect.w >= wall_hit.x + wall_hit.w:
+ self.movement = (-abs(self.movement[0]), self.movement[1])
+ if self.rect.y + self.rect.h >= wall_hit.y + wall_hit.h:
+ self.movement = (self.movement[0], -abs(self.movement[1]))
+ return
+ if dt:
+ self.position = (
+ self.position[0] + self.movement[0] * dt,
+ self.position[1] + self.movement[1] * dt)
+ self.rect = pygame.Rect(
+ Scaling.scale_to_display(self.position),
+ self.image.get_size())