added now functional cannon needs projectiles (and alot more)
This commit is contained in:
+55
-2
@@ -2,7 +2,7 @@ from game.util import *
|
||||
import pygame as pg
|
||||
import math
|
||||
|
||||
class Enemy(pg.sprite.Group):
|
||||
class Enemy(pg.sprite.Sprite):
|
||||
def __init__(self, game, speed, health):
|
||||
super().__init__()
|
||||
self.game = game
|
||||
@@ -23,6 +23,12 @@ class Enemy(pg.sprite.Group):
|
||||
|
||||
self.rect.center = self.pos
|
||||
|
||||
self.healthbar = HealthBar(
|
||||
18,
|
||||
5,
|
||||
self.health
|
||||
)
|
||||
|
||||
def update(self):
|
||||
self.move()
|
||||
self.rotate()
|
||||
@@ -71,4 +77,51 @@ class Enemy(pg.sprite.Group):
|
||||
scaled_pos = camera.world_to_screen(self.pos)
|
||||
scaled_rect.center = scaled_pos
|
||||
|
||||
surf.blit(scaled_image, scaled_rect)
|
||||
surf.blit(scaled_image, scaled_rect)
|
||||
self.healthbar.render(
|
||||
surf,
|
||||
camera,
|
||||
self.pos,
|
||||
self.rect.height
|
||||
)
|
||||
|
||||
def take_damage(self, amount):
|
||||
self.health -= amount
|
||||
self.healthbar.hp = self.health
|
||||
if self.health <= 0:
|
||||
self.kill()
|
||||
|
||||
class EnemyGroup(pg.sprite.Group):
|
||||
def render(self, surf, camera):
|
||||
for enemy in self.sprites():
|
||||
enemy.render(surf, camera)
|
||||
|
||||
def update(self):
|
||||
for enemy in self.sprites():
|
||||
enemy.update()
|
||||
|
||||
class HealthBar():
|
||||
def __init__(self, w, h, max_hp):
|
||||
self.w = w
|
||||
self.h = h
|
||||
self.hp = max_hp
|
||||
self.max_hp = max_hp
|
||||
|
||||
def render(self, surf, camera, pos, enemy_height):
|
||||
ratio = self.hp / self.max_hp
|
||||
|
||||
world_pos = (
|
||||
pos.x - self.w / 2,
|
||||
pos.y + enemy_height / 2 -20
|
||||
)
|
||||
|
||||
screen_pos = camera.world_to_screen(world_pos)
|
||||
|
||||
scaled_w = self.w * camera.zoom
|
||||
scaled_h = self.h * camera.zoom
|
||||
|
||||
bg_rect = pg.Rect(screen_pos[0], screen_pos[1], scaled_w, scaled_h)
|
||||
fg_rect = pg.Rect(screen_pos[0], screen_pos[1], scaled_w * ratio, scaled_h)
|
||||
|
||||
pg.draw.rect(surf, "red", bg_rect)
|
||||
pg.draw.rect(surf, "green", fg_rect)
|
||||
|
||||
Reference in New Issue
Block a user