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
|
""" player.player - Player sprite group and actions """
import os
import pygame
from display import Scaling
from text import Fonts
DEBUG = os.getenv("DEBUG")
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 = pygame.Surface((8,26), pygame.SRCALPHA)
for color, y, ch in (
("red",0,0x18),
("red",13,0x19),
("blue",6,0x09)):
ch = Fonts.fonts['standard'].get(ch)
ch.fill( color, special_flags = pygame.BLEND_RGBA_MULT)
image.blit(ch,(0,y))
image = pygame.transform.scale_by(image, 1_200 * Scaling.factor)
image = pygame.transform.rotate(image, 90)
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]]
|