def connect(self, players=2): self.players = players self.engine = GameEngine() self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.bind((self.host, self.port)) self.server.listen(players) for x in range(players): (client, address) = self.server.accept() self.clients.append((client, address)) client.sendall( Encoder.encode_string( str(self.engine.height * 50) + 'x' + str(self.engine.width * 50))) # SMSG_INIT_DATA contains the map, the number of players and their guids client.sendall(Encoder.encode_int(SMSG_INIT_DATA)) for row in self.engine.raw_map: for data in row: client.sendall(Encoder.encode_int(data)) pl = Player(self.guid_generator, client) pl.position = start_positions[pl.guid] self.engine.players.append(pl) self.engine.objects.append(pl) self.guid_generator += 1 for cl in self.clients: # SMSG_INIT_PLAYERS contains the number of players and the guid of each (plus a boolean to mark if it is the player) cl[0].sendall(Encoder.encode_int(SMSG_INIT_PLAYERS)) cl[0].sendall(Encoder.encode_int(players)) for k in range(players): cl[0].sendall(Encoder.encode_int(self.engine.players[k].guid)) cl[0].sendall( Encoder.encode_int(1 if self.engine.players[k].client == cl[0] else 0)) self.packet_threads = [ Thread(target=self.packets, args=(client[0], )) for client in self.clients ] for th in self.packet_threads: th.start() self.update()
def packets(self): while not self.quit: try: opcode = Decoder.decode_int(self.server.recv(4)) if opcode == SMSG_INIT_DATA: for i in range(self.rows): for j in range(self.cols): self.raw_map[i][j] = Decoder.decode_int( self.server.recv(4)) self.map_ready = True if opcode == SMSG_INIT_PLAYERS: nr_of_players = Decoder.decode_int(self.server.recv(4)) for k in range(nr_of_players): guid = Decoder.decode_int(self.server.recv(4)) is_me = Decoder.decode_int(self.server.recv(4)) pl = Player(guid) pl.position = start_positions[pl.guid] self.objects.append(pl) if is_me == 1: self.player = pl self.game_started = True if opcode == SMSG_UPDATE_POSITION: player_guid = Decoder.decode_int(self.server.recv(4)) pos_x = Decoder.decode_int(self.server.recv(4)) pos_y = Decoder.decode_int(self.server.recv(4)) for obj in self.objects: if obj.guid == player_guid: obj.position = (pos_x, pos_y) if opcode == SMSG_PUT_BOMB_ACK: bomb_guid = Decoder.decode_int(self.server.recv(4)) pos_x = Decoder.decode_int(self.server.recv(4)) pos_y = Decoder.decode_int(self.server.recv(4)) b = Bomb(bomb_guid, (pos_x, pos_y)) self.objects.append(b) if opcode == SMSG_DETONATE_BOMB: bomb_guid = Decoder.decode_int(self.server.recv(4)) hit_units = Decoder.decode_int(self.server.recv(4)) for i in range(hit_units): guid = Decoder.decode_int(self.server.recv(4)) for ob in self.objects: if ob.guid == guid: ob.healths -= 1 ob.invulnerability_timer = 1 dead_players = [] for ob in self.objects: if isinstance(ob, Player): if ob.healths <= 0: dead_players.append(ob.guid) if len(dead_players) != 0: if len(dead_players) > 1: self.game_over = -1 else: self.game_over = 1 if dead_players[0] == 2 else 2 hit_walls = Decoder.decode_int(self.server.recv(4)) for i in range(hit_walls): x = Decoder.decode_int(self.server.recv(4)) y = Decoder.decode_int(self.server.recv(4)) self.raw_map[x][y] = GameEngine.EMPTY bomb = None for obj in self.objects: if obj.guid == bomb_guid: bomb = obj break if bomb is not None: self.objects.append( Explosion( (bomb.position[0] - 50, bomb.position[1] - 50), 100)) self.objects.remove(bomb) if opcode == SMSG_REMOVE_INVULNERABILITY: guid = Decoder.decode_int(self.server.recv(4)) for ob in self.objects: if ob.guid == guid: self.expired_invulnerabilities.append(ob) except: self.quit = True