-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenemy.py
More file actions
55 lines (50 loc) · 2.07 KB
/
enemy.py
File metadata and controls
55 lines (50 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import pygame
import random
import config
from assets import Assets
import os
import math
class Enemy:
def __init__(self, screen_width, screen_height, player_pos):
# Load dragon sprite as a static image
dragon_path = Assets.DRAGON
if os.path.exists(dragon_path):
image = pygame.image.load(dragon_path).convert_alpha()
self.image = pygame.transform.smoothscale(image, (256, 256))
else:
self.image = pygame.Surface((128, 128))
self.image.fill((255, 0, 0))
# Spawn at least 400px away from player
while True:
x = random.randint(100, screen_width - 356)
y = random.randint(100, screen_height - 356)
if math.hypot(x - player_pos[0], y - player_pos[1]) > 400:
break
self.rect = self.image.get_rect(topleft=(x, y))
self.health = config.ENEMY_HEALTH
self.max_health = config.ENEMY_HEALTH
self.speed = config.ENEMY_SPEED
# For future: projectiles
self.projectiles = []
def move_towards(self, target_rect):
if self.rect.x < target_rect.x: self.rect.x += self.speed
if self.rect.x > target_rect.x: self.rect.x -= self.speed
if self.rect.y < target_rect.y: self.rect.y += self.speed
if self.rect.y > target_rect.y: self.rect.y -= self.speed
def draw(self, surface):
surface.blit(self.image, self.rect)
# Draw health bar above dragon
bar_width = 60
bar_height = 8
bar_x = self.rect.centerx - bar_width // 2
bar_y = self.rect.top - 20
# Background (dark red)
pygame.draw.rect(surface, (100, 0, 0), (bar_x, bar_y, bar_width, bar_height))
# Health (bright red)
health_width = int((self.health / self.max_health) * bar_width)
if health_width > 0:
pygame.draw.rect(surface, (255, 0, 0), (bar_x, bar_y, health_width, bar_height))
# Border
pygame.draw.rect(surface, (255, 255, 255), (bar_x, bar_y, bar_width, bar_height), 1)
def is_dead(self):
return self.health <= 0