Skip to content
This repository has been archived by the owner on Feb 18, 2025. It is now read-only.

Commit

Permalink
Move coordinates to center, read mecs.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Virashu committed Feb 5, 2024
1 parent f4822ae commit 162de00
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 13 deletions.
Binary file modified assets/DataBase.db
Binary file not shown.
1 change: 1 addition & 0 deletions danmaku/database/construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,6 @@
hp=1300,
dm=500,
endurance=1,
hitbox_radius=10,
)
player.save()
1 change: 1 addition & 0 deletions danmaku/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def get_player_type(name) -> dict:
"hp": a.hp,
"dm": a.dm,
"endurance": a.endurance,
"hitbox_radius": a.hitbox_radius,
}


Expand Down
1 change: 1 addition & 0 deletions danmaku/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class PlayerTypes(BaseModel):
hp = IntegerField()
dm = IntegerField()
endurance = FloatField()
hitbox_radius = IntegerField()


class SavedObjects(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion danmaku/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions danmaku/gameobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class GameObject(Sprite):
A base game entity object.
"""

hitbox_radius: int

def __init__(
self,
xy: tuple[int | float, int | float],
Expand All @@ -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,
)
Expand Down
33 changes: 23 additions & 10 deletions danmaku/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
)
Expand All @@ -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:
Expand All @@ -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)
28 changes: 28 additions & 0 deletions docs/mecs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:*
```
*-----\
| |
| |
| |
\-----/
```

0 comments on commit 162de00

Please sign in to comment.