Skip to content

Tairitsu.GameManager

God of the game.

GameManager

Classes

GameManager

GameManager()
Source code in Tairitsu/GameManager.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def __init__(self):
    window = pygame.display.set_mode((WindowSettings.width, WindowSettings.height))
    pygame.display.set_caption(WindowSettings.name)

    self.scene = StartMenu(window)
    self.state = GameState.MAIN_MENU
    self.window = window
    self.player = Player(WindowSettings.width // 2, WindowSettings.height // 2)
    Archive.player = self.player
    self.clock = pygame.time.Clock()

    self.dialogBox = None
    self.menu = None

    self.keys = pygame.key.get_pressed()
    Archive.talkType = TalkType.NOTHING

    self.battleBox = None
Attributes
battleBox instance-attribute
battleBox = None
clock instance-attribute
clock = pygame.time.Clock()
dialogBox instance-attribute
dialogBox = None
keys instance-attribute
keys = pygame.key.get_pressed()
menu instance-attribute
menu = None
player instance-attribute
player = Player(WindowSettings.width // 2, WindowSettings.height // 2)
scene instance-attribute
scene = StartMenu(window)
state instance-attribute
state = GameState.MAIN_MENU
window instance-attribute
window = window
Functions
flush_scene
flush_scene(GOTO: Scene)

Flushes the current scene and transitions to the specified scene.

PARAMETER DESCRIPTION
GOTO

The scene to transition to.

TYPE: Scene

Example

flush_scene(CityScene)

Source code in Tairitsu/GameManager.py
38
39
40
41
42
43
44
45
46
47
48
49
50
def flush_scene(self, GOTO: Scene):
    """
    Flushes the current scene and transitions to the specified scene.

    Parameters:
        GOTO (Scene): The scene to transition to.

    Example:
        >>> flush_scene(CityScene)
    """
    self.scene.BgmPlayer.stop()
    self.scene = GOTO(self.window)
    self.state = self.scene.state
render
render()

Renders the game scene and other information.

Source code in Tairitsu/GameManager.py
141
142
143
144
145
146
147
148
149
150
def render(self):
    """
    Renders the game scene and other information.
    """
    self.scene.render(self.player)
    if(self.dialogBox is not None): self.dialogBox.draw()
    if(self.battleBox is not None): self.battleBox.draw()
    if(self.scene.shoppingBox is not None): self.scene.shoppingBox.draw()
    if (self.menu is not None): self.menu.draw()
    if(self.scene.infoBox is not None): self.scene.infoBox.draw(self.window)
tick
tick(fps)
Source code in Tairitsu/GameManager.py
33
34
def tick(self, fps):
    self.clock.tick(fps)
update
update(events: List[pygame.event.Event])
Source code in Tairitsu/GameManager.py
53
54
55
56
57
58
59
def update(self, events: List[pygame.event.Event]):
    self.tick(WindowSettings.fps)
    self.keys = pygame.key.get_pressed()
    self.player.update(self.keys, self.scene)
    self.scene.update_camera(self.player)

    self.update_collide(events)
update_collide
update_collide(events: list[pygame.event.Event])
Source code in Tairitsu/GameManager.py
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def update_collide(self, events: list[pygame.event.Event]):
    self.keys = None
    for event in events:
        if event.type == pygame.KEYDOWN:
            self.keys = pygame.key.get_pressed()
            break
    # Player -> Obstacles
    if pygame.sprite.spritecollide(self.player, self.scene.obstacles, False):
        # 遇到障碍物,取消移动
        self.player.rect = self.player.rect.move(-self.player.dx, -self.player.dy)

    # Player -> NPCs; if multiple NPCs collided, only first is accepted and dealt with.

    # TODO: Distribute to other module
    for npc in self.scene.npcs.sprites():
        if isinstance(npc,ShopNPC):
            if npc.talking:
                if (self.keys is not None):
                    if self.keys[pygame.K_w]:
                        self.scene.shoppingBox.selectedID = max(0, 
                            self.scene.shoppingBox.selectedID - 1)
                    elif self.keys[pygame.K_s]:
                        self.scene.shoppingBox.selectedID = min(4, 
                            self.scene.shoppingBox.selectedID + 1)
                    elif self.keys[pygame.K_RETURN]:
                        if self.scene.shoppingBox.selectedID == 4:
                            npc.talking = False
                            npc.reset_talkCD()   
                            self.player.talking = False
                            self.scene.shoppingBox = None
                        else:
                            self.scene.shoppingBox.buy()   
            elif pygame.sprite.collide_rect(npc, self.player) and npc.can_talk():
                npc.talking = True
                self.player.talking = True
                self.scene.shoppingBox = npc.dialog
                self.scene.shoppingBox.draw()

        if isinstance(npc,DialogNPC):
            if Archive.talkType == TalkType.ENDED and npc.talking and self.keys is not None and self.keys[pygame.K_RETURN]:
                npc.talking = False
                self.player.talking = False
                npc.reset_talkCD()
                self.dialogBox = None
                Archive.talkType = TalkType.NOTHING
            # 保持对话
            elif pygame.sprite.collide_rect(self.player, npc) and npc.can_talk():
                npc.talking = True
                self.player.talking = True
                self.dialogBox = npc.dialog
                Archive.talkType = TalkType.ENDED

    # Player -> Monsters

    if self.state in [GameState.GAME_PLAY_CITY, GameState.GAME_PLAY_WILD]:
        if self.battleBox is None:
            for monster in self.scene.monsters.sprites():
                if pygame.sprite.collide_rect(self.player, monster):
                    self.battleBox = BattleBox(self.window, 
                                        self.player, monster)
                    self.player.fixed = True
                    # self.battleBox.draw()
        else:
            # self.battleBox.draw()
            if self.keys is not None and self.keys[pygame.K_RETURN]:
                self.battleBox.isChoicing = False
            if self.battleBox.isFinished and self.keys is not None and self.keys[pygame.K_RETURN]:
                self.battleBox = None
                self.player.fixed = False
                self.scene.BgmPlayer.play()


    # Player -> Portals
    portalList = pygame.sprite.spritecollide(self.player, self.scene.portals,     
                            False, pygame.sprite.collide_mask)
    if portalList:
        pygame.event.post(pygame.event.Event(GameEvent.EVENT_SWITCH, {"GOTO":portalList[0].directionType,}))