-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
161 lines (115 loc) · 3.93 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
# __main__.py
#
# Copyright (C) 2014 Alejandro Simon
# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
#
import pygame
#from pygame.locals import *
import utils as u
import sys
import menu.menu_controller as mc
import game.game_controller as gc
import game.tournament_controller as tc
class WiffWaffer:
def __init__(self):
self._state = 0
self._running = True
self._display_surf = None
self._clear_screen = False
self.size = self.weight, self.height = 800, 600 # 1920, 1080pygame.display.set_mode((640,480),pygame.)
def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
pygame.display.set_caption("Wiff Waffer")
# Ignore mouse motion (greatly reduces resources when not needed)
pygame.event.set_blocked(pygame.MOUSEMOTION)
# create the pygame clock
self._clock = pygame.time.Clock()
self._running = True
# Create menu
self._menu_controller = mc.MenuController(self._display_surf)
game._menu_controller.update_background()
# Create game
self._game_controller = gc.GameController(self._display_surf)
self._game_controller.on_load()
self._tournament_controller = tc.TournamentController(self._display_surf)
def on_event(self, event):
if event.type == pygame.QUIT:
self._running = False
if self._state == 0:
self._menu_controller.on_event(event)
elif self._state == 1:
self._game_controller.on_event(event)
def on_loop(self):
if self._state == 0:
self._menu_controller.on_loop()
elif self._state == 1:
self._game_controller.on_loop()
def on_render(self):
# Render
if self._state == 0:
self._menu_controller.on_render()
elif self._state == 1:
self._game_controller.on_render()
# Clear screen
if self._clear_screen:
self._display_surf.fill((u.BLACK))
pygame.display.update()
self._clear_screen = False
# refresh the display
pygame.display.flip()
def on_cleanup(self):
pygame.quit()
sys.exit()
def on_execute(self):
if self.on_init() is False:
self._running = False
while(self._running):
# make sure the program is running at 30 fps
self._clock.tick(30)
# Update
for event in pygame.event.get():
self.on_event(event)
self.on_loop()
# Render
self.on_render()
self.on_cleanup()
game = None
def clear_screen():
game._clear_screen = True
def on_change_menu(menu):
game._menu_controller.on_change_menu(menu)
game._menu_controller.update_background()
def back_to_menu():
game._state = 0
on_change_menu('main')
def start_game():
game._state = 1
clear_screen()
def create_player(name=None, avatar=None, colour=None):
game._game_controller.create_player(name, avatar, colour)
def add_player_by_name(name=None, tournament=False):
if tournament:
game._tournament_controller.add_player_by_name(name, game._game_controller._players)
else:
game._game_controller.add_player_by_name(name)
def get_current_player():
return game._game_controller._currentPlayer
def get_player_1():
return game._game_controller._player1
def get_player_2():
return game._game_controller._player2
def get_players():
return game._game_controller._players
def get_tournament():
return game._tournament_controller
def reset_game():
game._game_controller.on_reset()
# Program begins
if __name__ == "__main__":
if not u.is_gui():
sys.exit("wiff-waffer requires an X session")
u.ensure_dir("players")
game = WiffWaffer()
game.on_execute()