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)
class Game: ''' The Game encapsulates all of the information about a particular round of tunneler. It knows the state of the grid, the players, which particular timestep we are on, etc. ''' def __init__(self, playerHandlers, numRows, numCols, server): self.timestep = 0 self.changes = "" self.handlersReady = [] self.commands = [] self.bullets = [] self.nextBulletID = 3 self.server = server self.gameAlreadyOver = False self.grid = Grid(numRows, numCols) aBaseRow, aBaseCol = self.grid.createRandomBasePosition() bBaseRow, bBaseCol = self.grid.createRandomBasePosition() self.grid.addBase(aBaseRow, aBaseCol, A_BASE_UNIT) self.grid.addBase(bBaseRow, bBaseCol, B_BASE_UNIT) self.playerHandlers = playerHandlers self.createPlayers(aBaseRow, aBaseCol, bBaseRow, bBaseCol) self.sendInitialGameConditions() def createPlayers(self, aBaseRow, aBaseCol, bBaseRow, bBaseCol): ''' The players start off in the middle of their respective bases. This method creates the objects used to model them. ''' 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.playerHandlers[i-1].player = Player(i, playerRow, playerCol, self) self.playerHandlers[i-1].game = self i+= 1 def sendInitialGameConditions(self): ''' When both clients have connected and are ready to go, the server sends the initial data that each handler requires to create the initial gamestate. They are: Game start code - Indicates to the client that the game is starting Player id - These have been assigned by the server, so the client is just told if it's id 1 or id 2 numRows, numCols - The size of the grid Base Coordinates - The coordinates of the top left corners of the bases ''' print "Send init conditions" for playerHandler in self.playerHandlers: playerHandler.sendMessage("%d.%d.%d.%d.%d.%d.%d.%d;" %(GAME_START_CODE, playerHandler.player.id, self.grid.numRows, self.grid.numCols, self.grid.aBaseRow, self.grid.aBaseCol, self.grid.bBaseRow, self.grid.bBaseCol)) def addChange(self, change): if change != "": self.changes += change def terminateChangesForCurrentTimestep(self): ''' Simply adds the timestep to the change string and the terminating semi-colon. ''' self.changes += "%d;" % self.timestep; print self.changes def executeCommand(self, command): ''' Takes a command object (which was parsed and set up by the playerHandlers), and executes it. ''' player = "" handler = "" for h in self.playerHandlers: if h.player.id == command.id: player = h.player handler = h if command.move != NO_CHANGE: change = player.move(command.move) if change: self.addChange(change) if command.shoot != NO_CHANGE: change = player.shoot() if change: self.addChange(change) def playerAt(self, row, col): ''' Returns the the player at a particular row and col if there is player there, false if no player is at that location. ''' result = False for ph in self.playerHandlers: if ph.player.row == row and ph.player.col == col: result = ph.player break return result def bulletAt(self, row, col): ''' Returns the id of the bullet at a particular row and col if there is player there, false if no bullet is at that location. ''' result = False for bullet in self.bullets: if bullet.row == row and bullet == col: result = bullet.id break return result def spawnBullet(self, row, col, direction): ''' Spawns a new bullet in the grid, assigning it the next available bullet id. If the bullet is spawned on dirt, or another player, it is not actually created, the dirt or the player is simply damaged. ''' change = "" if self.grid.coordsInGrid(row, col): playerHit = self.playerAt(row, col) if self.grid.objectIsDirt(row, col): change = self.grid.damageDirt(row, col, BULLET_DIRT_DAMAGE) elif playerHit: change = playerHit.loseHealth(BULLET_PLAYER_DAMAGE) else: self.bullets.append(Bullet(self.nextBulletID, row, col, direction, self)) change = "%d.%d.%d.%d," %(MOBILE_SPAWN_CODE, self.nextBulletID, row, col) self.nextBulletID += 1 return change def destroyBullet(self, bullet): self.bullets.remove(bullet) return "%d.%d," % (MOBILE_DIE_CODE, bullet.id) def addCommand(self, cmd): ''' Adds a command from a PlayerHandler. There can only be one command per handler per timestep. ''' if len(self.commands) > 0 and cmd.id != self.commands[0].id or len(self.commands) == 0: self.commands.append(cmd) if len(self.commands) == 2: self.nextTimestep() self.commands = [] def nextTimestep(self): ''' This is where the magic happens. All of the commands are executed, any bullets that exist are moved along, and the players either lose or gain energy depending where they are on the grid. All of these changes are documented, and sent across the network to each of hte clients. ''' for cmd in self.commands: self.executeCommand(cmd) for bullet in self.bullets: # Bullets move twice per timestep firstChange = bullet.keepMoving() self.addChange(firstChange) if not re.search("%d.%d" % (MOBILE_DIE_CODE, bullet.id), firstChange): # The bullet should start off 1 unit away from the player when it's firt created if not bullet.justSpawned: self.addChange(bullet.keepMoving()) else: bullet.justSpawned = False for ph in self.playerHandlers: self.addChange(ph.player.passTimestep()) self.terminateChangesForCurrentTimestep() for playerHandler in self.playerHandlers: playerHandler.commitChanges(self.changes) self.changes = "" self.timestep += 1 def gameOver(self, playerWhoLost): ''' Tells the server and the clients that the game has ended. ''' if not self.gameAlreadyOver: self.addChange("%d.%d," % (GAME_OVER_CODE, playerWhoLost.id)) self.terminateChangesForCurrentTimestep() for playerHandler in self.playerHandlers: playerHandler.commitChanges(self.changes) self.server.gameOver() self.gameAlreadyOver = True