-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroboc.py
126 lines (91 loc) · 4.04 KB
/
roboc.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
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
"""Client source.
Initialize pygame, and start the main loop of the game.
"""
import pygame
from pygame.locals import QUIT
import f_roboc.images as r_images
import f_roboc.connection as f_connection
from f_roboc.introduction.introduction import Introduction
from f_roboc.main_menu.main_menu import MainMenu
from f_roboc.select_level.select_level import SelectLevel
from f_roboc.game.game_initiator import GameInitiator
from f_roboc.game.game import Game
def start_loop(images, main_screen, connection):
"""Start the main loop."""
running = True
interface = Introduction(images['introduction'], connection)
clock = pygame.time.Clock()
while running:
mouse = pygame.mouse.get_pos()
# Change the interface if needed.
interface = _change_interface(interface, images, connection)
# Events section.
for event in pygame.event.get(): # Events call.
if event.type == QUIT:
running = False
interface.start_events(event, mouse) # Ours variable events call.
# Update section.
interface.update()
# Communication to the server.
interface.transfer_datas()
# Drawing section.
interface.draw()
main_screen.blit(interface.sprt.main_surface, (0, 0))
# Screen refreshness.
pygame.display.flip()
# Control the frames per second (want 30 fps).
clock.tick(30)
interface.connection.close()
return
def _check_if_create_or_join_the_game(interface, connection, images):
"""Check if the player creates the game, or if he joins a game.
if he creates the game (from SelectLevel), some parameters will be added.
He can join a game (from MainMenu), if another client has created a game.
"""
if isinstance(interface, MainMenu):
return GameInitiator(images, connection)
elif isinstance(interface, SelectLevel):
return GameInitiator(images, connection, interface._map[0],
interface.nb_players, hote=True)
else:
raise TypeError("You cannot create or join a game "
"while being: {}\n".format(type(interface)),
"Only 'MainMenu' and 'SelectLevel' are valid types.")
def _change_interface(interface, images, connection):
"""Change the interface if needed (by the go_to variable)."""
if not interface.go_to:
return interface
elif 'main_menu' in interface.go_to:
if '-LostConnexion' in interface.go_to:
interface = MainMenu(images['main_menu'], connection, error=True)
else:
interface = MainMenu(images["main_menu"], connection)
elif interface.go_to == 'select_level':
interface = SelectLevel(images["select_level"], connection)
elif interface.go_to == 'game_init':
interface = _check_if_create_or_join_the_game(interface, connection,
images['game_init'])
elif interface.go_to == 'game':
interface = Game(images["game"], connection,
interface.players, interface._map,
interface.active_turn)
else:
raise ValueError("'go_to' must be equal to 'main_menu', "
"'select_level' or 'game'.\n"
"'go_to' is: {}".format(interface.go_to))
return interface
"""This condition avoids starting the game by doing the tests."""
if __name__ == '__main__':
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
"""Initialisation of all Pygame modules."""
pygame.display.set_caption('StarLab') # We choose a title.
main_screen = pygame.display.set_mode((1280, 720)) # the main window.
images = r_images.get_content_of('f_roboc/assets/images/')
r_images.find_characters_and_create_their_moove_r_folder(images)
"""Load of all project images.
We add a 'moove-r' folder for each character,
based on their moove_l folder.
"""
connection = f_connection.ServerConnection()
start_loop(images, main_screen, connection)