|
| 1 | +import pygame |
| 2 | +import random |
| 3 | + |
| 4 | +TILE_SIZE = 40 |
| 5 | +PLAYER_SPEED = 3 |
| 6 | +FLASHLIGHT_RADIUS = 150 |
| 7 | +TORCH_RADIUS = 100 |
| 8 | +TORCH_DURATION = 300 # frames |
| 9 | +SPEED_BOOST_DURATION = 600 # frames |
| 10 | +HEALTH_PACK_HEAL = 50 |
| 11 | + |
| 12 | +class Player(pygame.sprite.Sprite): |
| 13 | + def __init__(self, x, y): |
| 14 | + super().__init__() |
| 15 | + self.image = pygame.Surface((TILE_SIZE // 2, TILE_SIZE // 2)) |
| 16 | + self.image.fill((255, 255, 0)) # Yellow |
| 17 | + self.rect = self.image.get_rect(center=(x, y)) |
| 18 | + self.health = 100 |
| 19 | + self.battery = 100 |
| 20 | + self.torches = 0 |
| 21 | + self.active_torches = [] # List of (pos, timer) |
| 22 | + self.moving = False |
| 23 | + self.flashlight_on = True |
| 24 | + self.speed_boost = 0 |
| 25 | + self.speed_timer = 0 |
| 26 | + self.base_speed = PLAYER_SPEED |
| 27 | + |
| 28 | + def update(self, walls, items): |
| 29 | + self.moving = False |
| 30 | + keys = pygame.key.get_pressed() |
| 31 | + dx, dy = 0, 0 |
| 32 | + current_speed = self.base_speed + self.speed_boost |
| 33 | + if keys[pygame.K_LEFT] or keys[pygame.K_a]: |
| 34 | + dx = -current_speed |
| 35 | + if keys[pygame.K_RIGHT] or keys[pygame.K_d]: |
| 36 | + dx = current_speed |
| 37 | + if keys[pygame.K_UP] or keys[pygame.K_w]: |
| 38 | + dy = -current_speed |
| 39 | + if keys[pygame.K_DOWN] or keys[pygame.K_s]: |
| 40 | + dy = current_speed |
| 41 | + |
| 42 | + if dx or dy: |
| 43 | + self.move(dx, dy, walls) |
| 44 | + self.moving = True |
| 45 | + |
| 46 | + # Flashlight drain |
| 47 | + if self.flashlight_on and self.battery > 0: |
| 48 | + self.battery -= 0.05 |
| 49 | + if self.battery < 0: |
| 50 | + self.battery = 0 |
| 51 | + self.flashlight_on = False |
| 52 | + |
| 53 | + # Toggle flashlight |
| 54 | + if keys[pygame.K_f]: |
| 55 | + self.flashlight_on = not self.flashlight_on |
| 56 | + |
| 57 | + # Place torch |
| 58 | + if keys[pygame.K_t] and self.torches > 0: |
| 59 | + self.active_torches.append((self.rect.center, TORCH_DURATION)) |
| 60 | + self.torches -= 1 |
| 61 | + |
| 62 | + # Update torches |
| 63 | + self.active_torches = [(pos, timer - 1) for pos, timer in self.active_torches if timer > 0] |
| 64 | + |
| 65 | + # Update speed boost |
| 66 | + if self.speed_timer > 0: |
| 67 | + self.speed_timer -= 1 |
| 68 | + else: |
| 69 | + self.speed_boost = 0 |
| 70 | + |
| 71 | + def move(self, dx, dy, walls): |
| 72 | + new_rect = self.rect.move(dx, dy) |
| 73 | + if not any(new_rect.colliderect(wall.rect) for wall in walls): |
| 74 | + self.rect = new_rect |
| 75 | + |
| 76 | + def get_sound_position(self): |
| 77 | + return (self.rect.centerx + random.randint(-20, 20), self.rect.centery + random.randint(-20, 20)) |
| 78 | + |
| 79 | + def get_light_positions(self): |
| 80 | + positions = [] |
| 81 | + if self.flashlight_on: |
| 82 | + positions.append((self.rect.centerx, self.rect.centery, FLASHLIGHT_RADIUS)) |
| 83 | + for pos, _ in self.active_torches: |
| 84 | + positions.append((pos[0], pos[1], TORCH_RADIUS)) |
| 85 | + return positions |
| 86 | + |
| 87 | + def collect_item(self, item): |
| 88 | + points = 10 |
| 89 | + if item.type == 'battery': |
| 90 | + self.battery = min(100, self.battery + 50) |
| 91 | + elif item.type == 'torch': |
| 92 | + self.torches += 1 |
| 93 | + elif item.type == 'health': |
| 94 | + self.health = min(100, self.health + HEALTH_PACK_HEAL) |
| 95 | + points = 20 |
| 96 | + elif item.type == 'speed': |
| 97 | + self.speed_boost = 2 |
| 98 | + self.speed_timer = SPEED_BOOST_DURATION |
| 99 | + points = 15 |
| 100 | + return points |
| 101 | + |
| 102 | + def take_damage(self, amount): |
| 103 | + self.health -= amount |
| 104 | + if self.health < 0: |
| 105 | + self.health = 0 |
0 commit comments