From 1def24341ce4f3ab1c1de4a9dde27bf0b5fab179 Mon Sep 17 00:00:00 2001 From: Viljami Ilola <+@hix.fi> Date: Sat, 30 Mar 2024 16:08:03 +0200 Subject: bounching balls and more refactoring --- pyproject.toml | 2 +- src/sliceitoff/enemies/ball.py | 27 ++++++++++++++++++ src/sliceitoff/enemies/bouncher.py | 36 ++++++++++++++++++++++++ src/sliceitoff/enemies/enemies.py | 56 +++++++++----------------------------- src/sliceitoff/enemies/enemy.py | 32 ++++++++++++++++++++++ src/sliceitoff/status/__init__.py | 3 +- src/sliceitoff/status/status.py | 2 +- 7 files changed, 112 insertions(+), 46 deletions(-) create mode 100644 src/sliceitoff/enemies/ball.py create mode 100644 src/sliceitoff/enemies/bouncher.py create mode 100644 src/sliceitoff/enemies/enemy.py diff --git a/pyproject.toml b/pyproject.toml index e5c7888..295fa2e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "sliceitoff" -version = "0.2-alpha" +version = "0.2-beta" description = "Arcade game where one slices play area off avoiding enemies." authors = ["Viljami Ilola <+@hix.fi>"] readme = "README.md" diff --git a/src/sliceitoff/enemies/ball.py b/src/sliceitoff/enemies/ball.py new file mode 100644 index 0000000..b8c8467 --- /dev/null +++ b/src/sliceitoff/enemies/ball.py @@ -0,0 +1,27 @@ +""" enemies.ball - Enemy type that goes straight line hitting walls """ +from random import randrange, choice + +from text import get_letter_surface +from .enemy import Enemy + +BALL_SPAWN_AREA = (0, 0, 300_000, 200_000) +BALL_MOVEMENT = (100, 100) +BALL_SYMBOLS = (0x1,0x2) +BALL_SIZE = 8_000 + +class EnemyBall(Enemy): + """ Basic type of enemy. """ + def __init__(self): + super().__init__() + self.position = ( + randrange(BALL_SPAWN_AREA[0], BALL_SPAWN_AREA[2]), + randrange(BALL_SPAWN_AREA[1], BALL_SPAWN_AREA[3])) + self.movement = ( + randrange(0, BALL_MOVEMENT[0]*2) - BALL_MOVEMENT[0], + randrange(0, BALL_MOVEMENT[1]*2) - BALL_MOVEMENT[1]) + font_key = ('8x8', BALL_SIZE, 0) + surface = get_letter_surface(font_key, choice(BALL_SYMBOLS)) + self.image = surface.subsurface( + (0, 0, surface.get_rect().w, surface.get_rect().w)) + self.update() + self.rect = None diff --git a/src/sliceitoff/enemies/bouncher.py b/src/sliceitoff/enemies/bouncher.py new file mode 100644 index 0000000..692492b --- /dev/null +++ b/src/sliceitoff/enemies/bouncher.py @@ -0,0 +1,36 @@ +""" enemies.bouncher - Enemy type that bouches around """ +from random import randrange, choice + +from text import get_letter_surface +from .enemy import Enemy + +BOUNCHER_SPAWN_AREA = (0, 0, 300_000, 80_000) +BOUNCHER_MOVEMENT = (200, 20) +BOUNCHER_SIZE = 12_000 +BOUNCHER_SYMBOLS = (0x1,0x2) +GRAVITY = 4 + +class EnemyBouncher(Enemy): + """ Type of enemy that is affected by gravity """ + def __init__(self): + super().__init__() + self.position = ( + randrange(BOUNCHER_SPAWN_AREA[0], BOUNCHER_SPAWN_AREA[2]), + randrange(BOUNCHER_SPAWN_AREA[1], BOUNCHER_SPAWN_AREA[3])) + self.movement = ( + randrange(0, BOUNCHER_MOVEMENT[0]*2) - BOUNCHER_MOVEMENT[0], + randrange(0, BOUNCHER_MOVEMENT[1]*2) - BOUNCHER_MOVEMENT[1]) + font_key = ('8x8', BOUNCHER_SIZE, 0) + surface = get_letter_surface(font_key, choice(BOUNCHER_SYMBOLS)) + self.image = surface.subsurface( + (0, 0, surface.get_rect().w, surface.get_rect().w)) + self.rect = None + self.update() + + def update(self, dt = 0, wall_hit = None, **kwargs): + """ wall hit from super(). added gravity """ + super().update(dt = dt, wall_hit = wall_hit, **kwargs) + if dt: + self.movement = ( + self.movement[0], + (self.movement[1] + GRAVITY)*0.999) diff --git a/src/sliceitoff/enemies/enemies.py b/src/sliceitoff/enemies/enemies.py index 41a19fa..c52059f 100644 --- a/src/sliceitoff/enemies/enemies.py +++ b/src/sliceitoff/enemies/enemies.py @@ -1,52 +1,22 @@ -import pygame from random import randrange +import pygame -from display import Scaling -from text import get_letter_surface - -BALL_SPAWN_AREA = (0, 0, 300_000, 200_000) -BALL_MOVEMENT = (100, 100) - -class EnemyBall(pygame.sprite.Sprite): - def __init__(self): - super().__init__() - self.position = ( - randrange(BALL_SPAWN_AREA[0], BALL_SPAWN_AREA[2]), - randrange(BALL_SPAWN_AREA[1], BALL_SPAWN_AREA[3])) - self.movement = ( - randrange(0, BALL_MOVEMENT[0]*2) - BALL_MOVEMENT[0], - randrange(0, BALL_MOVEMENT[1]*2) - BALL_MOVEMENT[1]) - self.image = get_letter_surface( - ('8x8', 8_000, 0), - randrange(1,3)).subsurface( - (0,0,8_000*Scaling.factor,8_000*Scaling.factor)) - self.update() - self.rect = None - - def update(self, dt = 0, wall_hit = None, **kwargs): - 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()) +from .ball import EnemyBall +from .bouncher import EnemyBouncher class Enemies(pygame.sprite.Group): def __init__(self, field = None, count = 0): super().__init__() - for _ in range(count): - self.add(EnemyBall()) + while count: + match randrange(0,4): + case 0|1|2: + if count >= 1: + self.add(EnemyBall()) + count -= 1 + case 3: + if count >= 2: + self.add(EnemyBouncher()) + count -= 2 self.field = field def update(self, field_rects = [], **kwargs): 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()) diff --git a/src/sliceitoff/status/__init__.py b/src/sliceitoff/status/__init__.py index 2a4c93e..68a41e1 100644 --- a/src/sliceitoff/status/__init__.py +++ b/src/sliceitoff/status/__init__.py @@ -1 +1,2 @@ -from .status import Status \ No newline at end of file +""" status - Statusline bottom of screen when level is going on """ +from .status import Status diff --git a/src/sliceitoff/status/status.py b/src/sliceitoff/status/status.py index bbe9a03..8a1a130 100644 --- a/src/sliceitoff/status/status.py +++ b/src/sliceitoff/status/status.py @@ -9,7 +9,7 @@ class Status(pygame.sprite.Group): super().__init__() self.stats = stats self.old_srt = None - + def update(self, **kwargs): """ Rebuilds statusline if needed """ super().update(**kwargs) -- cgit v1.2.3