/
ap1k
/
PyGame_Practice
Обзор
Документация
Войти
/
ap1k
/
PyGame_Practice
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/entities/player.py
172 строки
7 KB
Roman
The all game
27 май 2025, 15:26
27 май 2025, 15:26
4ea7de9
Код
Авторство
О чём код?
""" Player class - represents the player entity in the game """ import pygame from src.entities.entity import Entity from src.utils.resource_loader import ResourceLoader from src.patterns.weapon_strategy import BasicWeapon, RapidWeapon, PowerWeapon class Player(Entity): """ Player entity class Implements the Entity interface """ def __init__(self, x, y): """Initialize the player entity""" super().__init__(x, y, 50, 50) # Load the player image self.image = ResourceLoader().get_image("player") # Player properties self.speed = 0 # Current speed (will be affected by acceleration) self.max_speed = 8 self.acceleration = 0.2 self.deceleration = 0.1 self.velocity_x = 0 self.velocity_y = 0 self.health = 100 self.lives = 3 self.invulnerable = False self.invulnerable_time = 0 self.invulnerable_duration = 2000 # 2 seconds of invulnerability after taking damage self.flash_timer = 0 self.flash_interval = 100 # Flash every 100ms when invulnerable self.visible = True # Weapon system using Strategy pattern self.weapons = [ BasicWeapon(self), RapidWeapon(self), PowerWeapon(self) ] self.current_weapon = 0 self.last_shot_time = 0 self.weapon_switch_time = 0 self.weapon_switch_cooldown = 300 # Cooldown between weapon switches def update(self): """Update the player state""" current_time = pygame.time.get_ticks() # Handle invulnerability if self.invulnerable: # Check if invulnerability has expired if current_time - self.invulnerable_time > self.invulnerable_duration: self.invulnerable = False self.visible = True else: # Flash the player when invulnerable if current_time - self.flash_timer > self.flash_interval: self.visible = not self.visible self.flash_timer = current_time # Get keyboard input keys = pygame.key.get_pressed() # Calculate acceleration based on input if keys[pygame.K_LEFT] or keys[pygame.K_a]: self.velocity_x = max(self.velocity_x - self.acceleration, -self.max_speed) elif keys[pygame.K_RIGHT] or keys[pygame.K_d]: self.velocity_x = min(self.velocity_x + self.acceleration, self.max_speed) else: # Apply deceleration when no input if self.velocity_x > 0: self.velocity_x = max(0, self.velocity_x - self.deceleration) elif self.velocity_x < 0: self.velocity_x = min(0, self.velocity_x + self.deceleration) if keys[pygame.K_UP] or keys[pygame.K_w]: self.velocity_y = max(self.velocity_y - self.acceleration, -self.max_speed) elif keys[pygame.K_DOWN] or keys[pygame.K_s]: self.velocity_y = min(self.velocity_y + self.acceleration, self.max_speed) else: # Apply deceleration when no input if self.velocity_y > 0: self.velocity_y = max(0, self.velocity_y - self.deceleration) elif self.velocity_y < 0: self.velocity_y = min(0, self.velocity_y + self.deceleration) # Move the player based on velocity self.x = max(0, min(800 - self.width, self.x + self.velocity_x)) self.y = max(0, min(600 - self.height, self.y + self.velocity_y)) # Update the player's rectangle position self.update_rect() # Switch weapons with number keys or mouse wheel if keys[pygame.K_1]: self.switch_weapon(0) elif keys[pygame.K_2]: self.switch_weapon(1) elif keys[pygame.K_3]: self.switch_weapon(2) # Check for weapon switching with Q and E keys if keys[pygame.K_q] and current_time - self.weapon_switch_time > self.weapon_switch_cooldown: self.switch_weapon((self.current_weapon - 1) % len(self.weapons)) self.weapon_switch_time = current_time elif keys[pygame.K_e] and current_time - self.weapon_switch_time > self.weapon_switch_cooldown: self.switch_weapon((self.current_weapon + 1) % len(self.weapons)) self.weapon_switch_time = current_time def render(self, screen): """Render the player to the screen""" if self.visible: screen.blit(self.image, self.rect) # Draw a colored outline around the player based on current weapon from src.utils.config import Config weapon_color = Config().weapon_colors[self.current_weapon] pygame.draw.rect(screen, weapon_color, self.rect, 2) # 2 pixel width outline def shoot(self): """Shoot a bullet using the current weapon""" # Get the current time current_time = pygame.time.get_ticks() # Check if enough time has passed since the last shot weapon = self.weapons[self.current_weapon] if current_time - self.last_shot_time > weapon.cooldown: # Shoot a bullet bullet = weapon.shoot() self.last_shot_time = current_time return bullet return None def take_damage(self, damage): """Take damage from an enemy or obstacle""" # Don't take damage if invulnerable if self.invulnerable: return False self.health -= damage # Play damage sound ResourceLoader().get_sound("explosion").play() # Make player invulnerable for a short time self.invulnerable = True self.invulnerable_time = pygame.time.get_ticks() # Check if the player has lost a life if self.health <= 0: self.lives -= 1 # Reset health if the player still has lives if self.lives > 0: self.health = 100 else: # Game over self.active = False return self.lives <= 0 # Return True if game over def switch_weapon(self, weapon_index): """Switch to the specified weapon""" if 0 <= weapon_index < len(self.weapons): if self.current_weapon != weapon_index: self.current_weapon = weapon_index # Play weapon switch sound ResourceLoader().get_sound("powerup").play()