Advertisement
ItzSaf

Untitled

May 6th, 2025
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.13 KB | None | 0 0
  1. import pygame
  2. from settings import *
  3.  
  4. class Player:
  5.     def __init__(self):
  6.         self.width = 40
  7.         self.height = 30
  8.         self.x = SCREEN_WIDTH // 2
  9.         self.y = SCREEN_HEIGHT // 2
  10.         self.speed = 5
  11.         self.color = WHITE
  12.         self.beam_active = False
  13.         self.beam_length = 150
  14.         self.beam_width = 10
  15.        
  16.         # Stamina system for beam
  17.         self.max_stamina = 100
  18.         self.stamina = self.max_stamina
  19.         self.stamina_usage = 1  # Stamina used per frame when beam is active
  20.         self.stamina_recovery = 0.5  # Base stamina recovered per frame when beam is off
  21.         self.recovery_boost = 1.0  # Multiplier for stamina recovery
  22.         self.exhausted_timer = 0  # Timer for delay before recovery when stamina is 0
  23.         self.exhausted_delay = 120  # 2 seconds at 60 FPS
  24.        
  25.         # Stamina upgrade system
  26.         self.stamina_level = 1
  27.         self.max_stamina_level = 5
  28.         self.upgrade_cost = 100  # Base cost to upgrade stamina
  29.  
  30.     def draw(self, surface):
  31.         points = [
  32.             (self.x, self.y - self.height // 2),
  33.             (self.x - self.width // 2, self.y + self.height // 2),
  34.             (self.x + self.width // 2, self.y + self.height // 2)
  35.         ]
  36.         pygame.draw.polygon(surface, self.color, points)
  37.  
  38.         # Draw beam if active and has stamina
  39.         if self.beam_active:
  40.             beam_start = (self.x, self.y + self.height // 2)
  41.             beam_end = (self.x, self.y + self.height // 2 + self.beam_length)
  42.             pygame.draw.line(surface, GREEN, beam_start, beam_end, self.beam_width)
  43.            
  44.         # Draw stamina bar
  45.         stamina_bar_width = 100
  46.         stamina_bar_height = 10
  47.         stamina_x = 10
  48.         stamina_y = 90
  49.        
  50.         # Background bar (empty stamina)
  51.         pygame.draw.rect(surface, (100, 100, 100),
  52.                          (stamina_x, stamina_y, stamina_bar_width, stamina_bar_height))
  53.        
  54.         # Foreground bar (current stamina)
  55.         stamina_percent = self.stamina / self.max_stamina
  56.         current_bar_width = max(0, int(stamina_bar_width * stamina_percent))
  57.        
  58.         # Red color for exhausted state, green otherwise
  59.         bar_color = (200, 0, 0) if self.exhausted_timer > 0 else (0, 200, 0)
  60.         pygame.draw.rect(surface, bar_color,
  61.                          (stamina_x, stamina_y, current_bar_width, stamina_bar_height))
  62.                          
  63.         # Draw recovery boost button
  64.         button_x = stamina_x + stamina_bar_width + 10
  65.         button_y = stamina_y
  66.         button_width = 20
  67.         button_height = stamina_bar_height
  68.        
  69.         # Button color changes when active
  70.         button_color = (200, 200, 0) if self.recovery_boost > 1.0 else (150, 150, 0)
  71.         pygame.draw.rect(surface, button_color,
  72.                          (button_x, button_y, button_width, button_height))
  73.                          
  74.         # Label for boost button
  75.         font = pygame.font.SysFont(None, 20)
  76.         button_text = font.render("R", True, BLACK)
  77.         text_rect = button_text.get_rect(center=(button_x + button_width // 2,
  78.                                                 button_y + button_height // 2))
  79.         surface.blit(button_text, text_rect)
  80.        
  81.         # Draw upgrade button
  82.         upgrade_x = stamina_x
  83.         upgrade_y = stamina_y + stamina_bar_height + 5
  84.         upgrade_width = 100
  85.         upgrade_height = 20
  86.        
  87.         # Button color - darker if max level reached
  88.         upgrade_color = (100, 100, 100) if self.stamina_level >= self.max_stamina_level else (0, 100, 200)
  89.         pygame.draw.rect(surface, upgrade_color,
  90.                         (upgrade_x, upgrade_y, upgrade_width, upgrade_height))
  91.        
  92.         # Upgrade button text
  93.         cost = self.get_upgrade_cost()
  94.         upgrade_label = f"Upgrade: ${cost}" if self.stamina_level < self.max_stamina_level else "MAX LEVEL"
  95.         upgrade_text = font.render(upgrade_label, True, WHITE)
  96.         text_rect = upgrade_text.get_rect(center=(upgrade_x + upgrade_width // 2,
  97.                                                 upgrade_y + upgrade_height // 2))
  98.         surface.blit(upgrade_text, text_rect)
  99.        
  100.         # Draw level indicator
  101.         level_x = upgrade_x + upgrade_width + 5
  102.         level_y = upgrade_y
  103.         level_text = font.render(f"Lvl {self.stamina_level}/{self.max_stamina_level}", True, WHITE)
  104.         surface.blit(level_text, (level_x, level_y))
  105.  
  106.     def update(self, keys):
  107.         if keys[pygame.K_a]:
  108.             self.x -= self.speed
  109.         if keys[pygame.K_d]:
  110.             self.x += self.speed
  111.         if keys[pygame.K_w]:
  112.             self.y -= self.speed
  113.         if keys[pygame.K_s]:
  114.             self.y += self.speed
  115.  
  116.         # Boundary checking
  117.         self.x = max(self.width // 2, min(self.x, SCREEN_WIDTH - self.width // 2))
  118.         self.y = max(self.height // 2, min(self.y, SCREEN_HEIGHT - self.height // 2))
  119.  
  120.         # Beam activation with stamina check
  121.         if keys[pygame.K_SPACE] and self.stamina > 0:
  122.             self.beam_active = True
  123.             self.stamina = max(0, self.stamina - self.stamina_usage)
  124.            
  125.             # Start the exhausted timer when stamina hits 0
  126.             if self.stamina == 0:
  127.                 self.exhausted_timer = self.exhausted_delay
  128.         else:
  129.             self.beam_active = False
  130.            
  131.             # Handle recovery delay when stamina was depleted
  132.             if self.exhausted_timer > 0:
  133.                 self.exhausted_timer -= 1
  134.             # Only recover if timer is done
  135.             elif self.stamina < self.max_stamina:
  136.                 self.stamina = min(self.max_stamina, self.stamina + (self.stamina_recovery * self.recovery_boost))
  137.            
  138.         # Boost recovery when R key is pressed
  139.         if keys[pygame.K_r]:
  140.             self.recovery_boost = 3.0  # Tripled recovery when R is held
  141.         else:
  142.             self.recovery_boost = 1.0  # Normal recovery rate
  143.            
  144.     def get_upgrade_cost(self):
  145.         # Calculate cost based on current level
  146.         return self.upgrade_cost * self.stamina_level
  147.        
  148.     def upgrade_stamina(self, player_money):
  149.         # Check if max level reached
  150.         if self.stamina_level >= self.max_stamina_level:
  151.             return False, player_money, "Max level reached!"
  152.            
  153.         # Calculate upgrade cost
  154.         cost = self.get_upgrade_cost()
  155.        
  156.         # Check if player has enough money
  157.         if player_money < cost:
  158.             return False, player_money, "Not enough money!"
  159.            
  160.         # Apply upgrade
  161.         player_money -= cost
  162.         self.stamina_level += 1
  163.        
  164.         # Increase max stamina by 25 points per level
  165.         old_max = self.max_stamina
  166.         self.max_stamina = 100 + (self.stamina_level - 1) * 25
  167.        
  168.         # Increase current stamina proportionally
  169.         self.stamina = self.stamina * (self.max_stamina / old_max)
  170.        
  171.         # Improve recovery rate slightly
  172.         self.stamina_recovery = 0.5 + (self.stamina_level - 1) * 0.1
  173.        
  174.         return True, player_money, f"Upgraded to level {self.stamina_level}!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement
OSZAR »