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

Commit

Permalink
Restructure project
Browse files Browse the repository at this point in the history
  • Loading branch information
Virashu committed Feb 13, 2024
1 parent 45d982a commit 174ecc6
Show file tree
Hide file tree
Showing 17 changed files with 389 additions and 389 deletions.
2 changes: 1 addition & 1 deletion danmaku/animated.py → danmaku/game/animated.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pygame

from danmaku.gameobject import GameObject
from danmaku.game.gameobject import GameObject


class Animated(GameObject):
Expand Down
2 changes: 1 addition & 1 deletion danmaku/background.py → danmaku/game/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import vgame

from danmaku.animated import Animated
from danmaku.game.animated import Animated


class Background(Animated):
Expand Down
54 changes: 27 additions & 27 deletions danmaku/bullet.py → danmaku/game/bullet.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
"""Entity's bullet declaration."""

from danmaku.entity import Entity
from danmaku.database import get_bullet_type


class Bullet(Entity):
"""Bullet object."""

def __init__(
self, xy: tuple[int | float, int | float], damage: int | float, object_type
):
args = get_bullet_type(object_type)
super().__init__(
xy,
(args["texture_size"][0], args["texture_size"][1]),
args["speed"],
0,
damage,
)
self.enemy = args["enemy"]
self.vx, self.vy = args["vx_vy"]
self.hitbox_radius = args["hitbox_radius"]

self.texture_file = args["texture_file"]
self.texture_size = args["texture_size"]
self.my_type = object_type
"""Entity's bullet declaration."""

from danmaku.game.entity import Entity
from danmaku.database import get_bullet_type


class Bullet(Entity):
"""Bullet object."""

def __init__(
self, xy: tuple[int | float, int | float], damage: int | float, object_type
):
args = get_bullet_type(object_type)
super().__init__(
xy,
(args["texture_size"][0], args["texture_size"][1]),
args["speed"],
0,
damage,
)
self.enemy = args["enemy"]
self.vx, self.vy = args["vx_vy"]
self.hitbox_radius = args["hitbox_radius"]

self.texture_file = args["texture_file"]
self.texture_size = args["texture_size"]
self.my_type = object_type
2 changes: 1 addition & 1 deletion danmaku/drop.py → danmaku/game/drop.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Entity's bullet declaration."""

from danmaku.gameobject import GameObject
from danmaku.game.gameobject import GameObject

# from danmaku.database import ...

Expand Down
218 changes: 109 additions & 109 deletions danmaku/enemy.py → danmaku/game/enemy.py
Original file line number Diff line number Diff line change
@@ -1,109 +1,109 @@
"""Enemy object declaration."""

from random import randint, choices
from math import sin, cos, pi

from danmaku.animated import Animated
from danmaku.bullet import Bullet
from danmaku.database import get_enemy_type
from danmaku.shooter import Shooter
from danmaku.drop import PowerUp, Points


class Enemy(Shooter, Animated):
"""Enemy object."""

def __init__(
self,
xy: tuple[int | float, int | float],
object_type: str,
start_hp: int | float = 0,
):
args = get_enemy_type(object_type)

health = start_hp or args["hp"]

super().__init__(
xy,
args["texture_size"],
args["speed"],
health,
args["dm"],
args["endurance"],
"basic enemy bullet",
0,
args["shoot_v"] / 1000,
hitbox_radius=args["texture_size"][0] // 2,
direction=(0, 1),
)

frames = list(map(lambda x: f"/enemy/{x}", args["texture_file"].split(";")))
Animated.__init__(
self, xy, args["texture_size"], args["speed"], frames, 0, period=0.1
)
self.texture_size = args["texture_size"]
self.my_type = object_type
self.cost = args["cost"]

self.vx, self.vy = 0, 1

def shoot(self) -> list[Bullet]:
if self.can_shoot():
match self.my_type:
case "boss":
if randint(0, 6) == 0:
return self.shoot_cluster()
return self.shoot_radial(waves=2, base_angle=randint(0, 359))
case "basic enemy":
bullet = Bullet((self.x, self.y), self.damage, self.bullet_type)
bullet.vx = randint(-100, 100) / 100
bullet.vy = (1 - bullet.vx**2) ** 0.5
return [bullet]
case "strong enemy":
return self.shoot_radial(waves=3, n=5, base_angle=randint(0, 359))
return []

def shoot_radial(self, base_angle=0, angle_step=0, waves=1, n=6) -> list[Bullet]:
"""Shoot circle of bullets"""

bullets = []

