-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
72 lines (54 loc) · 2.21 KB
/
main.py
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
65
66
67
68
69
70
71
72
import pygame
from button import ButtonSystem
from common_components import ContextComponent
from ecs import WORLD, Component
from scene import SceneManager
from scenes.title import TitleScene
from sound import AudioSystem
from utils import find_data_file
def main():
# Initialize pygame before we do anything else
pygame.init()
programIcon = pygame.image.load(find_data_file("resources/icarus_icon.png"))
pygame.display.set_icon(programIcon)
# Initialize global systems in the game world
if pygame.mixer.get_init() is not None:
WORLD.register_system(AudioSystem())
WORLD.register_system(ButtonSystem())
# Load game metadata and store it in an entity
settings = Component.load_from_json("settings")
WORLD.gen_entity().attach(settings)
# Set up the pygame window
flags = pygame.SCALED
screen = pygame.display.set_mode(
(settings["height"], settings["width"]), flags=flags, vsync=1
)
pygame.display.set_caption(settings["title"] + ": " + settings["subtitle"])
# Store our dynamic resources that are created at runtime in the game world
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((200, 200, 200))
context = ContextComponent(screen, pygame.time.Clock(), background)
game = WORLD.gen_entity()
game.attach(context)
# Create a new Title screen object
title_screen = TitleScene()
# Initialize a SceneManager with our title screen
manager = SceneManager(title_screen, WORLD)
# BIG GAME LOOP
while game["context"]["running"]:
game["context"]["clock"].tick(60)
# Process game wide events, most likely only QUIT
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
game["context"]["running"] = False
# Update the current scene
switch_event = manager.update(events, WORLD)
# Render the current scene
manager.render(WORLD)
pygame.display.flip() # Double buffers whatever was on the screen object to the actual display
# Finally switch scenes in the scene manager
manager.switch(switch_event, WORLD)
if __name__ == "__main__":
main()