def test_subtract(self): vector1 = ewutils.EwVector2D([1, 2]) vector2 = ewutils.EwVector2D([3, 4]) self.assertEqual(vector1.subtract(vector2).vector, [-2, -2])
def test_scalar_product(self): vector1 = ewutils.EwVector2D([1, 2]) vector2 = ewutils.EwVector2D([3, 4]) self.assertEqual(vector1.scalar_product(vector2), 11)
def test_add(self): vector1 = ewutils.EwVector2D([1, 2]) vector2 = ewutils.EwVector2D([3, 4]) self.assertEqual(vector1.add(vector2).vector, [4, 6])
def test_constructor(self): vector = ewutils.EwVector2D([1, 2]) self.assertTrue(vector) self.assertEqual(vector.vector, [1, 2])
def test_normalize(self): vector = ewutils.EwVector2D([10, 0]) self.assertEqual(vector.normalize().vector, [1, 0])
def test_norm(self): vector = ewutils.EwVector2D([3, 4]) self.assertEqual(vector.norm(), 5)
def move(self): resp_cont = EwResponseContainer(id_server=self.id_server) abs_x = abs(self.velocity[0]) abs_y = abs(self.velocity[1]) abs_sum = abs_x + abs_y if abs_sum == 0: return resp_cont if random.random() * abs_sum < abs_x: move = [self.velocity[0] / abs_x, 0] else: move = [0, self.velocity[1] / abs_y] move_vector = ewutils.EwVector2D(move) position_vector = ewutils.EwVector2D(self.coords) destination_vector = position_vector.add(move_vector) # global sb_games game_data = sports_utils.sb_games.get(self.id_game) player_data = EwPlayer(id_user=self.id_user) response = "" ball_contact = False for i in range(-1, 2): for j in range(-1, 2): neighbor_direction = [i, j] neighbor_vector = ewutils.EwVector2D(neighbor_direction) if move_vector.scalar_product(neighbor_vector) > 0: neighbor_position = position_vector.add(neighbor_vector) if neighbor_position.vector == game_data.ball_coords: ball_contact = True break if ball_contact: game_data.ball_velocity = [round(5 * self.velocity[0]), round(5 * self.velocity[1])] game_data.last_contact = self.id_player self.velocity = [0, 0] response = "{} has kicked the ball in direction {}!".format(player_data.display_name, game_data.ball_velocity) elif game_data.coords_free(destination_vector.vector): self.coords = destination_vector.vector elif game_data.out_of_bounds(destination_vector.vector): self.velocity = [0, 0] response = "{} has walked against the outer bounds and stopped at {}.".format(player_data.display_name, self.coords) else: vel = self.velocity for p in game_data.players: if p.coords == destination_vector.vector: self.velocity = p.velocity p.velocity = vel other_player_data = EwPlayer(id_user=p.id_user) response = "{} has collided with {}.".format(player_data.display_name, other_player_data.display_name) break if len(response) > 0: poi_data = poi_static.id_to_poi.get(game_data.poi) resp_cont.add_channel_response(poi_data.channel, response) return resp_cont
def move_ball(self): resp_cont = EwResponseContainer(id_server=self.id_server) abs_x = abs(self.ball_velocity[0]) abs_y = abs(self.ball_velocity[1]) abs_sum = abs_x + abs_y if abs_sum == 0: return resp_cont move = [self.ball_velocity[0], self.ball_velocity[1]] whole_move_vector = ewutils.EwVector2D(move) response = "" while abs_sum != 0: if random.random() * abs_sum < abs_x: part_move = [move[0] / abs_x, 0] else: part_move = [0, move[1] / abs_y] move_vector = ewutils.EwVector2D(part_move) position_vector = ewutils.EwVector2D(self.ball_coords) destination_vector = position_vector.add(move_vector) player_contact = False for i in range(-1, 2): for j in range(-1, 2): neighbor_direction = [i, j] neighbor_vector = ewutils.EwVector2D(neighbor_direction) if move_vector.scalar_product(neighbor_vector) > 0: neighbor_position = position_vector.add(neighbor_vector) player = self.player_at_coords(neighbor_position.vector) if player != -1: self.ball_velocity = [0, 0] self.last_contact = player player_contact = True break if player_contact: break elif self.coords_free(destination_vector.vector): self.ball_coords = destination_vector.vector elif self.out_of_bounds(destination_vector.vector): for i in range(2): if part_move[i] != 0: whole_move_vector.vector[i] *= -1 self.ball_velocity[i] *= -1 if self.is_goal(): global sb_slimeballerid_to_player scoring_player = sb_slimeballerid_to_player.get(self.last_contact) if scoring_player != None: player_data = EwPlayer(id_user=scoring_player.id_user) else: player_data = None if self.is_goal_purple(): if player_data != None: response = "{} scored a goal for the pink team!".format(player_data.display_name) else: response = "The pink team scored a goal!" self.score_pink += 1 elif self.is_goal_pink(): if player_data != None: response = "{} scored a goal for the purple team!".format(player_data.display_name) else: response = "The purple team scored a goal!" self.score_purple += 1 self.ball_velocity = [0, 0] self.ball_coords = get_starting_position("") self.last_contact = -1 break else: whole_move_vector = whole_move_vector.subtract(move_vector) abs_x = abs(whole_move_vector.vector[0]) abs_y = abs(whole_move_vector.vector[1]) abs_sum = abs_x + abs_y move = whole_move_vector.vector for i in range(2): if self.ball_velocity[i] > 0: self.ball_velocity[i] -= 1 elif self.ball_velocity[i] < 0: self.ball_velocity[i] += 1 if len(response) > 0: poi_data = poi_static.id_to_poi.get(self.poi) resp_cont.add_channel_response(poi_data.channel, response) return resp_cont