for wave in range(waves):
first_angle = base_angle + wave * angle_step
for add_angle in range(0, 360, 360 // n):
angle = pi * ((first_angle + add_angle) % 360) / 180
bullet = Bullet((self.x, self.y), self.damage, self.bullet_type)
bullet.vx = cos(angle)
bullet.vy = sin(angle)
for _ in range(wave):
bullet.update(0.3)
bullets.append(bullet)
return bullets

def shoot_cluster(self, waves=1, n=10, base_angle=0, arc=180):
bullets = []
for wave in range(waves):
for i, a in enumerate(range(0, arc, arc // n)):
angle = pi * ((base_angle + a) % 360) / 180
bullet = Bullet((self.x, self.y), self.damage, self.bullet_type)
bullet.vx = cos(angle) * (i + 1) * 0.2
bullet.vy = sin(angle) * (i + 1) * 0.2
for _ in range(wave):
bullet.update(0.3)
bullets.append(bullet)
return bullets

def generate_drops(self) -> list:
drops = []
count = 1
if self.my_type == "boss":
count = 5
for _ in range(count):
pos = (self.x + randint(-10, 10), self.y + randint(-10, 10))
match choices(("powerup", "points", None), (1, 1, 2))[0]:
case "powerup":
drops.append(PowerUp(pos))
case "points":
drops.append(Points(pos))

return drops
"""Enemy object declaration."""

from random import randint, choices
from math import sin, cos, pi

from danmaku.game.animated import Animated
from danmaku.game.bullet import Bullet
from danmaku.database import get_enemy_type
from danmaku.game.shooter import Shooter
from danmaku.game.drop import PowerUp, Points


class Enemy(Shooter, Animated):
"""Enemy object."""

def __init__(
self,
xy: tuple[int | float, int | float],
object_type: str,
start_hp: int | float = 0,
):
args = get_enemy_type(object_type)

health = start_hp or args["hp"]

super().__init__(
xy,
args["texture_size"],
args["speed"],
health,
args["dm"],
args["endurance"],
"basic enemy bullet",
0,
args["shoot_v"] / 1000,
hitbox_radius=args["texture_size"][0] // 2,
direction=(0, 1),
)

frames = list(map(lambda x: f"/enemy/{x}", args["texture_file"].split(";")))
Animated.__init__(
self, xy, args["texture_size"], args["speed"], frames, 0, period=0.1
)
self.texture_size = args["texture_size"]
self.my_type = object_type
self.cost = args["cost"]

self.vx, self.vy = 0, 1

def shoot(self) -> list[Bullet]:
if self.can_shoot():
match self.my_type:
case "boss":
if randint(0, 6) == 0:
return self.shoot_cluster()
return self.shoot_radial(waves=2, base_angle=randint(0, 359))
case "basic enemy":
bullet = Bullet((self.x, self.y), self.damage, self.bullet_type)
bullet.vx = randint(-100, 100) / 100
bullet.vy = (1 - bullet.vx**2) ** 0.5
return [bullet]
case "strong enemy":
return self.shoot_radial(waves=3, n=5, base_angle=randint(0, 359))
return []

def shoot_radial(self, base_angle=0, angle_step=0, waves=1, n=6) -> list[Bullet]:
"""Shoot circle of bullets"""

bullets = []

for wave in range(waves):
first_angle = base_angle + wave * angle_step
for add_angle in range(0, 360, 360 // n):
angle = pi * ((first_angle + add_angle) % 360) / 180
bullet = Bullet((self.x, self.y), self.damage, self.bullet_type)
bullet.vx = cos(angle)
bullet.vy = sin(angle)
for _ in range(wave):
bullet.update(0.3)
bullets.append(bullet)
return bullets

def shoot_cluster(self, waves=1, n=10, base_angle=0, arc=180):
bullets = []
for wave in range(waves):
for i, a in enumerate(range(0, arc, arc // n)):
angle = pi * ((base_angle + a) % 360) / 180
bullet = Bullet((self.x, self.y), self.damage, self.bullet_type)
bullet.vx = cos(angle) * (i + 1) * 0.2
bullet.vy = sin(angle) * (i + 1) * 0.2
for _ in range(wave):
bullet.update(0.3)
bullets.append(bullet)
return bullets

def generate_drops(self) -> list:
drops = []
count = 1
if self.my_type == "boss":
count = 5
for _ in range(count):
pos = (self.x + randint(-10, 10), self.y + randint(-10, 10))
match choices(("powerup", "points", None), (1, 1, 2))[0]:
case "powerup":
drops.append(PowerUp(pos))
case "points":
drops.append(Points(pos))

return drops
2 changes: 1 addition & 1 deletion danmaku/entity.py → danmaku/game/entity.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Base class for alive objects"""

from danmaku.gameobject import GameObject
from danmaku.game.gameobject import GameObject


class Entity(GameObject):
Expand Down
14 changes: 7 additions & 7 deletions danmaku/game.py → danmaku/game/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import pygame

from danmaku.utils import not_in_border, resource_path
from danmaku.enemy import Enemy
from danmaku.player import Player
from danmaku.bullet import Bullet
from danmaku.game.enemy import Enemy
from danmaku.game.player import Player
from danmaku.game.bullet import Bullet
from danmaku.database import (
get_saved_objects,
get_saved_game,
Expand All @@ -16,10 +16,10 @@
delete_saved_objects,
get_settings,
)
from danmaku.pause import Pause
from danmaku.background import Background
from danmaku.drop import Drop, PowerUp, Points
from danmaku.level import Level, Stage, BossStage
from danmaku.game.pause import Pause
from danmaku.game.background import Background
from danmaku.game.drop import Drop, PowerUp, Points
from danmaku.game.level import Level, Stage, BossStage


# pylint: disable=attribute-defined-outside-init, missing-class-docstring
Expand Down
Loading

0 comments on commit 174ecc6

Please sign in to comment.