forked from aelavell/tunneler
/
Client.py
411 lines (353 loc) · 15.4 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
'''
Copyright 2011 Allan Lavell
This file is part of Tunneler2.
Tunneler2 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Tunneler2 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Tunneler2. If not, see <http://www.gnu.org/licenses/>.
'''
import socket
import asyncore
import asynchat
import re
import math
import sys
import pygame
from pygame.locals import *
from GUIConstants import *
from Constants import *
from Grid import *
from PlayerSprite import *
from Viewport import *
from Tools import *
class Client(asynchat.async_chat):
def __init__(self, host, port):
asynchat.async_chat.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.host = host
self.port = port
self.inBuffer = ""
self.outBuffer = ""
self.connect((host,port))
self.outBuffer += "Tunneler1.0;"
self.gameStarted = False
self.grid = False
self.players = []
self.bullets = []
self.playerID = False
self.set_terminator(";")
self.timestep = 0
self.shotAtStep = -10
self.movedAtStep = -10
pygame.init()
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
self.HUDSurf = pygame.Surface((SCREEN_WIDTH, HUD_HEIGHT))
pygame.mouse.set_visible(0)
self.clock = pygame.time.Clock()
pygame.font.init()
self.font = pygame.font.Font(None, FONT_SIZE)
self.titleFont = pygame.font.Font(None, TITLE_FONT_SIZE)
self.displayTitleScreen()
def collect_incoming_data(self, data):
self.inBuffer += data
def found_terminator(self):
self.parseChange(self.inBuffer.strip())
self.inBuffer = ""
self.input()
def handle_connect(self):
pass
def writable(self):
return (len(self.outBuffer) > 0)
def handle_write(self):
sent = self.send(self.outBuffer)
self.outBuffer = self.outBuffer[sent:]
def parseInitialConditions(self, msg):
'''
The first thing the Server will do when the game is signaled to begin is to send
the initial conditions so that each client can set up, getting info about which player
ID its been assigned, how large the grid is, and where the bases are for each player on
the grid.
'''
m = re.match("([0-9]*)\.([0-9]*)\.([0-9]*)\.([0-9]*)\.([0-9]*)\.([0-9]*)\.([0-9]*)\.([0-9]*)", msg)
if m and int(m.group(1)) == GAME_START_CODE:
# So the client knows which player it's displaying information for
playerID = int(m.group(2))
self.playerID = playerID
numRows = int(m.group(3))
numCols = int(m.group(4))
aBaseRow = int(m.group(5))
aBaseCol = int(m.group(6))
bBaseRow = int(m.group(7))
bBaseCol = int(m.group(8))
# The player always starts off in the middle of his base
myPlayer = False
enemyPlayer = False
i = 1
while i < 3:
playerRow = 0
playerCol = 0
if i == 1:
playerRow = aBaseRow + int(math.ceil(BASE_SIZE / 2))
playerCol = aBaseCol + int(math.ceil(BASE_SIZE / 2))
else:
playerRow = bBaseRow + int(math.ceil(BASE_SIZE / 2))
playerCol = bBaseCol + int(math.ceil(BASE_SIZE / 2))
self.players.append(PlayerSprite(i, playerRow, playerCol))
if i == self.playerID:
myPlayer = self.players[i-1]
else:
enemyPlayer = self.players[i-1]
i+= 1
self.grid = Grid(numRows, numCols)
self.grid.addBase(aBaseRow, aBaseCol, A_BASE_UNIT)
self.grid.addBase(bBaseRow, bBaseCol, B_BASE_UNIT)
self.gameStarted = True
self.viewport = Viewport(self.grid, myPlayer, enemyPlayer)
else:
print "invalid initial conditions"
def parseChange(self, msg):
'''
This is where the client picks apart a Change message sent from the server, and applies it
to its own internal representation of what's going on in the game.
It figures out which type of change each one is, then calls methods with the appropriate
arguments for that particular change.
'''
if not self.gameStarted:
self.parseInitialConditions(msg)
else:
# The ; gets removed by the asynchat class
msg += ";"
tm = re.search("([0-9]*);", msg)
changeTimestep = int(tm.group(1))
if self.timestep == changeTimestep:
msg = msg.rstrip(tm.group(0))
m = re.match("([0-9]*\.)*[0-9]*,", msg)
while m:
cmd = m.group(0)
i = 0
type = False
args = []
# take the command apart and get its type and arguments
while cmd:
match = re.match("([0-9]*).", cmd)
if i == 0:
type = int(match.group(1))
else:
args.append(int(match.group(1)))
i += 1
cmd = cmd[len(match.group(0)):]
if type == MOBILE_MOVE_CODE:
self.moveSprite(args[0], args[1], args[2])
elif type == MOBILE_DIRECTION_CODE:
self.makePlayerChangeDirection(args[0], args[1])
elif type == MOBILE_SPAWN_CODE:
self.spawnBullet(args[0], args[1], args[2])
elif type == MOBILE_DIE_CODE:
if args[0] > 2:
self.destroyBullet(args[0])
elif type == DIRT_LOSE_HEALTH_CODE:
self.grid.damageDirt(args[0], args[1], args[2])
elif type == DIRT_DIE_CODE:
self.grid.setObject(args[0], args[1], 0)
elif type == SET_HEALTH_CODE:
self.setPlayerHealth(args[0], args[1])
elif type == SET_ENERGY_CODE:
self.setPlayerEnergy(args[0], args[1])
elif type == GAME_OVER_CODE:
self.gameover(args[0])
msg = msg[len(m.group(0)):]
m = re.match("([0-9]*\.)*[0-9]*,", msg)
self.timestep += 1
self.displayTimestep()
def spawnBullet(self, id, row, col):
bullet = MobileSprite(id, row, col)
bullet.setImage("bullet.png")
self.viewport.addBullet(bullet)
def destroyBullet(self, id):
self.viewport.destroyBulletWithID(id)
def setPlayerHealth(self, id, health):
self.players[id-1].health = health
def setPlayerEnergy(self, id, energy):
self.players[id-1].energy = energy
def moveSprite(self, id, row, col):
if id < 3:
player = self.players[id - 1]
player.row = row
player.col = col
else:
# bullets
for bullet in self.viewport.bullets:
if bullet.id == id:
bullet.row = row
bullet.col = col
def makePlayerChangeDirection(self, id, direction):
self.players[id-1].changeDirection(direction)
def input(self):
'''
This is where the Client gets the player's input to send off to the server.
It gets the keys that the player has pressed, and when the timer goes off,
it packages those commands up and sends them off to the server.
'''
cmd = ""
endInput = False
noInput = False
tick = 0
commandHasMove = False
while not endInput:
movePart = False
shootPart = False
keydown = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
print "Exiting"
self.exit()
if event.type == KEYDOWN:
keydown = event.key
if event.key == LEFT_KEY:
self.movedAtStep = self.timestep
movePart = "%d" % LEFT
elif event.key == RIGHT_KEY:
self.movedAtStep = self.timestep
movePart = "%d" % RIGHT
elif event.key == UP_KEY:
self.movedAtStep = self.timestep
movePart = "%d" % UP
elif event.key == DOWN_KEY:
self.movedAtStep = self.timestep
movePart = "%d" % DOWN
elif event.key == SHOOT_KEY:
if self.shotAtStep not in range(self.timestep - SHOOT_TIMESTEP_WAIT, self.timestep):
self.shotAtStep = self.timestep
shootPart = "%d" % SHOOT
elif event.key == K_ESCAPE:
print "Escape key pressed"
self.exit()
key = pygame.key.get_pressed()
if key[LEFT_KEY]:
if self.movedAtStep not in range(self.timestep - MOVE_TIMESTEP_WAIT, self.timestep):
self.movedAtStep = self.timestep
movePart = "%d" % LEFT
if key[RIGHT_KEY]:
if self.movedAtStep not in range(self.timestep - MOVE_TIMESTEP_WAIT, self.timestep):
self.movedAtStep = self.timestep
movePart = "%d" % RIGHT
if key[UP_KEY]:
if self.movedAtStep not in range(self.timestep - MOVE_TIMESTEP_WAIT, self.timestep):
self.movedAtStep = self.timestep
movePart = "%d" % UP
if key[DOWN_KEY]:
if self.movedAtStep not in range(self.timestep - MOVE_TIMESTEP_WAIT, self.timestep):
self.movedAtStep = self.timestep
movePart = "%d" % DOWN
if key[SHOOT_KEY]:
if self.shotAtStep not in range(self.timestep - SHOOT_TIMESTEP_WAIT, self.timestep):
self.shotAtStep = self.timestep
shootPart = "%d" % SHOOT
tick += self.clock.tick_busy_loop(FPS)
# So the game doesn't wait forever
if tick > TICK_LIMIT:
endInput = True
cmd = ""
if movePart:
cmd += movePart
else:
cmd += "0"
if shootPart:
cmd += shootPart
else:
cmd += "0"
cmd += ".%d;" % self.timestep
self.outBuffer += cmd
def updateHUD(self):
'''
Shows the player's health and energy at the bottom of the screen.
'''
healthFontSurf = self.font.render("Health: %d" % self.viewport.player.health, True, (255, 255, 255))
energyFontSurf = self.font.render("Energy: %d" % self.viewport.player.energy, True, (255, 255, 255))
self.screen.blit(healthFontSurf, (7, VP_HEIGHT + 9))
self.screen.blit(energyFontSurf, (7, VP_HEIGHT + 5 + FONT_SIZE))
def displayTimestep(self):
'''
Gets the viewport to update to display the latest timestep.
'''
self.screen.fill((50, 50, 50))
self.updateHUD()
self.viewport.updateDisplay()
self.screen.blit(self.viewport.surf, (0, 0))
pygame.display.flip()
def displayTitleScreen(self):
'''
When the user first opens the Client, they are smacked in the face with this Title screen.
'''
titleScreenImage = imageLoad("TitleScreen.png")
breakLoop = False
c = 0
while not breakLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print "Exiting"
self.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
print "Escape key pressed"
self.exit()
if event.key == K_RETURN:
self.indicateReadiness()
breakLoop = True
self.screen.fill((0,0,0))
self.screen.blit(titleScreenImage, (0,0))
if not breakLoop:
cs = self.titleFont.render("You are connected to %s." % self.host, False, (255, 255, 255))
es = self.titleFont.render("Press ENTER when you are ready to play.", True, (255, 255, 255))
self.screen.blit(cs, (45, 350))
self.screen.blit(es, (10, 375))
else:
rs = self.font.render("READY...", True, (255, 255, 255))
ws = self.titleFont.render("Waiting for other player...", True, (255,255,255))
self.screen.blit(rs, (102, 230))
self.screen.blit(ws, (70, 270))
pygame.display.flip()
def gameover(self, idOfLoser):
'''
When the game ends, the game displays the full grid and the loser.
'''
self.screen.fill((0,0,0))
renderedFont = False
if idOfLoser == self.viewport.player.id:
renderedFont = self.font.render("LOSS.", False, (255,255,255))
else:
renderedFont = self.font.render("VICTORY.", False, (255,255,255))
helpfulFont = self.titleFont.render("Press ESC to quit.", False, (255,255,255))
self.screen.blit(renderedFont, (5, 5))
self.screen.blit(helpfulFont, (5, 40))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print "Exiting"
self.exit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
print "Escape key pressed"
self.exit()
def indicateReadiness(self):
'''
Tell the server that this client is ready to play a game.
'''
self.outBuffer += "%d;" % GAME_START_CODE
def exit(self):
self.close()
sys.exit(0)
if __name__ == "__main__":
# Usage: python Client.py ip port
if len(sys.argv) >= 3:
c = Client(sys.argv[1], int(sys.argv[2]))
else:
c = Client('localhost', 61673)
asyncore.loop()