Skip to content

Tairitsu.Main

Main class of the game.

Main

Functions

main

main()

Main function of the game. Handles events and updates.

Events:

  • QUIT: Quit the game.
  • MOUSEMOTION: Get mouse position.
  • EVENT_SWITCH: Switch scene(Portal).
  • KEYDOWN: Get key pressed.
Source code in Tairitsu/Main.py
 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def main():
    """
    Main function of the game. Handles events and updates.

    Events:

    - `QUIT`: Quit the game.
    - `MOUSEMOTION`: Get mouse position.
    - `EVENT_SWITCH`: Switch scene(Portal).
    - `KEYDOWN`: Get key pressed.
    """
    pygame.init()
    manager = GameManager()
    Archive.manager = manager

    while True:
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                # If key == returnww
                print("Key pressed: ", event.key)

                # if event.key in [pygame.K_RETURN:
                if manager.state == GameState.MAIN_MENU:
                    # manager.scene.menu
                    if event.key == pygame.K_w:
                        manager.scene.menu.up()
                    elif event.key == pygame.K_s:
                        manager.scene.menu.down()
                    elif event.key == pygame.K_RETURN:
                        manager.scene.menu.select()
                        if(manager.scene.menu.selectedID == 0):
                            manager.flush_scene(CityScene)
                        elif(manager.scene.menu.selectedID == 1):
                            manager.flush_scene(BossScene)
                        elif(manager.scene.menu.selectedID == 2):
                            pygame.quit()
                            sys.exit()
                else:
                    if event.key == pygame.K_ESCAPE:
                        print("ESCAPE Pressed")
                        manager.menu = MenuBox(manager.window, 
                                               items = {"Continue":"","Return to the Main Menu":""},
                                               bgColor = (33, 53, 62, 130))
                        manager.player.fixed = True

                    if(manager.menu is not None):
                        if event.key == pygame.K_w:
                            manager.menu.up()
                        elif event.key == pygame.K_s:
                            manager.menu.down()
                        elif event.key == pygame.K_RETURN:
                            manager.menu.select()
                            if(manager.menu.selectedID == 0):
                                pass
                            elif(manager.menu.selectedID == 1):
                                manager.flush_scene(StartMenu)
                            manager.menu = None
                            manager.player.fixed = False

            # elif event.type == pygame.MOUSEMOTION:
            #     pass
                # print(pygame.mouse.get_pos())
            elif event.type == GameEvent.EVENT_SWITCH:
                manager.flush_scene(event.dict["GOTO"])

            manager.scene.scene_event(event)
            # except: pass
        if(manager.player.HP <= 0): manager.flush_scene(DeathScene)

        manager.update(events = events)
        manager.render()
        pygame.display.flip()