def get_input(self): if pygame_failed or Settings.TURN_OFF_GUI: return for event in pygame.event.get(): if event.type == pygame.QUIT: self._game.send_quit() self._game.stop() break elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self._game.send_quit() self._game.stop() break elif event.key == pygame.K_UP: self._messages.append(Messages.player_input('up')) elif event.key == pygame.K_DOWN: self._messages.append(Messages.player_input('down')) elif event.key == pygame.K_LEFT: self._messages.append(Messages.player_input('left')) elif event.key == pygame.K_RIGHT: self._messages.append(Messages.player_input('right')) elif event.key == pygame.K_a: self._messages.append(Messages.player_input('up')) elif event.key == pygame.K_z: self._messages.append(Messages.player_input('down')) elif event.key == pygame.K_SPACE: self._messages.append(Messages.player_input('space'))
def _use_ai(self): if not Settings.CLIENT_USE_AI: return if self._player_number not in self._light_cycles: return command = self._client_ai.tick(self._tick_number, self._ai_number_of_commands_received) if command == 'left': self._connection.send_message( Messages.tick(self._tick_number, self._game_number, [Messages.player_input('left')])) return if command == 'double_left': self._connection.send_message( Messages.tick(self._tick_number, self._game_number, [ Messages.player_input('left'), Messages.player_input('left') ])) return if command == 'right': self._connection.send_message( Messages.tick(self._tick_number, self._game_number, [Messages.player_input('right')])) return if command == 'double_right': self._connection.send_message( Messages.tick(self._tick_number, self._game_number, [ Messages.player_input('right'), Messages.player_input('right') ])) return
def _phase_load_cycles(self): if self._game_over: return logging.debug('_phase_load_cycles, _client_start_time: ' + str(self._client_start_time) + ' _game_number: ' + str(self._game_number) + ' _player_number: ' + str(self._player_number)) waiting_for_server_ticks = 0 while self._keep_running: self._user_input.get_input() self._connection.poll(Settings.TICK / 2.0, 2) if 1 in self._messages_on_tick: self._process_messages() break if waiting_for_server_ticks > Settings.CLIENT_SECONDS_TO_WAIT / Settings.TICK: self._game_over = True logging.debug( '_phase_load_cycles failed, _client_start_time: ' + str(self._client_start_time) + ' _game_number: ' + str(self._game_number) + ' _player_number: ' + str(self._player_number)) break waiting_for_server_ticks += 1 if not self._keep_running: return self._connection.send_message( Messages.start_game(self._tick_number, self._game_number)) self._connection.poll(0, 2) for key in self._light_cycles: if key == self._player_number: continue self._light_cycles[key].set_color(Colors.CYCLE_COLORS[key]) self._light_cycles[key].set_trail_color(Colors.CYCLE_COLORS[key]) self._text = None self._show_instructions = False self._update_display() self._user_input.get_input() self._user_input.get_and_delete_messages() if Settings.CLIENT_USE_AI: self._client_ai = Ai(False, self._light_cycles, self._player_number) self._connection.send_message( Messages.tick(self._tick_number, self._game_number, [Messages.player_input('space')])) self._connection.send_message( Messages.start_game(self._tick_number, self._game_number)) self._connection.poll(0, 2) waiting_for_server_ticks = 0 while self._keep_running: self._user_input.get_input() self._connection.poll(Settings.TICK / 2.0, 2) if 4 in self._messages_on_tick: break if waiting_for_server_ticks > Settings.CLIENT_SECONDS_TO_WAIT / Settings.TICK: self._game_over = True logging.debug( '_phase_load_cycles failed, _client_start_time: ' + str(self._client_start_time) + ' _game_number: ' + str(self._game_number) + ' _player_number: ' + str(self._player_number)) break waiting_for_server_ticks += 1 waiting_for_server_ticks = 0