/
menu.py
executable file
·431 lines (369 loc) · 12.5 KB
/
menu.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/python
#author:Jonas Bollgruen
#mail:apothecarius@web.de
#version: 2.3
#TODO netzwerknachrichten ueber optionsaenderungen nutzen
#BUG
#frisch gespawnt, mit 6 weglaufen, nach hinten bedroht, nach vorn gelaufen, konnte nicht geopfert werden
import gtk
import gobject
gobject.threads_init()
import surface
import game
import plun
import network
import language
class start_win(gtk.Window):
def __init__(self):
super(start_win,self).__init__()
self.destro_a = self.connect("destroy", gtk.main_quit)
#self.resize(250,250)
self.set_resizable(False)
self.set_position(gtk.WIN_POS_CENTER)
self.pl = [1,0,1,0] #list for what player is controlled by what(bot,user,LAN)
self.grid = gtk.Table(5,4,False)
self.loaded = False#required for start_game not to overwrite the players
self.tex = [gtk.Label("Red")]
self.tex.append(gtk.Label("Blue"))
self.tex.append(gtk.Label("Yellow"))
self.tex.append(gtk.Label("Green"))
self.sel_but = [gtk.combo_box_new_text(),gtk.combo_box_new_text(),gtk.combo_box_new_text(),gtk.combo_box_new_text()]
for i in range(0,4):#selectors or control
self.sel_but[i].append_text("Server")
self.sel_but[i].append_text("Closed")
#self.sel_but[i].append_text("Computer")
self.sel_but[i].append_text("Network")
self.sel_but[0].set_active(0)
self.sel_but[1].set_active(1)
self.sel_but[2].set_active(0)
self.sel_but[3].set_active(1)
for i in range(0,4):#connect the playercontrolswitches to the function
self.sel_but[i].connect("changed", self.switch_player)
self.names = []
for i in range(0,4):#entries for name
self.names.append(gtk.Entry(max = 24))
self.names[-1].set_text("Player "+str(i+1))
self.names[-1].connect("changed",self.name_change,i)
self.joinwindow = joinWindow()
self.optwindow = optionwindow()
self.opt_but = gtk.Button("Options",None,False)
self.opt_but.connect("clicked",self.optwindow.showwrap)
self.but = gtk.Button("Start Game", None, False)
self.but.connect("clicked", game_start, self.pl)
self.join = gtk.Button("Join Game",None, False)
self.join.connect("clicked",self.joinwindow.show_all_wrap)
self.leave = gtk.Button("Exit",None,False)
self.destro_b = self.leave.connect("clicked", self.quit_startgame)
#self.hostbut = gtk.ToggleButton(label="Host")#not necessary since opening player to network activates NW too
#self.hostbut.connect("toggled",self.switch_network)
for i in range(0,4):
self.grid.attach(self.tex[i],0,1,i,i+1)
self.grid.attach(self.sel_but[i],1,2,i,i+1)
self.grid.attach(self.names[i],2,3,i,i+1)
self.grid.attach(self.opt_but,2,3,4,5)
#self.grid.attach(self.hostbut,0,1,4,5)
self.grid.attach(self.but,0,1,5,6)
self.grid.attach(self.join,1,2,5,6)#todo: complete network
self.grid.attach(self.leave,2,3,5,6)
self.add(self.grid)
self.show_all()
gobject.timeout_add(int(1000*network.poll_intervall), self.networkPoll)
def networkPoll(self):
data = network.getInput()
for e in data:
a = e[0]#adress
e = e[1]#message
#print e
if e[0] == "g":#gamestart
game_start("lan",self.pl)
elif e[0] == "d":
if network.hosting:
network.send(e,a,True)
game.throw_the_dice("butter",inp=int(e[1]))
elif e[0] == "z":
if network.hosting:
print("MUHAHAHA")#this is called repeatedly
network.send(e,a,True)
game.actio(int(e[2]),None,int(e[1]),fr=True)#actio(doing,player,unit)
elif e[0] == "o":#optionsaenderung #TODO might not work, hasnt been tested
if e[1] == "c":#change chessmod
self.optwindow.chessmod.set_active(int(e[1]))
#self.optwindow.chessmod.toggled()
elif e[1] == "j":#change jumps
self.optwindow.check_jumps.set_active(int(e[1]))#TODO geht nicht, und nur host darf das aendern
else:
continue
elif e[0] == "s":#spieleraenderung
try:
num = int(e[1])#which player
except:
continue
if e[2] == "n":#name
self.names[num].set_text(e[3:])
elif e[2] == "c":#controlled how
t = {"h":0,"n":3,"b":2,"c":1}[e[3]]
self.sel_but[num].set_active(t)
else:
continue
elif e[0] == "e":#end of turn
if network.hosting:#send it to everyone else
network.send("e",adress=a,n=True)
game.next_pl("nw")
else:#nix
continue
return 1
def name_change(self,but,i):
#TODO prohibit empty name #= ""
if but.get_sensitive():
network.gamedetails.pn[i] = but.get_text()
def quit_startgame(self, but):#might have to do some stuff for network
gtk.main_quit()
exit()
#def switch_network(self,but):
# if self.hostbut.get_active():
# network.enable()
# else:
# network.disable()
# return
def switch_player(self, which):#reacts on changing the entry of a player
if not network.hosting:
return
if which == self.sel_but[0]:
order = 0
elif which == self.sel_but[1]:
order = 1
elif which == self.sel_but[2]:
order = 2
elif which == self.sel_but[3]:
order = 3
if "Closed" == which.get_active_text():
self.pl[order] = 0
tag = "c"
self.names[order].set_sensitive(True)
elif "Server" == which.get_active_text():
self.pl[order] = 1
tag = "h"
self.names[order].set_sensitive(True)
elif "Computer" == which.get_active_text():
self.pl[order] = 2
tag = "b"
self.names[order].set_sensitive(True)
elif "Network" == which.get_active_text():
self.pl[order] = 4
self.names[order].set_sensitive(False)
self.names[order].set_text("")
tag = "n"
if not network.enabled and self.but.get_sensitive():
network.enable()
#eig nicht noetig TO DO activate network if first
else:#network player player was assigned
pass
network.gamedetails.setType(order,tag)###bug ist hier
if network.enabled:
f = True
for but in self.sel_but:#suche nach offenen netzwerkslots
if but.get_active_text() == "Network":
f = False
#keiner mehr offen also netzwerk abschalten
if f:
network.enabled = False
#FALLS HIER NOCH WAS KOMMT DIE OBIGE STRUKTUR ERSETZEN
return
def leavegame(self,butt):
start_window.but.set_sensitive(True)
start_window.join.set_label("Join Game") #umbenennen
start_window.join.connect("clicked",self.joinwindow.join_game)
start_window.opt_but.set_sensitive(True)
def resetMenu(self):
butt.set_label("Join Game")
self.but.set_sensitive(True)
for i in range(4):
self.names[i].set_text("Player "+str(i+1))
class optionwindow(gtk.Window):#TODO closing destroys the window
def __init__(self):
super(optionwindow,self).__init__()
self.opt_win_grid = gtk.Table()
self.add(self.opt_win_grid)
self.check_jumps = gtk.CheckButton(label ="Jumping")
self.check_jumps.set_active(jumbs)
self.check_jumps.connect("toggled",self.tog_jump)
self.small_gui = gtk.CheckButton(label = "Small GUI")
self.small_gui.set_active(not bigui)
self.small_gui.connect("toggled", self.toggle_guisize)
self.chessmod = gtk.CheckButton(label="Chessmod")
self.chessmod.set_active(cheese)
self.chessmod.connect("toggled", self.toggle_chessmod)
self.opt_win_grid.attach(self.chessmod, 0,1,4,5)
self.opt_win_grid.attach(self.small_gui, 1,2,4,5)
self.opt_win_grid.attach(self.check_jumps, 2,3,4,5)
def showwrap(self,a):
self.show_all()
def tog_jump(self,but):
if self.check_jumps.get_active():#activated
game.rules.jumps = True
else:#deactivated
game.rules.jumps = False
return
def toggle_guisize(self,but):
if self.small_gui.get_active():#activated
surface.board.big_gui = False
else:
surface.board.big_gui = True
return
def toggle_chessmod(self,but):
if self.chessmod.get_active():#activated
game.rules.chessmod = True
else:#deactivated
game.rules.chessmod = False
surface.set_chessmod(game.rules.chessmod)
return
class joinWindow(gtk.Window):
def __init__(self):
#network join window
#currently not being used
super(joinWindow,self).__init__()
self.join_win_grid = gtk.Table(5,3,False)
self.add(self.join_win_grid)
self.con_win_name_note = gtk.Label("Name")
self.con_win_name = gtk.Entry(max = 24)
self.join_one = gtk.CheckButton(label = "Rot")
self.join_two = gtk.CheckButton(label ="Blau")
self.join_three = gtk.CheckButton(label = "Gelb")
self.join_four = gtk.CheckButton(label ="Gruen")
self.aipi_note = gtk.Label("IP")
self.aipi = gtk.Entry(max = 15)
self.but_connect = gtk.Button("Beitreten",None,False)
self.but_connect.connect("clicked",self.join_game)
self.join_win_grid.attach(self.con_win_name_note,0,1,0,1)
self.join_win_grid.attach(self.con_win_name,1,3,0,1)
self.join_win_grid.attach(self.join_one,0,2,1,2)
self.join_win_grid.attach(self.join_two,2,3,1,2)
self.join_win_grid.attach(self.join_three,0,2,2,3)
self.join_win_grid.attach(self.join_four,2,3,2,3)
self.join_win_grid.attach(self.aipi_note,0,1,3,4)
self.join_win_grid.attach(self.aipi,1,3,3,4)
self.join_win_grid.attach(self.but_connect,0,3,4,5)
def show_all_wrap(self,but):
self.show_all()
def join_game(self,a):
ip = self.aipi.get_text()
name = self.con_win_name.get_text()
if ";" in name or name == "":
self.errormsgwin = gtk.MessageDialog(message_format="Fehlerhafter Name")
self.errormsgwin.show_all()
return
players = 0
plist = []
if self.join_one.get_active():
players += 1
plist.append(0)
if self.join_two.get_active():
players += 2
plist.append(1)
if self.join_three.get_active():
players += 4
plist.append(2)
if self.join_four.get_active():
players += 8
plist.append(3)
self.hide()
result = network.connect(ip,name,players)
#1 : ip/port nicht richtig
#2 : verlangte spieler nicht frei
#else
if result == 1:
ermsg = "Konnte keine Verbindung erstellen"
elif result == 2:
ermsg = "Diese Spieler sind nicht mehr verfuegbar"
else:
#print plist
#passe menue an result an
#start_window.hostbut.set_sensitive(False)
start_window.but.set_sensitive(False)
start_window.join.set_label("Leave Game") #umbenennen
start_window.join.connect("clicked",start_window.leavegame)
network.hosting = False
#start_window.opt_but.set_sensitive(False)voruebergehend
for i in range(4):
typ = result[i][0]
name = result[i][1]
start_window.sel_but[i].set_sensitive(False)
if typ == "h":#server
start_window.sel_but[i].set_active(0)
start_window.pl[i] = 1
elif typ == "n":#network
start_window.sel_but[i].set_active(3)
start_window.pl[i] = 4
elif typ == "b":#bot
start_window.sel_but[i].set_active(2)
start_window.pl[i] = 2
elif typ == "c":#closed
start_window.sel_but[i].set_active(1)
start_window.pl[i] = 0
start_window.names[i].set_sensitive(0)
start_window.names[i].set_text(name)
network.gamedetails.pna[0]= name
network.gamedetails.pn[0] = name
for i in plist:
start_window.names[i].set_sensitive(1)
print("clientmenu set")
return
self.errormsgwin = gtk.MessageDialog(message_format=ermsg)
self.errormsgwin.show_all()
return
def game_start(asd,gamers):
#schnelles durchprobieren, ob genug spieler da sind
amount_players = 0
for i in range(0,4):#enough players or bots
if gamers[i] in [1,2,4]:#human, bot or networkplayer
amount_players += 1
if amount_players in [0,1]:
return
del amount_players
for i in range(4):#players with no names
if start_window.names[i].get_text() == "" and start_window.sel_but.get_active_text() != "Closed":
start_window.bgohg = gtk.MessageDialog(message_format="Ein Spieler hat keinen Namen")
start_window.bgohg.show_all()
return
#game can begin
if network.hosting:
network.send("g")
for i in range(4):
if start_window.sel_but[i].get_active_text() != "Network":
network.gamedetails.op.append(i)
#set players
print (network.gamedetails.op)
for i in range(0,4):
if start_window.loaded:
break
if gamers[i]:
game.players[i] = plun.player(i,"balubel")
game.players[i].name = start_window.names[i].get_text()
else:
game.players[i] = 0
# Make sure that the grave of the other players is grey
surface.restrict_to_player(game.memo.act_pl.nr)
start_window.optwindow.hide()
start_window.joinwindow.hide()
start_window.hide()
surface.set_visi(True)
#todo: network stuff
#send msg to clients that the game has started
#send playernames to the server and otherway round
#send rules of the game
surface.pre_render(game.players)
game.newgame()
surface.set_msg(game.memo.get_plstr()+"\nstarts the game")
game.rules = game.rule()
game.memo = game.memory()
surface.board = surface.brett()
#these values are some starting values for the startmenu
bigui = False
jumbs = True
cheese = False
surface.set_chessmod(cheese)
surface.set_size(bigui)
#surface.board.set_imgs()#done in set_size()
start_window = start_win()
start_window.show_all()
gtk.main()