-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathgame_client.py
277 lines (221 loc) · 9.71 KB
/
game_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
import tkinter as tk
from tkinter import PhotoImage
from tkinter import messagebox
import socket
from time import sleep
import threading
# MAIN GAME WINDOW
window_main = tk.Tk()
window_main.title("Game Client")
your_name = ""
opponent_name = ""
game_round = 0
game_timer = 4
your_choice = ""
opponent_choice = ""
TOTAL_NO_OF_ROUNDS = 5
your_score = 0
opponent_score = 0
# network client
client = None
HOST_ADDR = "0.0.0.0"
HOST_PORT = 8080
top_welcome_frame= tk.Frame(window_main)
lbl_name = tk.Label(top_welcome_frame, text = "Name:")
lbl_name.pack(side=tk.LEFT)
ent_name = tk.Entry(top_welcome_frame)
ent_name.pack(side=tk.LEFT)
btn_connect = tk.Button(top_welcome_frame, text="Connect", command=lambda : connect())
btn_connect.pack(side=tk.LEFT)
top_welcome_frame.pack(side=tk.TOP)
top_message_frame = tk.Frame(window_main)
lbl_line = tk.Label(top_message_frame, text="***********************************************************").pack()
lbl_welcome = tk.Label(top_message_frame, text="")
lbl_welcome.pack()
lbl_line_server = tk.Label(top_message_frame, text="***********************************************************")
lbl_line_server.pack_forget()
top_message_frame.pack(side=tk.TOP)
top_frame = tk.Frame(window_main)
top_left_frame = tk.Frame(top_frame, highlightbackground="green", highlightcolor="green", highlightthickness=1)
lbl_your_name = tk.Label(top_left_frame, text="Your name: " + your_name, font = "Helvetica 13 bold")
lbl_opponent_name = tk.Label(top_left_frame, text="Opponent: " + opponent_name)
lbl_your_name.grid(row=0, column=0, padx=5, pady=8)
lbl_opponent_name.grid(row=1, column=0, padx=5, pady=8)
top_left_frame.pack(side=tk.LEFT, padx=(10, 10))
top_right_frame = tk.Frame(top_frame, highlightbackground="green", highlightcolor="green", highlightthickness=1)
lbl_game_round = tk.Label(top_right_frame, text="Game round (x) starts in", foreground="blue", font = "Helvetica 14 bold")
lbl_timer = tk.Label(top_right_frame, text=" ", font = "Helvetica 24 bold", foreground="blue")
lbl_game_round.grid(row=0, column=0, padx=5, pady=5)
lbl_timer.grid(row=1, column=0, padx=5, pady=5)
top_right_frame.pack(side=tk.RIGHT, padx=(10, 10))
top_frame.pack_forget()
middle_frame = tk.Frame(window_main)
lbl_line = tk.Label(middle_frame, text="***********************************************************").pack()
lbl_line = tk.Label(middle_frame, text="**** GAME LOG ****", font = "Helvetica 13 bold", foreground="blue").pack()
lbl_line = tk.Label(middle_frame, text="***********************************************************").pack()
round_frame = tk.Frame(middle_frame)
lbl_round = tk.Label(round_frame, text="Round")
lbl_round.pack()
lbl_your_choice = tk.Label(round_frame, text="Your choice: " + "None", font = "Helvetica 13 bold")
lbl_your_choice.pack()
lbl_opponent_choice = tk.Label(round_frame, text="Opponent choice: " + "None")
lbl_opponent_choice.pack()
lbl_result = tk.Label(round_frame, text=" ", foreground="blue", font = "Helvetica 14 bold")
lbl_result.pack()
round_frame.pack(side=tk.TOP)
final_frame = tk.Frame(middle_frame)
lbl_line = tk.Label(final_frame, text="***********************************************************").pack()
lbl_final_result = tk.Label(final_frame, text=" ", font = "Helvetica 13 bold", foreground="blue")
lbl_final_result.pack()
lbl_line = tk.Label(final_frame, text="***********************************************************").pack()
final_frame.pack(side=tk.TOP)
middle_frame.pack_forget()
button_frame = tk.Frame(window_main)
photo_rock = PhotoImage(file=r"rock.gif")
photo_paper = PhotoImage(file = r"paper.gif")
photo_scissors = PhotoImage(file = r"scissors.gif")
btn_rock = tk.Button(button_frame, text="Rock", command=lambda : choice("rock"), state=tk.DISABLED, image=photo_rock)
btn_paper = tk.Button(button_frame, text="Paper", command=lambda : choice("paper"), state=tk.DISABLED, image=photo_paper)
btn_scissors = tk.Button(button_frame, text="Scissors", command=lambda : choice("scissors"), state=tk.DISABLED, image=photo_scissors)
btn_rock.grid(row=0, column=0)
btn_paper.grid(row=0, column=1)
btn_scissors.grid(row=0, column=2)
button_frame.pack(side=tk.BOTTOM)
def game_logic(you, opponent):
winner = ""
rock = "rock"
paper = "paper"
scissors = "scissors"
player0 = "you"
player1 = "opponent"
if you == opponent:
winner = "draw"
elif you == rock:
if opponent == paper:
winner = player1
else:
winner = player0
elif you == scissors:
if opponent == rock:
winner = player1
else:
winner = player0
elif you == paper:
if opponent == scissors:
winner = player1
else:
winner = player0
return winner
def enable_disable_buttons(todo):
if todo == "disable":
btn_rock.config(state=tk.DISABLED)
btn_paper.config(state=tk.DISABLED)
btn_scissors.config(state=tk.DISABLED)
else:
btn_rock.config(state=tk.NORMAL)
btn_paper.config(state=tk.NORMAL)
btn_scissors.config(state=tk.NORMAL)
def connect():
global your_name
if len(ent_name.get()) < 1:
tk.messagebox.showerror(title="ERROR!!!", message="You MUST enter your first name <e.g. John>")
else:
your_name = ent_name.get()
lbl_your_name["text"] = "Your name: " + your_name
connect_to_server(your_name)
def count_down(my_timer, nothing):
global game_round
if game_round <= TOTAL_NO_OF_ROUNDS:
game_round = game_round + 1
lbl_game_round["text"] = "Game round " + str(game_round) + " starts in"
while my_timer > 0:
my_timer = my_timer - 1
print("game timer is: " + str(my_timer))
lbl_timer["text"] = my_timer
sleep(1)
enable_disable_buttons("enable")
lbl_round["text"] = "Round - " + str(game_round)
lbl_final_result["text"] = ""
def choice(arg):
global your_choice, client, game_round
your_choice = arg
lbl_your_choice["text"] = "Your choice: " + your_choice
if client:
client.send("Game_Round"+str(game_round)+your_choice)
enable_disable_buttons("disable")
def connect_to_server(name):
global client, HOST_PORT, HOST_ADDR, your_name
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST_ADDR, HOST_PORT))
client.send(name) # Send name to server after connecting
# disable widgets
btn_connect.config(state=tk.DISABLED)
ent_name.config(state=tk.DISABLED)
lbl_name.config(state=tk.DISABLED)
enable_disable_buttons("disable")
# start a thread to keep receiving message from server
# do not block the main thread :)
threading._start_new_thread(receive_message_from_server, (client, "m"))
except Exception as e:
tk.messagebox.showerror(title="ERROR!!!", message="Cannot connect to host: " + HOST_ADDR + " on port: " + str(HOST_PORT) + " Server may be Unavailable. Try again later")
def receive_message_from_server(sck, m):
global your_name, opponent_name, game_round
global your_choice, opponent_choice, your_score, opponent_score
while True:
from_server = sck.recv(4096)
if not from_server: break
if from_server.startswith("welcome"):
if from_server == "welcome1":
lbl_welcome["text"] = "Server says: Welcome " + your_name + "! Waiting for player 2"
elif from_server == "welcome2":
lbl_welcome["text"] = "Server says: Welcome " + your_name + "! Game will start soon"
lbl_line_server.pack()
elif from_server.startswith("opponent_name$"):
opponent_name = from_server.replace("opponent_name$", "")
lbl_opponent_name["text"] = "Opponent: " + opponent_name
top_frame.pack()
middle_frame.pack()
# we know two users are connected so game is ready to start
threading._start_new_thread(count_down, (game_timer, ""))
lbl_welcome.config(state=tk.DISABLED)
lbl_line_server.config(state=tk.DISABLED)
elif from_server.startswith("$opponent_choice"):
# get the opponent choice from the server
opponent_choice = from_server.replace("$opponent_choice", "")
# figure out who wins in this round
who_wins = game_logic(your_choice, opponent_choice)
round_result = " "
if who_wins == "you":
your_score = your_score + 1
round_result = "WIN"
elif who_wins == "opponent":
opponent_score = opponent_score + 1
round_result = "LOSS"
else:
round_result = "DRAW"
# Update GUI
lbl_opponent_choice["text"] = "Opponent choice: " + opponent_choice
lbl_result["text"] = "Result: " + round_result
# is this the last round e.g. Round 5?
if game_round == TOTAL_NO_OF_ROUNDS:
# compute final result
final_result = ""
color = ""
if your_score > opponent_score:
final_result = "(You Won!!!)"
color = "green"
elif your_score < opponent_score:
final_result = "(You Lost!!!)"
color = "red"
else:
final_result = "(Draw!!!)"
color = "black"
lbl_final_result["text"] = "FINAL RESULT: " + str(your_score) + " - " + str(opponent_score) + " " + final_result
lbl_final_result.config(foreground=color)
enable_disable_buttons("disable")
game_round = 0
# Start the timer
threading._start_new_thread(count_down, (game_timer, ""))
sck.close()
window_main.mainloop()