-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_client.py
262 lines (201 loc) · 8.92 KB
/
match_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
import pygame
import pygame.locals
from pygase import Client
### SETUP ###
# Subclass pygase classes to scope event handlers and game-specific variables.
class ChaseClient(Client):
def __init__(self):
super().__init__()
self.player_id = None
self.status = "lobby"
self.match_id =None
#register stuff
self.register_event_handler("PLAYER_CREATED", self.on_player_created)
self.register_event_handler("JOINED_QUE", self.on_joined_que)
self.register_event_handler("JOINED_GAME", self.on_joined_game)
# "PLAYER_CREATED" event handler
def on_player_created(self, player_id):
# Remember the id the backend assigned the player.
self.player_id = player_id
print(player_id)
# "JOINED_QUE" event handler
def on_joined_que(self, status):
self.status = status
print(self.status)
# "JOINED_GAME" event handler
def on_joined_game(self, match_id):
self.status = "match"
game_loop_is_running = False
self.match_id == match_id
# Create a client.
client = ChaseClient()
def display():
mouse = pygame.mouse.get_pos()
pygame.display.set_caption("PvP Pac-Man")
WIN.fill(BLACK)
WIN.blit(BACKGROUND1, (0,0))
#"Playername" Button
pygame.draw.rect(WIN, LIGHT_GREY, [PLAYERNAME_POS_X, PLAYERNAME_POS_Y, PLAYERNAME_SIZE_X, PLAYERNAME_SIZE_Y])
playername = FONT2.render(user_text, True, BLACK)
WIN.blit(PLAYERNAME_TITLE, (PLAYERNAME_POS_X, PLAYERNAME_POS_Y - 32))
WIN.blit(playername, (PLAYERNAME_POS_X, PLAYERNAME_POS_Y+10))
#"Find Game" button
pygame.draw.rect(WIN,LIGHT_GREY,[FIND_GAME_POS_X, FIND_GAME_POS_Y, FIND_GAME_SIZE_X, FIND_GAME_SIZE_Y])
if FIND_GAME_POS_X <= mouse[0] <= FIND_GAME_POS_X + FIND_GAME_SIZE_X and FIND_GAME_POS_Y <= mouse[1] <= FIND_GAME_POS_Y + FIND_GAME_SIZE_Y:
pygame.draw.rect(WIN, GREEN, [FIND_GAME_POS_X, FIND_GAME_POS_Y, FIND_GAME_SIZE_X, FIND_GAME_SIZE_Y])
WIN.blit(FIND_GAME, (FIND_GAME_POS_X + FIND_GAME_SIZE_X/5.5, FIND_GAME_POS_Y + FIND_GAME_SIZE_Y/3))
#"Quit Button"
pygame.draw.rect(WIN, LIGHT_GREY, [QUIT_POS_X, QUIT_POS_Y, QUIT_SIZE_X, QUIT_SIZE_Y])
if QUIT_POS_X <= mouse[0] <= QUIT_POS_X + QUIT_SIZE_X and QUIT_POS_Y <= mouse[1] <= QUIT_POS_X + QUIT_SIZE_Y:
pygame.draw.rect(WIN, RED, [QUIT_POS_X, QUIT_POS_Y, QUIT_SIZE_X, QUIT_SIZE_Y])
WIN.blit(QUIT, (QUIT_POS_X + QUIT_SIZE_X/2.75, QUIT_POS_Y + QUIT_SIZE_Y/3))
#"Tutorial" Button
pygame.draw.rect(WIN, LIGHT_GREY, [TUTORIAL_POS_X, TUTORIAL_POS_Y, TUTORIAL_SIZE_X, TUTORIAL_SIZE_Y])
if TUTORIAL_POS_X <= mouse[0] <= TUTORIAL_POS_X + TUTORIAL_SIZE_X and TUTORIAL_POS_Y <= mouse[1] <= TUTORIAL_POS_Y + TUTORIAL_SIZE_Y:
pygame.draw.rect(WIN, GOLD, [TUTORIAL_POS_X, TUTORIAL_POS_Y, TUTORIAL_SIZE_X, TUTORIAL_SIZE_Y])
WIN.blit(TUTORIAL, (TUTORIAL_POS_X + TUTORIAL_SIZE_X/4, TUTORIAL_POS_Y + TUTORIAL_SIZE_Y/3))
pygame.display.update()
def find_game_screen():
mouse = pygame.mouse.get_pos()
pygame.display.set_caption("PvP Pac-Man - Finding Game")
WIN.fill(BLACK)
WIN.blit(BACKGROUND2, (0,0))
pygame.draw.rect(WIN, LIGHT_GREY, [QUIT_POS_X, QUIT_POS_Y, QUIT_SIZE_X, QUIT_SIZE_Y])
if QUIT_POS_X <= mouse[0] <= QUIT_POS_X + QUIT_SIZE_X and QUIT_POS_Y <= mouse[1] <= QUIT_POS_X + QUIT_SIZE_Y:
pygame.draw.rect(WIN, RED, [QUIT_POS_X, QUIT_POS_Y, QUIT_SIZE_X, QUIT_SIZE_Y])
WIN.blit(QUIT, (QUIT_POS_X + QUIT_SIZE_X/2.75, QUIT_POS_Y + QUIT_SIZE_Y/3))
pygame.display.update()
def tutorial_screen():
mouse = pygame.mouse.get_pos()
pygame.display.set_caption("PvP Pac-Man - Tutorial")
WIN.fill(BLACK)
WIN.blit(BACKGROUND4, (0,0))
pygame.draw.rect(WIN, LIGHT_GREY, [BACK_POS_X, BACK_POS_Y, BACK_SIZE_X, BACK_SIZE_Y])
if BACK_POS_X <= mouse[0] <= BACK_POS_X + BACK_SIZE_X and BACK_POS_Y <= mouse[1] <= BACK_POS_Y + BACK_SIZE_Y:
pygame.draw.rect(WIN, GOLD, [BACK_POS_X, TUTORIAL_POS_Y, TUTORIAL_SIZE_X, TUTORIAL_SIZE_Y])
WIN.blit(BACK, (BACK_POS_X + BACK_SIZE_X/2.75, BACK_POS_Y + BACK_SIZE_Y/2.75))
pygame.display.update()
def match_found_screen():
pygame.display.set_captaion("PvP Pac-Man - Match Found!")
WIN.fill(BLACK)
WIN.blit(BACKGROUND3, (0,0))
pygame.display.update()
def main():
clock = pygame.time.Clock()
run = True
display_main = True
display_tutorial = False
display_find_game = False
while run:
mouse = pygame.mouse.get_pos()
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT: #windows quit button
run = False
#quit
if event.type == pygame.MOUSEBUTTONDOWN and (display_main or display_find_game):
if QUIT_POS_X <= mouse[0] <= QUIT_POS_X + QUIT_SIZE_X and QUIT_POS_Y <= mouse[1] <= QUIT_POS_Y + QUIT_SIZE_Y: #if mouse click is within this area
run = False
if event.type == pygame.MOUSEBUTTONDOWN and display_main:
if TUTORIAL_POS_X <= mouse[0] <= TUTORIAL_POS_X + TUTORIAL_SIZE_X and TUTORIAL_POS_Y <= mouse[1] <= TUTORIAL_POS_Y + TUTORIAL_SIZE_Y:
display_main = False
display_tutorial = True
tutorial_screen()
if event.type == pygame.MOUSEBUTTONDOWN and display_tutorial:
if BACK_POS_X <= mouse[0] <= BACK_POS_X + BACK_SIZE_X and BACK_POS_Y <= mouse[1] <= BACK_POS_Y + BACK_SIZE_Y:
display_main = True
display_tutorial = False
#playername
if event.type == pygame.KEYDOWN and display_main: #enter playername
global user_text
if event.key == pygame.K_BACKSPACE:
user_text = user_text[:-1]
else:
if len(user_text) < 10:
user_text += event.unicode
#find game
if event.type == pygame.MOUSEBUTTONDOWN and display_main:
if FIND_GAME_POS_X <= mouse[0] <= FIND_GAME_POS_X + FIND_GAME_SIZE_X and FIND_GAME_POS_Y <= mouse[1] <= FIND_GAME_POS_Y + FIND_GAME_SIZE_Y:
client.dispatch_event("CREATE", user_text)
# Wait until "PLAYER_CREATED" has been handled.
while client.player_id is None:
pass
client.dispatch_event("JOIN_QUE", client.player_id)
while client.status == "lobby":
pass
display_main = False
display_find_game = True
find_game_screen()
if client.status == "match":
pass
#match_found_screen()
#TODO: launch actual game
if display_main:
display()
if display_find_game:
find_game_screen()
if display_tutorial:
tutorial_screen()
pygame.quit()
### MAIN PROCESS ###
if __name__ == "__main__":
# Connect the client.
client.connect_in_thread(hostname="localhost", port=8080)
# Set up pygame.
keys_pressed = set() # all keys that are currently pressed down
clock = pygame.time.Clock()
# Initialize a pygame screen.
pygame.init()
pygame.font.init()
#Setting Up
DIMENSIONS = (1000,1000)
WIN = pygame.display.set_mode(DIMENSIONS)
WIDTH = WIN.get_width()
HEIGHT = WIN.get_height()
BACKGROUND1 = pygame.image.load('images/MainScreen.png')
BACKGROUND2 = pygame.image.load('images/LoadingScreen.png')
BACKGROUND3 = pygame.image.load('images/GameFound.png')
BACKGROUND4 = pygame.image.load('images/Tutorial.png')
FPS = 50
#Colors
BLACK = 0,0,0
WHITE = 255,255,255
RED = 255,0,0
GREEN = 0,255,0
LIGHT_GREY = 211,211,211
GOLD = 255, 215, 0
#text
DEFAULT_FONT = pygame.font.get_default_font()
FONT = pygame.font.SysFont(DEFAULT_FONT, 40)
FONT2 = pygame.font.SysFont(DEFAULT_FONT, 80)
FIND_GAME = FONT.render('Find Game', True, BLACK)
QUIT = FONT.render('Quit', True, BLACK)
PLAYERNAME_TITLE = FONT.render("Playername:", True, WHITE)
TUTORIAL = FONT.render('Tutorial', True, BLACK)
BACK = FONT.render('Back', True, BLACK)
user_text = ''
#Buttons
PLAYERNAME_POS_X = 282
PLAYERNAME_POS_Y = 407
PLAYERNAME_SIZE_X = 437
PLAYERNAME_SIZE_Y = 69
FIND_GAME_POS_X = 166
FIND_GAME_POS_Y = 614
FIND_GAME_SIZE_X = 219
FIND_GAME_SIZE_Y = 69
QUIT_POS_X = 603
QUIT_POS_Y = 614
QUIT_SIZE_X = 219
QUIT_SIZE_Y = 69
TUTORIAL_POS_X = 739
TUTORIAL_POS_Y = 892
TUTORIAL_SIZE_X = 219
TUTORIAL_SIZE_Y = 69
BACK_POS_X = 71
BACK_POS_Y = 892
BACK_SIZE_X = 219
BACK_SIZE_Y = 69
main()
pygame.quit()
# Disconnect afterwards and shut down the server if the client is the host.
client.disconnect(shutdown_server=True)