-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboxes-client.py
More file actions
275 lines (226 loc) · 9.67 KB
/
Copy pathboxes-client.py
File metadata and controls
275 lines (226 loc) · 9.67 KB
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
import pygame
import math
from PodSixNet.Connection import ConnectionListener, connection
from time import sleep
from env import HOST, PORT
import time
class GameClient(ConnectionListener):
username = None
scores = [0, 0]
player = 0
min_click_time = 0.2
def __init__(self, host, port):
# connect to the server
print('client connecting to {}:{}'.format(host, port))
self.Connect((host, port))
print('Connected!')
if not self.username:
username = input('What is your username?\n')
self.Send({'action': 'unameRes', 'uname': username})
sleep(0.01)
self.last_click = time.time()
self.gameid = None
self.num = None
#put something here that will run when you init the class.
# initiate the main game stuffs
pygame.init()
pygame.font.init()
width, height = 389, 489
#initialize the screen
self.screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Boxes")
#initialize pygame clock
self.clock=pygame.time.Clock()
self.boardh = [[None for x in range(6)] for y in range(7)]
self.boardv = [[None for x in range(7)] for y in range(6)]
self.boxes = [[None for x in range(6)] for y in range(6)]
self.initGraphics()
# to track scores
self.turn = 0
self.didiwin=False
def set_username(self, username):
print('Sending Username to server')
self.Send({'action': 'set_uname', 'uname': username})
def drawBoard(self):
for x in range(6):
for y in range(7):
if self.boardh[y][x] is None:
self.screen.blit(self.none_lineh, [(x)*64+5, (y)*64])
elif self.boardh[y][x] == 0:
self.screen.blit(self.green_lineh, [(x)*64+5, (y)*64])
elif self.boardh[y][x] == 1:
self.screen.blit(self.red_lineh, [(x)*64+5, (y)*64])
for x in range(7):
for y in range(6):
if self.boardv[y][x] is None:
self.screen.blit(self.none_linev, [(x)*64, (y)*64+5])
elif self.boardv[y][x] == 0:
self.screen.blit(self.green_linev, [(x)*64, (y)*64+5])
elif self.boardv[y][x] == 1:
self.screen.blit(self.red_linev, [(x)*64, (y)*64+5])
for x in range(7):
for y in range(7):
self.screen.blit(self.separators, [x*64, y*64])
self.drawHUD()
self.drawOwnermap()
if self.turn == 0:
self.screen.blit(self.green_player, (220, 395))
else:
self.screen.blit(self.red_player, (220, 395))
def drawOwnermap(self):
for x in range(6):
for y in range(6):
if self.boxes[x][y] == 0:
self.screen.blit(self.green_box, [(x)*64+5, (y)*64+5])
if self.boxes[x][y] == 1:
self.screen.blit(self.red_box, [(x)*64+5, (y)*64+5])
def drawHUD(self):
#draw the background for the bottom:
self.screen.blit(self.score_panel, [0, 389])
#create font
myfont = pygame.font.SysFont(None, 32)
#create text surface
if self.turn == self.player:
label = myfont.render("Your Turn:", 1, (255,255,255))
else:
label = myfont.render("Opponent's Turn:", 1, (255,255,255))
#draw surface
self.screen.blit(label, (10, 400))
#same thing here
myfont64 = pygame.font.SysFont(None, 64)
myfont20 = pygame.font.SysFont(None, 20)
scoreme = myfont64.render(str(self.scores[self.player]), 1, (255,255,255))
scoreother = myfont64.render(str(self.scores[(self.player + 1) % 2]), 1, (255,255,255))
scoretextme = myfont20.render("You", 1, (255,255,255))
scoretextother = myfont20.render("Other Player", 1, (255,255,255))
self.screen.blit(scoretextme, (10, 425))
self.screen.blit(scoreme, (10, 435))
self.screen.blit(scoretextother, (280, 425))
self.screen.blit(scoreother, (340, 435))
def initGraphics(self):
self.none_linev = pygame.image.load("boxes/none_line.png")
self.none_lineh = pygame.transform.rotate(pygame.image.load("boxes/none_line.png"), -90)
self.blue_linev = pygame.image.load("boxes/cyan_line.png")
self.blue_lineh = pygame.transform.rotate(pygame.image.load("boxes/cyan_line.png"), -90)
self.green_linev = pygame.image.load("boxes/green_line.png")
self.green_lineh = pygame.transform.rotate(pygame.image.load("boxes/green_line.png"), -90)
self.red_linev = pygame.image.load("boxes/red_line.png")
self.red_lineh = pygame.transform.rotate(pygame.image.load("boxes/red_line.png"), -90)
self.hoverlinev = pygame.image.load("boxes/hoverline.png")
self.hoverlineh = pygame.transform.rotate(pygame.image.load("boxes/hoverline.png"), -90)
# adding separators
self.separators=pygame.image.load("boxes/separators.png")
self.red_player=pygame.image.load("boxes/red_player.png")
self.green_player=pygame.image.load("boxes/green_player.png")
self.green_box=pygame.image.load("boxes/green_box.png")
self.red_box=pygame.image.load("boxes/red_box.png")
self.winningscreen=pygame.image.load("boxes/youwin.png")
self.gameover=pygame.image.load("boxes/gameover.png")
self.drawscreen = pygame.image.load("boxes/draw.png")
self.score_panel=pygame.image.load("boxes/score_panel.png")
def update(self):
connection.Pump()
self.Pump()
#sleep to make the game 60 fps
self.clock.tick(60)
#clear the screen
self.screen.fill(0)
self.drawBoard()
for event in pygame.event.get():
#quit if the quit button was pressed
if event.type == pygame.QUIT:
exit()
# setting up the mouse positioning
# get the position of mouse
mouse = pygame.mouse.get_pos()
# in relation to the grid because each square is 64x64 pixels
xpos = int(math.ceil((mouse[0]-32)/64.0))
ypos = int(math.ceil((mouse[1]-32)/64.0))
# check if the mouse is closer to the horizontal or vertical line
is_horizontal = abs(mouse[1] - ypos*64) < abs(mouse[0] - xpos*64)
# get new postiion on the grid based on is_horizontal variable
ypos = ypos - 1 if mouse[1] - ypos*64 < 0 and not is_horizontal else ypos
xpos = xpos - 1 if mouse[0] - xpos*64 < 0 and is_horizontal else xpos
# initialise the board variable based on whichever is correct
board=self.boardh if is_horizontal else self.boardv
isoutofbounds=False
# draw the hover line, checks if out of bounds. If the line is alr drawn, don't draw the hover line
try:
if board[ypos][xpos] is None: self.screen.blit(self.hoverlineh if is_horizontal else self.hoverlinev, [xpos*64+5 if is_horizontal else xpos*64, ypos*64 if is_horizontal else ypos*64+5])
except:
isoutofbounds=True
pass
if not isoutofbounds:
owner = board[ypos][xpos]
else:
owner = None
# what happens in the game when you click the button
if pygame.mouse.get_pressed()[0] and owner is None and not isoutofbounds:
if time.time() - self.last_click > self.min_click_time:
self.last_click = time.time()
print("click")
self.Send({"action": "place", "x":xpos, "y":ypos, "is_horizontal": is_horizontal, "gameid": self.gameid, "num": self.num,
'turn': self.player})
#update the screen
pygame.display.flip()
def Network_place(self, data):
x = data['x']
y = data['y']
is_h = data['is_h']
player = data['player']
if is_h:
self.boardh[y][x] = player
else:
self.boardv[y][x] = player
def Network_status(self, data):
scores = data['scores']
turn = data['turn']
print('Scores', scores)
print('Turn', turn)
self.turn = turn
self.scores = scores
def Network_startgame(self, data):
self.started = True
self.player = data['player']
self.gameid = data['gameid']
print('Game Started!')
def Network_placefail(self, data):
'''
Called when user tries to place even though it is not his turn
'''
print('place failed')
def Network_end(self, data):
victor = data['victor']
self.finished(victor)
def Network_unameRes(self, data):
print('Returned uname setting')
res = data['res']
uname = data['uname']
if res:
self.uname = uname
print('Username set to {}'.format(uname))
else:
print('Username already taken')
def Network_giveBox(self, data):
print('Giving box')
owner = data['owner']
x = data['x']
y = data['y']
self.boxes[x][y] = owner
self.scores[owner] += 1
def finished(self, victor):
if victor == None:
self.screen.blit(self.drawscreen, (0,0))
elif victor == self.player:
self.screen.blit(self.winningscreen, (0,0))
else:
self.screen.blit(self.gameover, (0,0))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.display.flip()
if __name__ == '__main__':
bg = GameClient(HOST, PORT)
while True:
bg.update()