def _create_receiver(self, ip): index = self.game.add_new_player() receiver = Receiver(ip, index, frame_time()) self.clients.append(receiver) self.clients_by_id[index] = receiver self.clients_by_ip[ip] = receiver self._send_initialization(receiver)
def _save_input(self, client, message): index = client.player_index package_number = message.get("package_number") if client.current_package_number < package_number: client.current_input = create_character_input( message.get("content"), index) client.package_number = package_number client.last_message = frame_time()
def update_character(game, player, character_input): if player.dead and frame_time() - player.death_time >= respawn_cooldown: revive(player, game) if player.dead: return update_location(player, character_input) update_rotation(player, character_input) update_shooting(game, player, character_input)
def check_is_playing(game): if game.winner == None: return True elapsed = frame_time() - game.victory_time if elapsed < rematch_time: return False reset_game(game) return True
def _check_for_dead_connections(self): to_remove = [] for client in self.clients: passed_time = frame_time() - client.last_message if passed_time > disconnect_time: to_remove.append(client) for client in to_remove: self.clients.remove(client) del self.clients_by_id[client.player_index] del self.clients_by_ip[client.ip] self.game.remove_player(client.player_index)
def shoot(game, player): player.last_shot = frame_time() real_rotation = player.rotation * math.pi / -180 bullet = Bullet(player) dx = -1 * math.sin(real_rotation) dy = 1 * math.cos(real_rotation) bullet_direction = gmath.Vector2(dx, dy) * -1 bullet.location = player.location + bullet_direction * 70 bullet.rotation = player.rotation bullet.direction = bullet_direction game.bullets.append(bullet)
from model import Game, Player, update_world from game_time import frame_time, set_frame from network import Socket from transport import TransportHandler width, height = 640, 640 game = Game(width, height) socket = Socket("0.0.0.0", 5001, 5002) transport_handler = TransportHandler(game, socket) last_frame = 0 frame_length = 10 while 1: socket.update() set_frame() dt = frame_time() - last_frame if dt < frame_length: continue last_frame = frame_time() character_inputs = transport_handler.get_inputs() update_world(game, character_inputs) transport_handler.send_state()
def check_victory(game): for player in game.players: if player.kills < victory_condition: continue game.winner = player game.victory_time = frame_time() return
def hit(bullet, player): bullet.to_destroy = True bullet.owner.kills += 1 if bullet.owner != player else 0 player.die(frame_time())
def update_shooting(game, player, character_input): dt = frame_time() - player.last_shot if character_input.shooting and dt >= shoot_cooldown: shoot(game, player)
from input import CharacterInput, init_input, read_input, local_input from output import init_output, update_screen from game_time import frame_time, set_frame from network import Socket from transport import TransportHandler if len(sys.argv) < 2: raise Exception("Give server ip") server_ip = sys.argv[1] pygame.init() socket = Socket("0.0.0.0", 5002, 5001) transport = TransportHandler(socket) set_frame() transport.connect(server_ip, frame_time()) while 1: socket.update() set_frame() game_info = transport.get_game_info(frame_time()) if game_info != None: break local_input() init_input() init_output(game_info.width, game_info.height) last_frame = 0 frame_length = 10