/
ap1k
/
PyGame_Practice
Обзор
Документация
Войти
/
ap1k
/
PyGame_Practice
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/entities/enemy.py
151 строка
5 KB
Roman
The all game
27 май 2025, 15:26
27 май 2025, 15:26
4ea7de9
Код
Авторство
О чём код?
""" Enemy classes - represent different types of enemy entities in the game """ import random import pygame import math from src.entities.entity import Entity from src.utils.resource_loader import ResourceLoader class Enemy(Entity): """ Base enemy class Implements the Entity interface and serves as a base for specific enemy types """ def __init__(self, x, y, width, height, enemy_type): """Initialize the enemy entity""" super().__init__(x, y, width, height) # Load the enemy image self.image = ResourceLoader().get_image(f"enemy_{enemy_type}") # Enemy properties self.enemy_type = enemy_type self.speed = 2 self.health = 30 self.damage = 10 self.score_value = 10 def update(self): """Update the enemy state""" # Move the enemy downward self.y += self.speed # Update the enemy's rectangle position self.update_rect() # Deactivate the enemy if it goes off the bottom of the screen if self.y > 600: self.active = False def render(self, screen): """Render the enemy to the screen""" # Draw the enemy screen.blit(self.image, self.rect) # Draw health bar background health_bar_width = self.width health_bar_height = 5 health_bar_x = self.x health_bar_y = self.y - 10 # Position above the enemy # Make sure health bar is visible on screen if health_bar_y > 0: # Draw background (dark gray) pygame.draw.rect(screen, (64, 64, 64), (health_bar_x, health_bar_y, health_bar_width, health_bar_height)) # Calculate health percentage health_percent = self.health / self.get_max_health() # Choose color based on health percentage if health_percent > 0.7: color = (0, 255, 0) # Green elif health_percent > 0.3: color = (255, 255, 0) # Yellow else: color = (255, 0, 0) # Red # Draw health bar fill fill_width = int(health_bar_width * health_percent) if fill_width > 0: # Only draw if there's health left pygame.draw.rect(screen, color, (health_bar_x, health_bar_y, fill_width, health_bar_height)) def get_max_health(self): """Get the maximum health for this enemy type""" # Default implementation returns the initial health based on enemy type if self.enemy_type == "basic": return 30 elif self.enemy_type == "fast": return 15 elif self.enemy_type == "tank": return 60 return 30 # Default def take_damage(self, damage): """Take damage from a bullet""" self.health -= damage # Deactivate the enemy if it has no health left if self.health <= 0: self.active = False # Play explosion sound ResourceLoader().get_sound("explosion").play() return True # Enemy was destroyed return False # Enemy is still alive class BasicEnemy(Enemy): """ Basic enemy type with balanced stats """ def __init__(self, x, y): super().__init__(x, y, 30, 30, "basic") self.speed = 2 self.health = 30 self.damage = 10 self.score_value = 10 class FastEnemy(Enemy): """ Fast enemy type with high speed but low health """ def __init__(self, x, y): super().__init__(x, y, 30, 30, "fast") self.speed = 4 self.health = 15 self.damage = 5 self.score_value = 15 def update(self): """Update the fast enemy with zigzag movement""" # Move the enemy downward self.y += self.speed # Add zigzag movement self.x += 2 * math.sin(self.y / 20) # Keep the enemy within the screen bounds self.x = max(0, min(800 - self.width, self.x)) # Update the enemy's rectangle position self.update_rect() # Deactivate the enemy if it goes off the bottom of the screen if self.y > 600: self.active = False class TankEnemy(Enemy): """ Tank enemy type with high health but low speed """ def __init__(self, x, y): super().__init__(x, y, 40, 40, "tank") self.speed = 1 self.health = 60 self.damage = 15 self.score_value = 20