forked from RaffaeleFiorillo/Snake_Game_Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauxiliar_functions.py
42 lines (33 loc) · 1.54 KB
/
auxiliar_functions.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
import pygame
pygame.font.init()
WORLD_SIZE = 24
CELL_SIZE = 15
SCREEN = pygame.display.set_mode((WORLD_SIZE * CELL_SIZE, WORLD_SIZE * CELL_SIZE))
CLOCK = pygame.time.Clock()
TEXT_FONT = pygame.font.SysFont('Times New Roman', 12)
# returns an image ready to be displayed on the screen. "convert_alpha" makes it much faster to display
def load_image(directory: str)-> pygame.Surface:
return pygame.image.load(f"images/{directory}").convert_alpha()
class Game:
link_function_dict: dict
def __init__(self, screen_lable, link_functions):
self.screen = None
self.link_function_dict = link_functions
self.previous_link = None
self.create_screen(screen_lable)
def create_screen(self, lable: str) -> None:
global SCREEN
pygame.display.set_caption(lable)
self.screen = SCREEN # module must be initialized or the "convert_alpha" method wont work
def start(self, link: str, state=True) -> None:
keys_list = list(self.link_function_dict.keys())
while True:
if state:
self.previous_link = keys_list[keys_list.index(link)] # saving current link in case the state is False
state = self.link_function_dict[link](self.screen)
if state:
link = state
state = True
else: # In case the user wants to exit the game by clicking on the red crux the state is set to False
state = self.link_function_dict["exit1"](self.screen)
link = self.previous_link