def run(self): last_time = Time.now() while self._scenes.count() > 0: this_time = Time.now() dt = Time.interval_as_float(this_time - last_time) last_time = this_time top_scene = self._scenes.get_scene(-1) # get last scene if top_scene.is_stackable(): for scene_index in range(self._scenes.count()): current_scene = self._scenes.get_scene(scene_index) if current_scene.is_stack_usable() or current_scene == top_scene: self._process_scene(current_scene, dt, scene_index) else: scene_index = self._scenes.count() - 1 last_scene = self._scenes.get_scene(scene_index) self._process_scene(last_scene, dt, scene_index) Console.cls()
def _process_game_state_update(self, client, json_proc): gsup = GameStateUpdateProc(None).from_json(json_proc) ball = self._game_state.ball p1 = gsup.data['game_state'].player1 p2 = gsup.data['game_state'].player2 # Check it this data didn't come after game ended, if so then stop processing it if self._is_game_over(): return # Delta time computing self._this_time = Time.now() if self._last_time is None: self._last_time = self._this_time dt = Time.interval_as_float(self._this_time - self._last_time) self._last_time = self._this_time # Update ball position ball.x += ball.vx * self._ball_speed * dt ball.y += ball.vy * self._ball_speed * dt # Check collision with paddles p1_rect = self.get_player_rect(p1) p2_rect = self.get_player_rect(p2) b_rect = self.get_ball_rect(ball) if self.intersect(p1_rect, b_rect): ball.x = (p1_rect[0] + p1_rect[2]) * 2.0 - 1.0 ball.vx = abs(ball.vx) self._increase_ball_speed_on_hit() elif self.intersect(p2_rect, b_rect): ball.x = p2_rect[0] - b_rect[2] ball.vx = -1.0 * abs(ball.vx) self._increase_ball_speed_on_hit() # Check if ball doesn't touch upper or lower edge and has to bounce if ball.y < -1.0: ball.y = -1.0 ball.vy = -ball.vy if ball.y > 1.0: ball.y = 1.0 ball.vy = -ball.vy # Check if ball didn't escape from game area (leading to end of round) if ball.x < -1.0: ball.reset() self._game_state.player2.pts += 1 self._client_start_round = 0 if ball.x > 1.0: ball.reset() self._game_state.player1.pts += 1 self._client_start_round = 1 # Check if it's not end of game if self._is_game_over(): self._send_game_over_signals() # If it's not, send back updated gameplay state else: # Update ball's state in client's message and broadcast it along gsup.data['game_state'].ball = ball gsup.data['game_state'].player1.pts = self._game_state.player1.pts gsup.data['game_state'].player2.pts = self._game_state.player2.pts self._server.send_all(gsup.to_json())