-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_start.py
218 lines (190 loc) · 6.92 KB
/
client_start.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
import pygame
import time
import socket
import pickle
import threading
import queue
print("Enter Server IP: ", end ="")
host = input()
print("Joining server at:" , host)
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load("mazeSong.mp3")
pygame.mixer.music.set_volume(0.1)
pygame.mixer.music.play()
font1 = pygame.font.SysFont("comicsansms", 49, True)
font2 = pygame.font.SysFont("comicsansms", 150, True)
font3 = pygame.font.SysFont("comicsansms", 28, True)
screen = pygame.display.set_mode((1395, 1100))
done = False
color = (0, 128, 255) # color of the walls
x = 16
y = 16
victory = False
speed = 3 # movement speed
pause = False
pause_time = 0 # time spent in pause menu
latency = 0
startTime = 0
port = 2001 # socket server port number
client_socket = socket.socket() # instantiate
client_socket.connect((host, port)) # connect to the server
data_queue = queue.Queue()
def client_program(client_socket):
"""
Principal function that receives the initial data from the server (maze, goal, color)
"""
data = b''
# waits to receive entire message based on bytestring
while b"746869736973746865656e64" not in data:
packet = client_socket.recv(4096)
data += packet
data_arr= pickle.loads(data)
return data_arr # close the connection
def data_receiver(client_socket, queue):
"""
Threaded function to handle client coordinate updates from server
:param client_socket: the socket the particular client is using
:param queue: queue containing the updates in order as received
"""
count = 0
#loop for duration of game
while(not done):
data = b''
try:
# wait to receive full message
while b"746869736973746865656e64" not in data:
packet = client_socket.recv(4096)
data += packet
#print("updated chords:", count)
count += 1
stuff = pickle.loads(data)
queue.put(stuff)
except:
pass
latency = time.time() - startTime #in seconds
latency = latency * 100 #in milliseconds
latency = str(latency)[:5]
print(latency, "ms of latency")
data = client_program(client_socket)
maze = data[0]
goal = data[1]
difficulty = data[2]
Loss = False
data_receiver = threading.Thread(target=data_receiver, args=(client_socket, data_queue))
data_receiver.start()
coords = {}
while not done:
if Loss:
screen.fill((0, 0, 0))
victory_text = font2.render("DEFEAT!",True,(255,255,255))
screen.blit(victory_text,(700 - (victory_text.get_width() // 2), 550 - (victory_text.get_height() // 2)))
pygame.display.flip()
oldX = x
oldY= y
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
cords = "quit"
cords = pickle.dumps(cords)
cords += b"746869736973746865656e647373737373737373"
client_socket.send(cords)
print("Thanks for playing.")
pygame.display.quit()
pygame.quit()
if event.type == pygame.KEYDOWN and not done:
if event.key == pygame.K_ESCAPE or event.key == pygame.K_p:
if pause:
pause = False
pause_time += time.time() - pause_time_start
else:
pause = True
pause_time_start = time.time()
if event.key == pygame.K_RETURN:
done = True
if pause and not done:
screen.fill((0, 0, 0))
pause_text = font2.render("PAUSE",True,(255,255,255))
screen.blit(pause_text, (700 - (pause_text.get_width() // 2), 550 - (pause_text.get_height() // 2)))
# the actual game
if not victory and not pause and not Loss and not done:
move_up = True
move_down = True
move_left = True
move_right = True
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP] or pressed[pygame.K_w]:
# checks if their is a overlap with the wall
for m in maze.maze_walls:
player = pygame.Rect(x, y - speed, 10, 10)
if player.colliderect(pygame.Rect(m[0],m[1],m[2],m[3])):
move_up = False
if(difficulty==True):
x = 16
y = 16
break
if move_up:
y -= speed
if pressed[pygame.K_DOWN] or pressed[pygame.K_s]:
player = pygame.Rect(x, y + speed, 10, 10)
for m in maze.maze_walls:
if player.colliderect(pygame.Rect(m[0],m[1],m[2],m[3])):
move_down = False
if(difficulty==True):
x = 16
y = 16
break
if move_down:
y += speed
if pressed[pygame.K_LEFT] or pressed[pygame.K_a]:
player = pygame.Rect(x - speed, y, 10, 10)
for m in maze.maze_walls:
if player.colliderect(pygame.Rect(m[0],m[1],m[2],m[3])):
move_left = False
if(difficulty==True):
x = 16
y = 16
break
if move_left:
x -= speed
if pressed[pygame.K_RIGHT] or pressed[pygame.K_d]:
player = pygame.Rect(x + speed, y, 10, 10)
for m in maze.maze_walls:
if player.colliderect(pygame.Rect(m[0],m[1],m[2],m[3])):
move_right = False
if(difficulty==True):
x = 16
y = 16
break
if move_right:
x += speed
# checks if player has reached the goal
if goal.colliderect((x, y, 10, 10)):
victory = True
# draws the screen
if not oldX == x or not oldY == y:
cords = [x,y]
cords = pickle.dumps(cords)
cords += b"746869736973746865656e647373737373737373"
client_socket.send(cords)
startTime = time.time() #####latency count
try:
coords = data_queue.get_nowait()
except queue.Empty:
pass
for player in coords.values():
if(player == "win"):
Loss = True
else:
pygame.draw.rect(screen, (player[2][0],player[2][1],player[2][2]), pygame.Rect(player[0],player[1],10,10))
pygame.display.flip()
maze.draw(goal)
if victory and not done:
screen.fill((0, 0, 0))
victory_text = font2.render("VICTORY!",True,(255,255,255))
screen.blit(victory_text,(700 - (victory_text.get_width() // 2), 550 - (victory_text.get_height() // 2)))
cords = "win"
cords = pickle.dumps(cords)
cords += b"746869736973746865656e647373737373737373"
client_socket.send(cords)
pygame.display.flip()