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

Commit

Permalink
Improve enemies
Browse files Browse the repository at this point in the history
  • Loading branch information
Virashu committed Feb 11, 2024
1 parent e208423 commit 718ec2b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 44 deletions.
Binary file modified assets/DataBase.db
Binary file not shown.
10 changes: 5 additions & 5 deletions danmaku/database/construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
texture_size_height=65,
speed=30,
shoot_v=1500,
hp=250,
hp=150,
dm=50,
endurance=1,
cost=50,
Expand All @@ -36,8 +36,8 @@
texture_size_width=50,
texture_size_height=65,
speed=20,
shoot_v=1000,
hp=250,
shoot_v=7000, # ms
hp=200,
dm=50,
endurance=1,
cost=100,
Expand All @@ -52,9 +52,9 @@
texture_size_height=85,
speed=10,
shoot_v=500,
hp=550,
hp=500,
dm=70,
endurance=3,
endurance=2,
cost=500,
)
boss.save()
Expand Down
67 changes: 52 additions & 15 deletions danmaku/enemy.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Enemy object declaration."""

from random import randint
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):
Expand Down Expand Up @@ -48,25 +49,61 @@ def __init__(

def shoot(self) -> list[Bullet]:
if self.can_shoot():
if self.my_type == "boss":
return self.shoot_radial()
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]
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)
return []

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

bullets = []

a = randint(0, 359)
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

for i in range(0, 360, 60):
angle = pi * ((a + i) % 360) / 180
bullet = Bullet((self.x, self.y), self.damage, self.bullet_type)
bullet.vx = cos(angle)
bullet.vy = sin(angle)
bullets.append(bullet)
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)
bullet.vy = sin(angle) * (i + 1)
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
8 changes: 1 addition & 7 deletions danmaku/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,9 @@ def update_game(self):
enemy.get_damage(bullet.damage)
if enemy.health <= 0:
self.player.score += enemy.cost
x, y = enemy.x, enemy.y
self.drops += enemy.generate_drops()
self.enemies.remove(enemy)

match random.choices(("powerup", "points", None), (1, 1, 2))[0]:
case "powerup":
self.drops.append(PowerUp((x, y)))
case "points":
self.drops.append(Points((x, y)))

self.bullets.remove(bullet)
break

Expand Down
35 changes: 18 additions & 17 deletions danmaku/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,6 @@ def __init__(
args["shoot_v"] / 1000,
)

self.my_type = object_type
self.score = 0
self.power = 1

self.hitbox_radius = args["hitbox_radius"]
self.slow = False

# Bounds
self.left = self.top = 0
self.right = self.bottom = 10e6

# Animation
files = args["texture_file"].split(";")
self.animation_frames = {
Expand All @@ -50,6 +39,7 @@ def __init__(
Direction.UP: [],
Direction.DOWN: [],
}

for i in files:
path = f"/player/{i}"
if "left" in i:
Expand All @@ -70,6 +60,17 @@ def __init__(
]
self.texture_size = args["texture_size"]

self.my_type = object_type
self.score = 0
self.power = 1

self.hitbox_radius = args["hitbox_radius"]
self.slow = False

# Bounds
self.left = self.top = 0
self.right = self.bottom = 10e6

def shoot(self) -> list[Bullet]:
res: list[Bullet] = []

Expand All @@ -84,8 +85,8 @@ def shoot(self) -> list[Bullet]:
res.append(bullet)

if self.power > 4:
vx = math.cos(math.pi * 75 / 180)
vy = -math.sin(math.pi * 75 / 180)
vx = math.cos(math.pi * 85 / 180)
vy = -math.sin(math.pi * 85 / 180)

b1 = Bullet(
(self.x, self.y),
Expand Down Expand Up @@ -142,9 +143,9 @@ def update(self, delta: int | float) -> None:
self.y, self.top + self.height / 2, self.bottom - self.height / 2
)

self.rect.x, self.rect.y, self.rect.w, self.rect.h = (
int(self.x - self.width / 2),
int(self.y - self.height / 2),
self.rect.centerx, self.rect.centery, self.rect.w, self.rect.h = (
int(self.x),
int(self.y),
int(self.width),
int(self.height),
)
Expand Down Expand Up @@ -172,4 +173,4 @@ def animate(self) -> None:
def draw(self, graphics: vgame.graphics.Graphics) -> None:
graphics.draw_sprite(self)
if self.slow:
graphics.circle((self.x, self.y), self.hitbox_radius, (255, 255, 255))
graphics.circle((self.x, self.y), self.hitbox_radius, (200, 0, 200))

0 comments on commit 718ec2b

Please sign in to comment.