diff --git a/assets/DataBase.db b/assets/DataBase.db index c7769f5..9040098 100644 Binary files a/assets/DataBase.db and b/assets/DataBase.db differ diff --git a/danmaku/database/construct.py b/danmaku/database/construct.py index 81326a7..592f9cb 100644 --- a/danmaku/database/construct.py +++ b/danmaku/database/construct.py @@ -114,5 +114,6 @@ hp=1300, dm=500, endurance=1, + hitbox_radius=10, ) player.save() diff --git a/danmaku/database/database.py b/danmaku/database/database.py index eff2ee9..eb8ce89 100644 --- a/danmaku/database/database.py +++ b/danmaku/database/database.py @@ -44,6 +44,7 @@ def get_player_type(name) -> dict: "hp": a.hp, "dm": a.dm, "endurance": a.endurance, + "hitbox_radius": a.hitbox_radius, } diff --git a/danmaku/database/models.py b/danmaku/database/models.py index 1e22f1b..6a7753f 100644 --- a/danmaku/database/models.py +++ b/danmaku/database/models.py @@ -55,6 +55,7 @@ class PlayerTypes(BaseModel): hp = IntegerField() dm = IntegerField() endurance = FloatField() + hitbox_radius = IntegerField() class SavedObjects(BaseModel): diff --git a/danmaku/game.py b/danmaku/game.py index eb370d4..0ee5a3e 100644 --- a/danmaku/game.py +++ b/danmaku/game.py @@ -198,7 +198,7 @@ def update(self): def draw(self): self.graphics.draw_sprite(self.background_object) - self.graphics.draw_sprite(self.player) + self.player.draw(self.graphics) for enemy in self.enemies: self.graphics.draw_sprite(enemy) diff --git a/danmaku/gameobject.py b/danmaku/gameobject.py index bd19759..370db1c 100644 --- a/danmaku/gameobject.py +++ b/danmaku/gameobject.py @@ -10,6 +10,8 @@ class GameObject(Sprite): A base game entity object. """ + hitbox_radius: int + def __init__( self, xy: tuple[int | float, int | float], @@ -31,9 +33,10 @@ def __init__( def update(self, delta: int | float): self.x += self.vx * delta * self.speed self.y += self.vy * delta * self.speed + self.rect.x, self.rect.y, self.rect.w, self.rect.h = ( - self.x, - self.y, + self.x - self.width / 2, + self.y - self.height / 2, self.width, self.height, ) diff --git a/danmaku/player.py b/danmaku/player.py index f6104d2..3840358 100644 --- a/danmaku/player.py +++ b/danmaku/player.py @@ -54,6 +54,7 @@ def __init__( self.last_shoot = 0 self.shoot_v = args["shoot_v"] self.score = 0 + self.hitbox_radius = args["hitbox_radius"] self.left = self.top = 0 self.right = self.bottom = 10e6 @@ -62,7 +63,7 @@ def shoot(self) -> list[Bullet]: t = pygame.time.get_ticks() if t - self.last_shoot >= self.shoot_v: bullet = Bullet( - (self.x + (self.width // 2), self.y + (self.height // 2)), + (self.x, self.y), self.damage, "basic player bullet", ) @@ -85,16 +86,20 @@ def set_bounds( def update(self, delta: int | float) -> None: self.x += self.vx * delta * self.speed - self.x = constrain(self.x, self.left, self.right - self.width) + self.x = constrain( + self.x, self.left + self.width / 2, self.right - self.width / 2 + ) self.y += self.vy * delta * self.speed - self.y = constrain(self.y, self.top, self.bottom - self.height) + self.y = constrain( + self.y, self.top + self.height / 2, self.bottom - self.height / 2 + ) self.rect.x, self.rect.y, self.rect.w, self.rect.h = ( - self.x, - self.y, - self.width, - self.height, + int(self.x - self.width / 2), + int(self.y - self.height / 2), + int(self.width), + int(self.height), ) def animation(self) -> None: @@ -119,8 +124,16 @@ def animation(self) -> None: def draw(self, graphics: vgame.graphics.Graphics) -> None: graphics.draw_sprite(self) + graphics.circle((self.x, self.y), self.hitbox_radius, (255, 255, 255)) def collision(self, other) -> bool: - e = pygame.Rect(other.x - other.r, other.y - other.r, 2 * other.r, 2 * other.r) - s = pygame.Rect(self.x, self.y, self.width, self.height) - return e.colliderect(s) + other_rect = pygame.Rect( + other.x - other.r, other.y - other.r, 2 * other.r, 2 * other.r + ) + self_rect = pygame.Rect( + self.x - self.hitbox_radius, + self.y - self.hitbox_radius, + self.hitbox_radius * 2, + self.hitbox_radius * 2, + ) + return other_rect.colliderect(self_rect) diff --git a/docs/mecs.md b/docs/mecs.md index b179d86..5f65224 100644 --- a/docs/mecs.md +++ b/docs/mecs.md @@ -40,3 +40,31 @@ graph LR ``` As we can see, all other scenes return to the main menu + + + +## Position, hitboxes + + +Position of an object is a position of it's center point + +*placement:* +``` +/-----\ +| | +| * | +| | +\-----/ +``` +(where the star is at) + +**NOT** coordinates of left top corner + +*wrong placement:* +``` +*-----\ +| | +| | +| | +\-----/ +```