pneuma-pygame/PyGame/entities/observer.py
2024-05-17 01:15:22 +02:00

66 lines
1.8 KiB
Python

import os
import pygame
from configs.system.window_config import HITBOX_OFFSET
from utils.resource_loader import import_assets
class Observer(pygame.sprite.Sprite):
def __init__(self, position, groups):
super().__init__(groups)
self.sprite_type = 'camera'
self.image = pygame.image.load(
import_assets(os.path.join('graphics',
'observer.png'))
).convert_alpha()
self.rect = self.image.get_rect(topleft=position)
self.hitbox = self.rect.inflate(HITBOX_OFFSET[self.sprite_type])
# Stats
self.exp = -1 # This prints OBSERVER in the UI
self.speed = 10 # Speed for moving around
# Movement
self.direction = pygame.math.Vector2()
def input(self):
keys = pygame.key.get_pressed()
# Movement Input
if keys[pygame.K_w]:
self.direction.y = -1
self.status = 'up'
self.can_move = False
elif keys[pygame.K_s]:
self.direction.y = 1
self.status = 'down'
self.can_move = False
else:
self.direction.y = 0
if keys[pygame.K_a]:
self.direction.x = -1
self.status = 'left'
self.can_move = False
elif keys[pygame.K_d]:
self.direction.x = 1
self.status = 'right'
self.can_move = False
else:
self.direction.x = 0
def move(self, speed):
if self.direction.magnitude() != 0:
self.direction = self.direction.normalize()
self.hitbox.x += self.direction.x * speed
self.hitbox.y += self.direction.y * speed
self.rect.center = self.hitbox.center
def update(self):
self.input()
self.move(self.speed)