def test_body_acceleration_zeros(): """ Test update_body_acceleration() using a zero vector """ test_rocket = rm.Rocket() test_rocket.accelerometer = sensors.Accelerometer() test_rocket.orientation = Quaternion(axis=[1, 1, 1], angle=np.pi / 2).elements test_rocket.world_acceleration = np.array([0, 0, 0]) body_acceleration_after_rotate = np.array([0, 0, 0]) new_body_acceleration = test_rocket.accelerometer.update(test_rocket) assert np.all(body_acceleration_after_rotate == new_body_acceleration)
def test_body_acceleration_180_degrees(): """ Test update_body_acceleration() at an angle of 180 degrees """ test_rocket = rm.Rocket() test_rocket.accelerometer = sensors.Accelerometer() test_rocket.orientation = Quaternion(axis=[1, 0, 1], angle=np.pi).elements test_rocket.world_acceleration = np.array([1, 1, 1]) body_acceleration_after_rotate = np.array([1, -1, 1]) new_body_acceleration = test_rocket.accelerometer.update(test_rocket) np.testing.assert_allclose(new_body_acceleration, body_acceleration_after_rotate, atol=rm.TOLERANCE)
def test_body_acceleration_negative_floats(): """ Test update_body_acceleration() using a vector of negative floats """ test_rocket = rm.Rocket() test_rocket.accelerometer = sensors.Accelerometer() test_rocket.orientation = Quaternion(axis=[1, 0, 1], angle=np.pi / 2).elements test_rocket.world_acceleration = np.array([-1.5, -2, -3]) body_acceleration_after_rotate = np.array([-0.8358, 1.0606, -3.6642]) new_body_acceleration = test_rocket.accelerometer.update(test_rocket) np.testing.assert_allclose(new_body_acceleration, body_acceleration_after_rotate, atol=rm.TOLERANCE)
def test_body_acceleration_positive_floats(): """ Test update_body_acceleration() using a vector of positive floats """ test_rocket = rm.Rocket() test_rocket.accelerometer = sensors.Accelerometer() test_rocket.orientation = Quaternion(axis=[1, 0, 1], angle=np.pi / 2).elements test_rocket.world_acceleration = np.array([1.5, 2.5, 3.5]) body_acceleration_after_rotate = np.array([0.7322, -1.4142, 4.2678]) new_body_acceleration = test_rocket.accelerometer.update(test_rocket) np.testing.assert_allclose(new_body_acceleration, body_acceleration_after_rotate, atol=rm.TOLERANCE)
def test_body_acceleration_negative_integers(): """ Test update_body_acceleration() using a vector of negative integers """ test_rocket = rm.Rocket() test_rocket.accelerometer = sensors.Accelerometer() test_rocket.orientation = Quaternion(axis=[1, 0, 1], angle=np.pi / 2).elements test_rocket.world_acceleration = np.array([-1, -1, -3]) body_acceleration_after_rotate = np.array([-1.2929, 1.4142, -2.7071]) new_body_acceleration = test_rocket.accelerometer.update(test_rocket) np.testing.assert_allclose(new_body_acceleration, body_acceleration_after_rotate, atol=rm.TOLERANCE)
def __init__(self, activity): super().__init__() # note what time it was when we first launched self.game_start_time = time.time() # the activity is used to communicate with other players self._activity = activity # keep a list of all local players self.localplayers = [] # start with just one player player = Player(activity.owner) self.localplayers.append(player) # plus some bonus players (all hidden to start with) self.localplayers.extend(player.bonusPlayers()) # keep a dictionary of all remote players, indexed by handle self.remoteplayers = {} # keep a list of all players, local and remote, self.allplayers = [] + self.localplayers screen = Gdk.Screen.get_default() self.aspectRatio = float(screen.width()) / screen.height() # start with a small maze using a seed that will be different # each time you play state = activity.state if state is None: height = 9 width = int(height * self.aspectRatio) if width % 2 == 0: width -= 1 state = { 'seed': int(time.time()), 'height': height, 'width': width, 'risk': 0 } if 'finish_time' in state and state['finish_time'] is not None: # the maze was alread played, reset it to start a new one state['seed'] = int(time.time()) logging.debug('Starting the game with: %s', state) self.maze = Maze(state['seed'], state['width'], state['height'], state['risk']) self._ebook_mode_detector = sensors.EbookModeDetector() self._finish_window = None self.reset() self.frame = 0 self._show_trail = True self._cached_surface = None # support arrow keys, game pad arrows and game pad buttons # each set maps to a local player index and a direction self.arrowkeys = { # real key: (localplayer index, ideal key) 'Up': (0, 'Up'), 'Down': (0, 'Down'), 'Left': (0, 'Left'), 'Right': (0, 'Right'), 'KP_Up': (1, 'Up'), 'KP_Down': (1, 'Down'), 'KP_Left': (1, 'Left'), 'KP_Right': (1, 'Right'), 'KP_Page_Up': (2, 'Up'), 'KP_Page_Down': (2, 'Down'), 'KP_Home': (2, 'Left'), 'KP_End': (2, 'Right') } Gdk.Screen.get_default().connect('size-changed', self.__configure_cb) self.connect('draw', self.__draw_cb) self.connect('size-allocate', self.__size_allocate_cb) self.connect('event', self.__event_cb) self.set_events(Gdk.EventMask.EXPOSURE_MASK | Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK | Gdk.EventMask.BUTTON_MOTION_MASK | Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.POINTER_MOTION_HINT_MASK | Gdk.EventMask.KEY_PRESS_MASK | Gdk.EventMask.TOUCH_MASK) self.set_can_focus(True) self.grab_focus() self._accelerometer = sensors.Accelerometer() self._read_accelerator_id = None if self._ebook_mode_detector.get_ebook_mode(): self._activity.show_accelerator_alert() self._start_accelerometer() self._ebook_mode_detector.connect('changed', self._ebook_mode_changed_cb)