summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-03-16 19:25:16 +0200
committerViljami Ilola <+@hix.fi>2024-03-16 19:25:16 +0200
commit6a9f8ab2b47ab9dd19b7ae0e507c16bb919de975 (patch)
tree2abbbb9d1213472e2eda3e86ed8e5275323fd0b6
parent3d1e8a068f98a32e0146d8e9ab58dea49fbb4c74 (diff)
add enemies
-rw-r--r--src/sliceitoff/enemies/__init__.py1
-rw-r--r--src/sliceitoff/enemies/enemies.py84
-rw-r--r--src/sliceitoff/main.py7
3 files changed, 90 insertions, 2 deletions
diff --git a/src/sliceitoff/enemies/__init__.py b/src/sliceitoff/enemies/__init__.py
new file mode 100644
index 0000000..2d00927
--- /dev/null
+++ b/src/sliceitoff/enemies/__init__.py
@@ -0,0 +1 @@
+from .enemies import Enemies
diff --git a/src/sliceitoff/enemies/enemies.py b/src/sliceitoff/enemies/enemies.py
new file mode 100644
index 0000000..bed4a86
--- /dev/null
+++ b/src/sliceitoff/enemies/enemies.py
@@ -0,0 +1,84 @@
+import pygame
+from random import randrange
+
+from display import Scaling
+from images import Images
+
+class EnemySprite(pygame.sprite.Sprite):
+ def __init__(self):
+ super().__init__()
+ self.position = (0, 0)
+ self.movement = (0, 0)
+
+ def set_position(self, position: tuple):
+ self.position = position
+ self.update()
+
+ def set_movement(self, movement: tuple):
+ self.movement = movement
+
+ def update(self, move = 0):
+ if move:
+ self.position = (
+ self.position[0] + self.movement[0] * move,
+ self.position[1] + self.movement[1] * move)
+ self.rect = pygame.Rect(
+ Scaling.scale_to_display(self.position),
+ self.image.get_size())
+
+ def force_right(self):
+ self.movement = (abs(self.movement[0]), self.movement[1])
+
+ def force_left(self):
+ self.movement = (-abs(self.movement[0]), self.movement[1])
+
+ def force_down(self):
+ self.movement = (self.movement[0], abs(self.movement[1]))
+
+ def force_up(self):
+ self.movement = (self.movement[0], -abs(self.movement[1]))
+
+class EnemyBall(EnemySprite):
+ def __init__(self):
+ super().__init__()
+ self.image = Images.surfaces['ball']
+ self.update()
+
+
+class Enemies():
+ def __init__(self, field = None, level = 2):
+ self.sprites = pygame.sprite.Group()
+ self.updated = True
+ self.level = level
+ self.emerge_enemies()
+ self.field = field
+
+ def emerge_enemies(self):
+ for _ in range(self.level * 3):
+ enemy = EnemyBall()
+ enemy.set_position( (randrange(0,38_000), randrange(0,26_000)) )
+ enemy.set_movement( (randrange(0,400)-200, randrange(0,400)-200) )
+ self.sprites.add(enemy)
+
+ def wall_hit(self, field, enemy):
+ if enemy.rect.x < field.rect.x:
+ enemy.force_right()
+ if enemy.rect.y < field.rect.y:
+ enemy.force_down()
+ if ( enemy.rect.x + enemy.rect.w >= field.rect.x + field.rect.w ):
+ enemy.force_left()
+ if ( enemy.rect.y + enemy.rect.h >= field.rect.y + field.rect.h ):
+ enemy.force_up()
+
+ def get_sprites(self):
+ self.sprites.update(move=1)
+ for enemy in self.sprites:
+ for field in self.field.sprites:
+ if field.rect.contains(enemy):
+ break
+ else:
+ for field in self.field.sprites:
+ if field.rect.colliderect(enemy):
+ self.wall_hit(field, enemy)
+
+ return self.sprites
diff --git a/src/sliceitoff/main.py b/src/sliceitoff/main.py
index 1e84d28..966f3a1 100644
--- a/src/sliceitoff/main.py
+++ b/src/sliceitoff/main.py
@@ -6,6 +6,7 @@ from display import Display
from status import Status
from player import Player
from field import Field
+from enemies import Enemies
from images import Images
from game import Game
@@ -19,20 +20,22 @@ def main():
status = Status()
field = Field()
player = Player(field = field)
+ enemies = Enemies(field = field, level = 2)
game = Game(player=player)
clock = pygame.time.Clock()
- for _ in range(600):
+ for _ in range(6000):
if game.step():
break
display.update(
[
status.get_sprites(),
field.get_sprites(),
+ enemies.get_sprites(),
player.get_sprites()
])
- clock.tick(60)
+ clock.tick(120)
sleep(2)