Repository navigation
Expand file tree
/
Copy pathGameController.py
More file actions
97 lines (78 loc) · 3.15 KB
/
Copy pathGameController.py
File metadata and controls
97 lines (78 loc) · 3.15 KB
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
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
# Custom imports
from Player import Player
from Visualize.Visualizer import Visualizer # [PROTOTYPE]
from GridMap import MapManager
from Sounds import SoundsHandler
from Visualize.Playgif import play_gif
# Pre-defined imports
import sys
import pygame
import numpy as np
import cv2
from enum import Enum
# init hyperparameters here
from CONSTANTS import FPS, RESOLUTION, CELLS_LIST, MAPS_LIST, SCENES, SOUNDS
class GameController:
"""
This is a class to represent Game Controller Instance
"""
def __init__(self):
pygame.init()
pygame.key.set_repeat(200, 125)
initial_scene = "Menu"
self.game_state_manager = GameStateManager(initial_scene) # [PROTOTYPE]
self.clock = pygame.time.Clock() # Define a variable to control the main loop
self.screen = pygame.display.set_mode(
RESOLUTION) # Create a surface on screen that has the size of `resolution`
# INSTANTIATE SCREEN GRID MAP
self.grid_map = MapManager(resolution=RESOLUTION,
cell=CELLS_LIST,
list_maps=MAPS_LIST) # [PROTOTYPE]
# INSTANTIATE SOUNDS HANDLER
self.sounds_handler = SoundsHandler()
self.sounds_handler.turn_on()
for key, val in SOUNDS['SFX'].items():
self.sounds_handler.add_sfx(key, val['file_name'])
# INSTANTIATE PLAYER
self.player = Player(self.screen,
grid_map=self.grid_map,
current_scene=initial_scene,
initial_pos=SCENES[initial_scene]["initial_pos"],
sounds_handler=self.sounds_handler) # [PROTOTYPE]
# INSTANTIATE VISUALIZER
self.visualizer = Visualizer(self.screen,
self.player,
sounds_handler=self.sounds_handler) # [PROTOTYPE]
# INSTANTIATE ALGORITHMS
# pass
# INSTANTIATE MAZE
# pass
def run(self):
self.visualizer.start_visualize() # [PROTOTYPE]
# [MAIN GAME LOOP]
pygame.init()
play_gif(self.screen, name='welcome') # [PROTOTYPE]
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.exit()
sys.exit(0)
next_scene, next_grid_pos = self.visualizer.draw_scene(self.game_state_manager.get_state())
# Transmit signal to another scene here, this is just a prototype
if next_scene:
self.game_state_manager.change_state(next_scene)
self.player.set_current_scene(next_scene, initial_pos=next_grid_pos)
else:
running = False
# pygame.display.update()
self.clock.tick(FPS)
# [MAIN GAME LOOP]
class GameStateManager:
def __init__(self, current_state: str):
self.game_state = current_state
def change_state(self, new_state: str):
self.game_state = new_state
def get_state(self) -> str:
return self.game_state