-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
465 lines (442 loc) · 19.8 KB
/
client.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
'''
client.py description
loginGUI Class: class for creating the login window
__init__: takes the parent tkinter window, sets a login screen
verifyLogin: sends login request to server using the user input of username and password
decodeGame: takes received game info and decode it into a game object
checkForGame: periodically check for incoming game info on server, and start a game window if detected
gameGUI Class: class for creating the game window
__init__: takes game object, chatComm object, and the username for login, initializes the objects and GUI
sendGame: takes game status (ongoing, professor won, or student won) and send the game info to the other players
updateGame: update the current game object with new game information
decodeGame: takes game info and decode it to game object
confirmIdentity: confirm the identity as a professor
passIdentity: pass the chance of being a professor
selectCard: mark a card object as selected
deselectCard: deselect an already selected card
confirmCard: confirm a card play
passCard: pass a turn
updateScreen: update game screen using game object
initGUI: intialize pygame GUI
run: runs the pygame window and everything that needs to be run
This file can be directly called to create a client window
'''
import tkinter
import pygame
from chatComm import *
import tkinter.messagebox
from pygameWidgets import *
from GameEngine import *
import ast
from utils import convertHelper, anotherConvertHelper
# class for the login window
class loginGUI:
def __init__(self, parent):
# tkinter gui initialization
self.parent = parent
self.mainFrame = tkinter.Frame(parent)
self.mainFrame.pack()
# connection class initialization
self.chatComm = chatComm("86.36.42.136", 15112)
self.chatComm.startConnection()
self.loggedin = False
# title label
titleLab = tkinter.Label(
self.mainFrame, text="FIGHT THE PROFESSOR! presented by Eric Gao\n", font=30)
titleLab.pack()
# username field
lab1 = tkinter.Label(self.mainFrame, text='Username')
lab1.pack()
self.nameBox = tkinter.Entry(self.mainFrame)
self.nameBox.pack()
# password field
lab2 = tkinter.Label(self.mainFrame, text='Password')
lab2.pack()
self.passwordBox = tkinter.Entry(self.mainFrame, show="*")
self.passwordBox.pack()
# ok button
self.ok = tkinter.Button(
self.mainFrame, text="OK", command=self.verifyLogin)
self.ok.pack()
# try to login with username and password
def verifyLogin(self):
# get information from entry fields
self.username = self.nameBox.get()
password = self.passwordBox.get()
# login to server
status = self.chatComm.login(self.username, password)
if status:
# disable widgets
self.nameBox.configure(state=tkinter.DISABLED)
self.passwordBox.configure(state=tkinter.DISABLED)
self.ok.configure(state=tkinter.DISABLED)
# show status
tkinter.messagebox.showinfo(
"Status", "Please click OK and wait for game to be started by host...")
self.checkForGame()
else: # show error, could be less offensive
tkinter.messagebox.showerror(
"Error", "Wrong username or password. You should be smarter than this to fight the professor.")
# decode game
def decodeGame(self, gameInfo):
newGame = Game(gameInfo[1], gameInfo[4], gameInfo[7])
newGame.p1.identity = gameInfo[2]
newGame.p2.identity = gameInfo[5]
newGame.p3.identity = gameInfo[8]
newGame.p1.cards = gameInfo[3]
newGame.p2.cards = gameInfo[6]
newGame.p3.cards = gameInfo[9]
newGame.currentPlayer = gameInfo[10]
newGame.prevPlay = gameInfo[11]
newGame.playOrder = gameInfo[12]
newGame.landLordCards = gameInfo[13]
return newGame
# check for game from host and start main game
def checkForGame(self):
messages = self.chatComm.getMail()[0]
for i in messages:
info = i[1].replace('\n', '').replace(' ', '').split('#')
info = info[1:]
info[3] = convertHelper(info[3])
info[6] = convertHelper(info[6])
info[9] = convertHelper(info[9])
info[11] = anotherConvertHelper(info[11])
info[12] = ast.literal_eval(info[12])
info[13] = convertHelper(info[13])
if info[0] in ['0', '1', '2']:
self.Game = self.decodeGame(info)
self.parent.destroy()
clientGUIObj = clientGUI(
self.Game, self.chatComm, self.username)
return
self.parent.after(5000, self.checkForGame)
# main game GUI
class clientGUI:
def __init__(self, Game, chatComm, name):
# initialize main parameters
self.chatComm = chatComm
self.name = name
self.width = 800
self.height = 600
self.fps = 20
self.title = "Fight the Professor! By Eric Gao"
self.bgColor = (255, 255, 255)
self.bg = pygame.image.load('./imgs/bg/tartanbg.png')
self.bg = pygame.transform.scale(self.bg, (self.width, self.height))
# initialize game object
self.Game = Game
if self.Game.p2.name == self.name:
self.player = self.Game.p2
else:
self.player = self.Game.p3
self.objs = []
self.cardDict = {}
self.selectedCards = []
self.chosenLandlord = False
pygame.init()
self.run()
# send game to other two players
def sendGame(self, mod=0):
encoded = self.Game.encodeGame(mod)
target = [self.Game.p1.name, self.Game.p2.name, self.Game.p3.name]
target.remove(self.name)
for i in target:
self.chatComm.sendMessage(i, encoded)
# update game from received game object
def updateGame(self):
messages = self.chatComm.getMail()[0]
if messages != []:
for i in messages:
info = i[1].replace('\n', '').replace(' ', '').split('#')
info = info[1:]
info[3] = convertHelper(info[3])
info[6] = convertHelper(info[6])
info[9] = convertHelper(info[9])
info[11] = anotherConvertHelper(info[11])
info[12] = ast.literal_eval(info[12])
info[13] = convertHelper(info[13])
if info[0] in ['0', '1', '2']:
self.Game = self.decodeGame(info)
if self.Game.p2.name == self.name:
self.player = self.Game.p2
else:
self.player = self.Game.p3
# decode game info into new game object
def decodeGame(self, gameInfo):
newGame = Game(gameInfo[1], gameInfo[4], gameInfo[7])
newGame.p1.identity = gameInfo[2]
newGame.p2.identity = gameInfo[5]
newGame.p3.identity = gameInfo[8]
if gameInfo[2] == 'p' or gameInfo[5] == 'p' or gameInfo[8] == 'p':
self.chosenLandlord = True
newGame.p1.cards = gameInfo[3]
newGame.p2.cards = gameInfo[6]
newGame.p3.cards = gameInfo[9]
newGame.currentPlayer = gameInfo[10]
newGame.prevPlay = gameInfo[11]
newGame.playOrder = gameInfo[12]
newGame.landLordCards = gameInfo[13]
if newGame.p2.name == self.name:
self.player = self.Game.p2
else:
self.player = self.Game.p3
if gameInfo[0] == '1':
if self.player.identity == 'p':
tkinter.Tk().wm_withdraw() # to hide the main window
tkinter.messagebox.showinfo(
f'Winner is: {newGame.prevPlayer}', 'CONGRATS PROFESSOR! KEEP OPPRESSING YOUR STUDENTS!')
pygame.quit()
else:
tkinter.Tk().wm_withdraw() # to hide the main window
tkinter.messagebox.showinfo(
f'Winner is: {newGame.prevPlayer}', 'YOU LOST! FIGHT HARDER AGAINST YOUR PROFESSOR!')
pygame.quit()
elif gameInfo[0] == '2':
if self.player.identity == 's':
tkinter.Tk().wm_withdraw() # to hide the main window
tkinter.messagebox.showinfo(
f'Winner is: {newGame.prevPlayer}', 'CONGRATS STUDENTS! KILL MORE PROFESSORS!')
pygame.quit()
else:
tkinter.Tk().wm_withdraw() # to hide the main window
tkinter.messagebox.showinfo(
f'Winner is: {newGame.prevPlayer}', 'YOU LOST! BECOME BETTER AT BEING AN EVIL PROFESSOR!')
pygame.quit()
return newGame
# confirm proofessor identity
def confirmIdentity(self):
self.updateGame()
self.Game.chooseLandlord(self.name)
self.chosenLandlord = True
self.Game.assignPlayOrder()
self.updateScreen()
self.sendGame()
# pass professor identity
def passIdentity(self):
self.Game.makePlay([])
self.updateScreen()
self.sendGame()
# select cards
def selectCard(self, cardVal):
if cardVal not in self.selectedCards and cardVal in self.player.cards:
self.selectedCards.append(cardVal)
# deselect cards
def deSelect(self, cardVal):
if cardVal in self.selectedCards and cardVal in self.player.cards:
self.selectedCards.remove(cardVal)
# confirm card play
def confirmCard(self):
if self.selectedCards != [] and self.Game.isValidPlay(self.selectedCards):
self.Game.makePlay(self.selectedCards)
self.selectedCards = []
mod = self.Game.checkWin()
if mod == 1: # current player wins as professor
self.sendGame(1)
if self.Game.playerDict[self.name].identity == 'p':
tkinter.Tk().wm_withdraw() # to hide the main window
tkinter.messagebox.showinfo(
f'Winner is: {self.Game.prevPlayer}', 'CONGRATS PROFESSOR! KEEP OPPRESSING YOUR STUDENTS!')
pygame.quit()
else:
tkinter.Tk().wm_withdraw() # to hide the main window
tkinter.messagebox.showinfo(
f'Winner is: {self.Game.prevPlayer}', 'YOU LOST! FIGHT HARDER AGAINST YOUR PROFESSOR!')
pygame.quit()
elif mod == 2: # current player wins as student
self.sendGame(2)
if self.Game.playerDict[self.name].identity == 's':
tkinter.Tk().wm_withdraw() # to hide the main window
tkinter.messagebox.showinfo(
f'Winner is: {self.Game.prevPlayer}', 'CONGRATS STUDENTS! KILL MORE PROFESSORS!')
pygame.quit()
else:
tkinter.Tk().wm_withdraw() # to hide the main window
tkinter.messagebox.showinfo(
f'Winner is: {self.Game.prevPlayer}', 'YOU LOST! BECOME BETTER AT BEING AN EVIL PROFESSOR!')
pygame.quit()
else:
self.sendGame(0) # game continues
elif self.Game.prevPlay == []: # show error
tkinter.Tk().wm_withdraw()
tkinter.messagebox.showwarning('Warning', 'Invalid play!')
self.updateScreen()
# pass turn
def passCard(self):
if self.selectedCards == []:
self.Game.makePlay([])
self.updateScreen()
self.sendGame()
# update screen from game object
def updateScreen(self):
# clear everything
self.objs.clear()
self.updateGame()
# update hand cards
for i in self.cardDict:
if i not in self.player.cards:
self.cardDict[i] = None
xStart = 50
cardCnt = len(self.player.cards)
for card in self.player.cards:
if card not in self.cardDict:
cardObj = Card(self.screen, card, xStart, 430, 50, 70, lambda x=card: self.selectCard(
x), lambda x=card: self.deSelect(x))
self.cardDict[card] = cardObj
else:
cardObj = self.cardDict[card]
cardObj.x = xStart
if cardObj not in self.objs:
self.objs.append(cardObj)
xStart += 700/cardCnt
# update played cards
xStart = 230
cardCnt = len(self.Game.prevPlay[1])
if cardCnt != 0:
for card in self.Game.prevPlay[1]:
prevPlayObj = Card(self.screen, card, xStart, 180, 50, 70)
self.objs.append(prevPlayObj)
xStart += 350/cardCnt
# update landlord cards
if self.chosenLandlord == True:
xStart = 320
for card in self.Game.landLordCards:
landlordCardObj = Card(self.screen, card, xStart, 40, 50, 70)
self.objs.append(landlordCardObj)
xStart += 200/3
# update current player
text = f"Current playing: {self.Game.currentPlayer}"
currentPlayerTextObj = Text(self.screen, text, 230, 120, 350, 50)
self.objs.append(currentPlayerTextObj)
# update avatars and positions
myPos = self.Game.playOrder.index(self.name)
if myPos == 0:
self.prevPlayer = Player(
self.screen, self.Game.playerDict[self.Game.playOrder[2]], 50, 70, 60, 20, self.chosenLandlord)
self.objs.append(self.prevPlayer)
self.nextPlayer = Player(
self.screen, self.Game.playerDict[self.Game.playOrder[1]], 700, 70, 60, 20, self.chosenLandlord)
self.objs.append(self.nextPlayer)
self.myPlayer = Player(
self.screen, self.Game.playerDict[self.Game.playOrder[0]], 370, 570, 60, 20, self.chosenLandlord)
self.objs.append(self.myPlayer)
self.prevImg = Img(
self.screen, self.Game.playerDict[self.Game.playOrder[2]], 50, 10, 60, 60)
self.objs.append(self.prevImg)
self.afterImg = Img(
self.screen, self.Game.playerDict[self.Game.playOrder[1]], 700, 10, 60, 60)
self.objs.append(self.afterImg)
self.myImg = Img(
self.screen, self.Game.playerDict[self.Game.playOrder[0]], 370, 510, 60, 60)
self.objs.append(self.myImg)
self.prevCardCnt = Text(
self.screen, str(len(self.Game.playerDict[self.Game.playOrder[2]].cards)), 70, 90, 20, 20)
self.objs.append(self.prevCardCnt)
self.afterCardCnt = Text(
self.screen, str(len(self.Game.playerDict[self.Game.playOrder[1]].cards)), 720, 90, 20, 20)
self.objs.append(self.afterCardCnt)
elif myPos == 1:
self.prevPlayer = Player(
self.screen, self.Game.playerDict[self.Game.playOrder[0]], 50, 70, 60, 20, self.chosenLandlord)
self.objs.append(self.prevPlayer)
self.nextPlayer = Player(
self.screen, self.Game.playerDict[self.Game.playOrder[2]], 700, 70, 60, 20, self.chosenLandlord)
self.objs.append(self.nextPlayer)
self.myPlayer = Player(
self.screen, self.Game.playerDict[self.Game.playOrder[1]], 370, 570, 60, 20, self.chosenLandlord)
self.objs.append(self.myPlayer)
self.prevImg = Img(
self.screen, self.Game.playerDict[self.Game.playOrder[0]], 50, 10, 60, 60)
self.objs.append(self.prevImg)
self.afterImg = Img(
self.screen, self.Game.playerDict[self.Game.playOrder[2]], 700, 10, 60, 60)
self.objs.append(self.afterImg)
self.myImg = Img(
self.screen, self.Game.playerDict[self.Game.playOrder[1]], 370, 510, 60, 60)
self.objs.append(self.myImg)
self.prevCardCnt = Text(
self.screen, str(len(self.Game.playerDict[self.Game.playOrder[0]].cards)), 70, 90, 20, 20)
self.objs.append(self.prevCardCnt)
self.afterCardCnt = Text(
self.screen, str(len(self.Game.playerDict[self.Game.playOrder[2]].cards)), 720, 90, 20, 20)
self.objs.append(self.afterCardCnt)
else:
self.prevPlayer = Player(
self.screen, self.Game.playerDict[self.Game.playOrder[1]], 50, 70, 60, 20, self.chosenLandlord)
self.objs.append(self.prevPlayer)
self.nextPlayer = Player(
self.screen, self.Game.playerDict[self.Game.playOrder[0]], 700, 70, 60, 20, self.chosenLandlord)
self.objs.append(self.nextPlayer)
self.myPlayer = Player(
self.screen, self.Game.playerDict[self.Game.playOrder[2]], 370, 570, 60, 20, self.chosenLandlord)
self.objs.append(self.myPlayer)
self.prevImg = Img(
self.screen, self.Game.playerDict[self.Game.playOrder[1]], 50, 10, 60, 60)
self.objs.append(self.prevImg)
self.afterImg = Img(
self.screen, self.Game.playerDict[self.Game.playOrder[0]], 700, 10, 60, 60)
self.objs.append(self.afterImg)
self.myImg = Img(
self.screen, self.Game.playerDict[self.Game.playOrder[2]], 370, 510, 60, 60)
self.objs.append(self.myImg)
self.prevCardCnt = Text(
self.screen, str(len(self.Game.playerDict[self.Game.playOrder[1]].cards)), 70, 90, 20, 20)
self.objs.append(self.prevCardCnt)
self.afterCardCnt = Text(
self.screen, str(len(self.Game.playerDict[self.Game.playOrder[0]].cards)), 720, 90, 20, 20)
self.objs.append(self.afterCardCnt)
# update buttons
if self.chosenLandlord and self.Game.currentPlayer == self.name:
self.passCardButton = Button(
self.screen, 100, 350, 100, 50, 'Pass turn', self.passCard)
self.objs.append(self.passCardButton)
self.confirmCardButton = Button(
self.screen, 600, 350, 100, 50, 'Confirm Play', self.confirmCard)
self.objs.append(self.confirmCardButton)
elif not self.chosenLandlord and self.Game.currentPlayer == self.name:
self.passButton = Button(
self.screen, 100, 350, 100, 50, 'Pass', self.passIdentity)
self.objs.append(self.passButton)
self.confirmButton = Button(
self.screen, 600, 350, 100, 50, 'Be Professor', self.confirmIdentity)
self.objs.append(self.confirmButton)
# initialize game GUI
def initGUI(self):
# set basic variables
self.clock = pygame.time.Clock()
self.screen = pygame.display.set_mode((self.width, self.height))
self.screen.fill(self.bgColor)
pygame.display.set_caption(self.title)
# update screen
self.updateScreen()
# main function to be called
def run(self):
self.updateGame()
self.initGUI()
playing = True
while playing:
self.screen.fill(self.bgColor)
self.screen.blit(self.bg, (0, 0))
self.clock.tick(self.fps)
self.updateScreen()
# to show the initial screen
for obj in self.objs:
obj.process()
for event in pygame.event.get():
if event.type in [pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP]:
for obj in self.objs:
obj.process()
if event.type == pygame.QUIT:
playing = False
for obj in self.objs:
obj.process()
pygame.display.flip()
pygame.quit()
exit()
# start game as client
if __name__ == "__main__":
wnd = tkinter.Tk()
wnd.geometry("800x600")
wnd.title("Fight the Professor!")
wnd.resizable(0, 0)
loginGUIObj = loginGUI(wnd)
wnd.mainloop()