summaryrefslogtreecommitdiff
path: root/src/sliceitoff/game/initials.py
blob: 49db3d69907827ba157d63e89ce427a8e9119d49 (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
60
61
62
63
64
""" game.initials - Use will be asked for initials """
import pygame

from screens import initials_screen

from .anykey import anykey

class Initials(pygame.sprite.Group):
    """ Sprite group that asks initials to self.name from user """
    def __init__(self):
        super().__init__()
        self.add(initials_screen(""))
        self.name = ""
        self.explode = False
        self.active = True
        self.fadeout = 1_000
        
    def update(self, dt = 0, **kwargs):
        if not self.active:
            return

        super().update(dt = dt, explode = self.explode, **kwargs)

        if self.explode:
            if self.fadeout <= 0:
                self.active = False
            else:
                if anykey():
                    self.fadeout = 0
                    self.active = False
                self.fadeout -= dt
            return

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.explode = True
                break
            if event.type == pygame.KEYDOWN:
                if event.key in (
                        pygame.K_ESCAPE,
                        pygame.K_KP_ENTER,
                        pygame.K_RETURN):
                    self.explode = True
                    break
                if event.key in (
                        pygame.K_RSHIFT,
                        pygame.K_LSHIFT,
                        pygame.K_RCTRL,
                        pygame.K_LCTRL,
                        pygame.K_RALT,
                        pygame.K_LALT,
                        pygame.K_RMETA,
                        pygame.K_LMETA,
                        pygame.K_LSUPER,
                        pygame.K_RSUPER,
                        pygame.K_SPACE):
                    continue
                if event.key in (pygame.K_BACKSPACE, pygame.K_DELETE):
                    self.name = self.name [:-1]
                elif pygame.key.name(event.key):
                    self.name += pygame.key.name(event.key)[0].upper()
                    self.name = self.name[:3]
                self.empty()
                self.add(initials_screen(self.name))