def test_single_sprite_collision_default(self, mock_pygame, mock_load_png): """Test that the default bounce calculation is used, the ball speed adjusted, and a collision callback invoked when the ball collides with a single sprites which does not have an associated ball bounce strategy callback. """ mock_sprite = self._configure_mocks(mock_pygame, mock_load_png) mock_on_collide, mock_calc_new_angle = Mock(), Mock() mock_pygame.sprite.spritecollide.return_value = [mock_sprite] ball = Ball((100, 100), 2.36, 8) ball.add_collidable_sprite(mock_sprite, speed_adjust=0.5, on_collide=mock_on_collide) ball._calc_new_angle = mock_calc_new_angle ball.update() self.assertEqual(ball.speed, 8.5) mock_on_collide.assert_called_once_with(mock_sprite, ball) mock_calc_new_angle.assert_called_once_with([mock_sprite.rect])
def test_multiple_sprite_collision(self, mock_pygame, mock_load_png): """Test that the default bounce calculation is used, the ball speed adjusted, and collision callbacks invoked when the ball collides with multiple sprites. """ mock_sprite = self._configure_mocks(mock_pygame, mock_load_png) (mock_bounce, mock_on_collide, mock_calc_new_angle, mock_sprite2, mock_sprite3) = (Mock(), Mock(), Mock(), Mock(), Mock()) mock_pygame.sprite.spritecollide.return_value = [ mock_sprite, mock_sprite2, mock_sprite3 ] mock_bounce.return_value = 3.2 ball = Ball((100, 100), 2.36, 8, top_speed=9) ball.add_collidable_sprite(mock_sprite, bounce_strategy=mock_bounce, speed_adjust=0.5, on_collide=mock_on_collide) ball.add_collidable_sprite(mock_sprite2, bounce_strategy=mock_bounce, speed_adjust=0.5, on_collide=mock_on_collide) ball.add_collidable_sprite(mock_sprite3, bounce_strategy=mock_bounce, speed_adjust=0.5, on_collide=mock_on_collide) ball._calc_new_angle = mock_calc_new_angle ball.update() self.assertEqual(ball.speed, 9.0) # Gone up, but not above top speed. mock_on_collide.assert_has_calls([ call(mock_sprite, ball), call(mock_sprite2, ball), call(mock_sprite3, ball) ]) mock_calc_new_angle.assert_called_once_with( [mock_sprite.rect, mock_sprite2.rect, mock_sprite3.rect])