summaryrefslogtreecommitdiff
path: root/src/sliceitoff/player/player.py
blob: 82cc9a43cc544c6e1edbb265134de16f39f04717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
""" player.player - Player sprite group and actions """
import os
import pygame

from display import Scaling

from .static import SLICER

DEBUG = os.getenv("DEBUG")

def dataclass_to_surface(dc):
    """ Converts dataclass to surface """
    image = pygame.Surface(dc.DIMENSIONS, pygame.SRCALPHA)
    for x in range(dc.DIMENSIONS[0]):
        for y in range(dc.DIMENSIONS[1]):
            image.set_at((x,y), dc.COLORS[dc.IMAGE[y*dc.DIMENSIONS[0]+x]])
    return image

class PlayerSprite(pygame.sprite.Sprite):
    """ The slicing tool. There is 2 of these. Horizontal and vertical """
    def __init__(self, image):
        super().__init__()
        self.image = image
        self.rect = self.image.get_rect()

    def update(self, pos = None):
        """ Sets sprite center at given position """
        if pos:
            w, h = self.image.get_size()
            self.rect = self.image.get_rect().move(pos[0]-w//2,pos[1]-h//2)

class Player(pygame.sprite.Group):
    """ The slicer. Special sprite group that only list 1 sprite """
    def __init__(self):
        super().__init__()
        self.position = (0,0)
        self.direction = False
        self.lazer = False
        image = dataclass_to_surface(SLICER)
        image = pygame.transform.scale_by(image, 1_000 * Scaling.factor)
        self.add(PlayerSprite(image))
        image = pygame.transform.rotate(image, 90)
        self.add(PlayerSprite(image))

    def update(self, pos = None, direction = False):
        """ Updates the position and direction """
        if self.lazer:
            direction = False
            pos = None
        if direction:
            self.direction = not self.direction
        if pos:
            self.position = Scaling.scale_to_internal(pos)
            for sprite in super().sprites():
                sprite.update(pos = pos)

    def sprites(self):
        """ Only list sprite of current direction for draw """
        return [super().sprites()[self.direction]]