-
Notifications
You must be signed in to change notification settings - Fork 0
/
hud.py
353 lines (276 loc) · 9.73 KB
/
hud.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
##########################################################################
## hud.py
##
## Layouts of all the different interfaces
##
## by Andrew Francis
##########################################################################
import pygame, threading, string
from pgu import gui
from tempfile import mkstemp
from shutil import move
from os import remove, close
from pygame.event import Event
from constants import *
class MyApp(gui.App):
def __init__(self):
gui.App.__init__(self)
def event(self, ev):
if ev.type == CHATMESSAGE:
print '********************************************************Event : ' + str(ev)
e = pygame.event.Event(ev.type, nick=ev.nick)
gui.Widget.send(self, e.type, e)
#gui.App.event(self,ev)
def load_pre_lobby():
"""Pre-Lobby Interface"""
menu = gui.App()
menu.connect(gui.QUIT,menu.quit,None)
main = gui.Container(width=500, height=400)
main.add(gui.Label("Select User", cls="h1"), 20, 20)
my_input = gui.Input(value='New User', size=12)
main.add(my_input, 40,80)
my_list = gui.List(width=150, height=100)
main.add(my_list, 250, 80)
count = 1
def remove_list_item(arg):
"""Removes selected list item"""
v = my_list.value
if v:
item = v
my_list.remove(item)
my_list.resize()
my_list.repaint()
fh, abs_path = mkstemp()
new_users = open(abs_path, 'w')
users = open(USERS_FILE)
for line in users:
if line and not(line.strip() == v):
new_users.write(line)
new_users.close()
close(fh)
users.close()
remove(USERS_FILE)
move(abs_path, USERS_FILE)
def add_list_item(arg):
"""Adds new item to list with the value entered in the text box"""
my_list.add(my_input.value,value=my_input.value)
my_list.resize()
my_list.repaint()
users = open(USERS_FILE, 'a')
users.write(my_input.value)
users.write('\n')
users.close()
def select_user(arg):
"""Picks the selected username"""
user = my_list.value
if user:
pygame.event.post(Event(MODECHANGE, mode=1, nick=user))
users = open(USERS_FILE, 'r')
for line in users:
user = line.strip()
if user:
my_list.add(user, value=user)
users.close()
b = gui.Button("Add New User", width=150)
main.add(b, 40, 110)
b.connect(gui.CLICK, add_list_item, None)
b = gui.Button("Remove Selected", width=150)
main.add(b, 40, 140)
b.connect(gui.CLICK, remove_list_item, None)
b = gui.Button("Select", width=150)
main.add(b, 250, 200)
b.connect(gui.CLICK, select_user, None)
menu.init(main)
return menu
def load_lobby():
"""Lobby Interface"""
lobby = MyApp()
lobby.connect(gui.QUIT,lobby.quit,None)
main = gui.Container(width=1280, height=720)
def chat_message(_event,_widget,_code,a,b,c):
t.tr()
t.td(gui.Label(nick))
main.add(gui.Label("Main Lobby", cls="h1"), 20, 20)
t = gui.Table()
box = gui.ScrollArea(t,800,550)
main.add(box,20,60)
box.connect(gui.CLICK, chat_message,1,2,3)
lobby.init(main)
return lobby
def load_loading_screen():
"""Loading Screen Interface"""
screen = MyApp()
screen.connect(gui.QUIT, screen.quit, None)
main = gui.Container(width=500, height=400)
main.add(gui.Label("Finding Available Games...", cls="h1"), 100, 100)
screen.init(main)
return screen
def load_game_lobby(server=False, games=[]):
"""Game Lobby interface"""
menu = gui.App()
menu.connect(gui.QUIT,menu.quit,None)
main = gui.Container(width=500, height=400)
main.add(gui.Label("Select Game", cls="h1"), 20, 20)
my_list = gui.List(width=150, height=100)
main.add(my_list, 250, 80)
count = 1
def clean_room_name(name):
"""Make the room name clean for XMPP rooms"""
clean_name = ''
name = name.lower()
name_parts = name.split(' ')
for i in range(len(name_parts)):
for letter in name_parts[i]:
if letter in string.ascii_lowercase:
clean_name += letter
if i < len(name_parts)-1:
clean_name += '_'
return clean_name
def add_list_item(arg):
"""Adds new item to list with the value entered in the text box"""
room_name = clean_room_name(my_input.value)
pygame.event.post(Event(NETWORK, msg='create_room', room=room_name))
my_list.add(room_name,value=room_name)
my_list.resize()
my_list.repaint()
FILE_LOCK.acquire()
rooms = open(ROOMS_FILE, 'a')
rooms.write(room_name)
rooms.write('\n')
rooms.close()
FILE_LOCK.release()
def select_room(arg):
"""Picks the highlighted room"""
room_name = my_list.value
if room_name:
if server:
pygame.event.post(Event(MODECHANGE, mode=1, room=room_name))
else:
pygame.event.post(Event(MODECHANGE, mode=3, room=room_name))
def refresh_rooms(arg):
"""Requests a new list of available games"""
if server:
pygame.event.post(Event(MODECHANGE, mode=0))
else:
pygame.event.post(Event(NETWORK, msg='connected'))
if server:
for game in games:
my_list.add(game, value=game)
else:
FILE_LOCK.acquire()
rooms = open(ROOMS_FILE, 'r')
for line in rooms:
room = line.strip()
if room:
my_list.add(room, value=room)
rooms.close()
FILE_LOCK.release()
my_input = gui.Input(value='New Game', size=12)
main.add(my_input, 40,80)
b = gui.Button("Create New Game", width=150)
main.add(b, 40, 110)
b.connect(gui.CLICK, add_list_item, None)
b = gui.Button("Select", width=150)
main.add(b, 250, 200)
b.connect(gui.CLICK, select_room, None)
b = gui.Button("Refresh", width=150)
main.add(b, 250, 225)
b.connect(gui.CLICK, refresh_rooms, None)
menu.init(main)
return menu
def load_loading_game():
"""Loading game interface"""
screen = MyApp()
screen.connect(gui.QUIT, screen.quit, None)
main = gui.Container(width=500, height=400)
main.add(gui.Label("Connecting to Game...", cls="h1"), 100, 100)
screen.init(main)
return screen
def load_pre_game_hud():
"""Pre-game interface"""
hud = gui.App()
hud.connect(gui.QUIT, hud.quit, None)
main = gui.Container(width=1280, height=720)
main.add(gui.Label("Choose Your Positions", cls="h1"), 20, 20)
def add_ai_player(arg):
pygame.event.post(Event(NETWORK, msg='add_ai'))
b = gui.Button("Add AI-Controlled Bot", width=150)
main.add(b, 20, 50)
b.connect(gui.CLICK, add_ai_player, None)
hud.init(main)
return hud
def load_waiting_hud():
"""Waiting for game to start interface"""
hud = gui.App()
hud.connect(gui.QUIT, hud.quit, None)
main = gui.Container(width=1280, height=720)
main.add(gui.Label("Waiting for other players...", cls="h1"), 20, 20)
def add_ai_player(arg):
pygame.event.post(Event(NETWORK, msg='add_ai'))
b = gui.Button("Add AI-Controlled Bot", width=150)
main.add(b, 20, 50)
b.connect(gui.CLICK, add_ai_player, None)
hud.init(main)
return hud
def load_game_hud(server=False):
"""In-game interface"""
hud = gui.App()
hud.connect(gui.QUIT, hud.quit, None)
main = gui.Container(width=1280, height=720)
if server:
def back_to_lobby(arg):
pygame.event.post(Event(MODECHANGE, mode=0))
b = gui.Button("Back to Lobby", width=150)
main.add(b, 20, 100)
b.connect(gui.CLICK, back_to_lobby, None)
hud.init(main)
return hud
def load_post_game_hud(result):
"""Post-game interface, displays result"""
hud = gui.App()
hud.connect(gui.QUIT, hud.quit, None)
main = gui.Container(width=1280, height=720)
win_str = result + " Team wins!"
main.add(gui.Label(win_str, cls="h1"), 20, 40)
def back_to_lobby(arg):
pygame.event.post(Event(MODECHANGE, mode=8))
b = gui.Button("Back to Lobby", width=150)
main.add(b, 20, 100)
b.connect(gui.CLICK, back_to_lobby, None)
hud.init(main)
return hud
def load_quitting_game_hud():
"""Quitting game interface"""
hud = gui.App()
hud.connect(gui.QUIT, hud.quit, None)
main = gui.Container(width=1280, height=720)
main.add(gui.Label("Quitting Game...", cls="h1"), 100, 100)
hud.init(main)
return hud
def load_hud(mode, text=None):
"""Loads the correct interface corresponding to mode"""
if mode is 0:
return load_pre_lobby()
elif mode is 1:
#return load_lobby()
return load_loading_screen()
elif mode is 2:
return load_game_lobby()
elif mode is 3:
return load_loading_game()
elif mode is 4:
return load_pre_game_hud()
elif mode is 5:
return load_waiting_hud()
elif mode is 6:
return load_game_hud()
elif mode is 7:
return load_post_game_hud(text)
elif mode is 8:
return load_loading_screen()
def load_server_hud(mode, text=None, games=[]):
"""Loads the correct interface according to the server mode"""
if mode is 0:
return load_game_lobby(server=True, games=games)
if mode is 1:
return load_game_hud(server=True)