YAP/player.py
Carlo Ghisolfi c2f76d4222 First commit
2026-10-04 19:17:16 +02:00

66 lines
2 KiB
Python

# libraries
from enum import Enum
import pygame
# project modules
from constants import *
# ------------------------------------------------------------
# Class Player
# - represents the player object
# ------------------------------------------------------------
class PlayerTypes(Enum):
BLUE = "blue_robot"
GREEN = "green_robot"
class Powers(Enum):
PRESS = "press"
GRAB = "grab"
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, active, type=PlayerTypes.BLUE):
super().__init__()
# Attributes
self.speed = 1
self.width = 50
self.height = 50
self.x = x
self.y = y
self.active = active
# Yet to see if useful
self.direction = "up"
# Yet to find usage for grab
self.powers = [Powers.PRESS.value, Powers.GRAB.value]
self.current_power_i = 0
self.current_power = self.powers[self.current_power_i]
# Sprite image
self.active_sprite = pygame.image.load(f"assets/{type.value}.svg").convert_alpha()
self.inactive_sprite = pygame.image.load(f"assets/{type.value}_inactive.svg").convert_alpha()
self.image = pygame.transform.smoothscale(self.inactive_sprite, (TILE_SIZE, TILE_SIZE))
# Hitbox and position
self.rect = self.image.get_rect()
self.rect.topleft = (self.x*TILE_SIZE, self.y*TILE_SIZE)
def update(self):
self.image = pygame.transform.smoothscale(self.inactive_sprite, (TILE_SIZE, TILE_SIZE))
if self.active:
self.image = pygame.transform.smoothscale(self.active_sprite, (TILE_SIZE, TILE_SIZE))
# update rect position
self.rect.x = self.x * TILE_SIZE + X_OFFSET
self.rect.y = self.y * TILE_SIZE + Y_OFFSET
# Function to change current power
def change_power(self):
self.current_power_i += 1
if self.current_power_i >= len(self.powers):
self.current_power_i = 0
self.current_power = self.powers[self.current_power_i]