Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from settings import *
- class Player:
- def __init__(self):
- self.width = 40
- self.height = 30
- self.x = SCREEN_WIDTH // 2
- self.y = SCREEN_HEIGHT // 2
- self.speed = 5
- self.color = WHITE
- self.beam_active = False
- self.beam_length = 150
- self.beam_width = 10
- # Stamina system for beam
- self.max_stamina = 100
- self.stamina = self.max_stamina
- self.stamina_usage = 1 # Stamina used per frame when beam is active
- self.stamina_recovery = 0.5 # Base stamina recovered per frame when beam is off
- self.recovery_boost = 1.0 # Multiplier for stamina recovery
- self.exhausted_timer = 0 # Timer for delay before recovery when stamina is 0
- self.exhausted_delay = 120 # 2 seconds at 60 FPS
- # Stamina upgrade system
- self.stamina_level = 1
- self.max_stamina_level = 5
- self.upgrade_cost = 100 # Base cost to upgrade stamina
- def draw(self, surface):
- points = [
- (self.x, self.y - self.height // 2),
- (self.x - self.width // 2, self.y + self.height // 2),
- (self.x + self.width // 2, self.y + self.height // 2)
- ]
- pygame.draw.polygon(surface, self.color, points)
- # Draw beam if active and has stamina
- if self.beam_active:
- beam_start = (self.x, self.y + self.height // 2)
- beam_end = (self.x, self.y + self.height // 2 + self.beam_length)
- pygame.draw.line(surface, GREEN, beam_start, beam_end, self.beam_width)
- # Draw stamina bar
- stamina_bar_width = 100
- stamina_bar_height = 10
- stamina_x = 10
- stamina_y = 90
- # Background bar (empty stamina)
- pygame.draw.rect(surface, (100, 100, 100),
- (stamina_x, stamina_y, stamina_bar_width, stamina_bar_height))
- # Foreground bar (current stamina)
- stamina_percent = self.stamina / self.max_stamina
- current_bar_width = max(0, int(stamina_bar_width * stamina_percent))
- # Red color for exhausted state, green otherwise
- bar_color = (200, 0, 0) if self.exhausted_timer > 0 else (0, 200, 0)
- pygame.draw.rect(surface, bar_color,
- (stamina_x, stamina_y, current_bar_width, stamina_bar_height))
- # Draw recovery boost button
- button_x = stamina_x + stamina_bar_width + 10
- button_y = stamina_y
- button_width = 20
- button_height = stamina_bar_height
- # Button color changes when active
- button_color = (200, 200, 0) if self.recovery_boost > 1.0 else (150, 150, 0)
- pygame.draw.rect(surface, button_color,
- (button_x, button_y, button_width, button_height))
- # Label for boost button
- font = pygame.font.SysFont(None, 20)
- button_text = font.render("R", True, BLACK)
- text_rect = button_text.get_rect(center=(button_x + button_width // 2,
- button_y + button_height // 2))
- surface.blit(button_text, text_rect)
- # Draw upgrade button
- upgrade_x = stamina_x
- upgrade_y = stamina_y + stamina_bar_height + 5
- upgrade_width = 100
- upgrade_height = 20
- # Button color - darker if max level reached
- upgrade_color = (100, 100, 100) if self.stamina_level >= self.max_stamina_level else (0, 100, 200)
- pygame.draw.rect(surface, upgrade_color,
- (upgrade_x, upgrade_y, upgrade_width, upgrade_height))
- # Upgrade button text
- cost = self.get_upgrade_cost()
- upgrade_label = f"Upgrade: ${cost}" if self.stamina_level < self.max_stamina_level else "MAX LEVEL"
- upgrade_text = font.render(upgrade_label, True, WHITE)
- text_rect = upgrade_text.get_rect(center=(upgrade_x + upgrade_width // 2,
- upgrade_y + upgrade_height // 2))
- surface.blit(upgrade_text, text_rect)
- # Draw level indicator
- level_x = upgrade_x + upgrade_width + 5
- level_y = upgrade_y
- level_text = font.render(f"Lvl {self.stamina_level}/{self.max_stamina_level}", True, WHITE)
- surface.blit(level_text, (level_x, level_y))
- def update(self, keys):
- if keys[pygame.K_a]:
- self.x -= self.speed
- if keys[pygame.K_d]:
- self.x += self.speed
- if keys[pygame.K_w]:
- self.y -= self.speed
- if keys[pygame.K_s]:
- self.y += self.speed
- # Boundary checking
- self.x = max(self.width // 2, min(self.x, SCREEN_WIDTH - self.width // 2))
- self.y = max(self.height // 2, min(self.y, SCREEN_HEIGHT - self.height // 2))
- # Beam activation with stamina check
- if keys[pygame.K_SPACE] and self.stamina > 0:
- self.beam_active = True
- self.stamina = max(0, self.stamina - self.stamina_usage)
- # Start the exhausted timer when stamina hits 0
- if self.stamina == 0:
- self.exhausted_timer = self.exhausted_delay
- else:
- self.beam_active = False
- # Handle recovery delay when stamina was depleted
- if self.exhausted_timer > 0:
- self.exhausted_timer -= 1
- # Only recover if timer is done
- elif self.stamina < self.max_stamina:
- self.stamina = min(self.max_stamina, self.stamina + (self.stamina_recovery * self.recovery_boost))
- # Boost recovery when R key is pressed
- if keys[pygame.K_r]:
- self.recovery_boost = 3.0 # Tripled recovery when R is held
- else:
- self.recovery_boost = 1.0 # Normal recovery rate
- def get_upgrade_cost(self):
- # Calculate cost based on current level
- return self.upgrade_cost * self.stamina_level
- def upgrade_stamina(self, player_money):
- # Check if max level reached
- if self.stamina_level >= self.max_stamina_level:
- return False, player_money, "Max level reached!"
- # Calculate upgrade cost
- cost = self.get_upgrade_cost()
- # Check if player has enough money
- if player_money < cost:
- return False, player_money, "Not enough money!"
- # Apply upgrade
- player_money -= cost
- self.stamina_level += 1
- # Increase max stamina by 25 points per level
- old_max = self.max_stamina
- self.max_stamina = 100 + (self.stamina_level - 1) * 25
- # Increase current stamina proportionally
- self.stamina = self.stamina * (self.max_stamina / old_max)
- # Improve recovery rate slightly
- self.stamina_recovery = 0.5 + (self.stamina_level - 1) * 0.1
- return True, player_money, f"Upgraded to level {self.stamina_level}!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement