def test_whenCalculatingMagnitude_thenMagnitudeIsCorrect(self): position = Position(X_1, Y_1) magnitude = position.magnitude() expected_magnitude = sqrt(X_1 * X_1 + Y_1 * Y_1) self.assertEqual(expected_magnitude, magnitude)
def __init__(self, ride): self.index = Ride.current_index Ride.current_index += 1 self.start_position = Position(ride[0], ride[1]) self.end_position = Position(ride[2], ride[3]) self.start_time = int(ride[4]) self.end_time = int(ride[5])
def test_whenInversingPosition_thenCoordiantesAreInversed(self): position = Position(X_1, Y_1) inversed_position = position.inverse() expected_position = Position(-X_1, -Y_1) self.assertEqual(expected_position, inversed_position)
def test_should_return_dot_product_with_other_point(self): # noqa: D102 pos = Position(9, 12) other_pos = Position(2, -3) dot_product = pos.dot_product(other_pos) self.assertEquals(Position(18, -36), dot_product)
def test_getters(self): position = Position(1, 2) x = position.get_horizontal_position() y = position.get_vertical_position() self.assertEqual(x, 1) self.assertEqual(y, 2)
def test_should_add_to_other_point(self): # noqa: D102 pos = Position(4, 5) other_pos = Position(6, 7) result = pos.add(other_pos) self.assertEquals(Position(10, 12), result)
def __init__(self): self.number = Car.current_number Car.current_number += 1 self.position = Position(0, 0) self.taken_rides_indexes = [] self.possible_rides = [] self.busy_until = 0
def __init__(self, file_path): self.coordinates = [] with open(file_path) as input_file: for line in input_file: position = Position() position.deserialize(line) self.coordinates.append(position)
def __init__(self, size): """Construct a square TetrisGrid with the given size.""" # Initialise grid squares grid_squares = [] for column_index in range(0, size): column = [] for row_index in range(0, size): column.append(GridSquare()) grid_squares.append(column) super(TetrisGrid, self).__init__( attrs_dict={ "_size": size, "x_bounds": (0, size - 1), "y_bounds": (0, size - 1), "grid_squares": grid_squares }) # Populate grid with fixed elements for row_num in range(*self.y_bounds): # Left Wall self._get_grid_square(Position(self.x_bounds[0], row_num)).fill_with( Element(ElementType.WALL)) # Right wall self._get_grid_square(Position(self.y_bounds[-1], row_num)).fill_with( Element(ElementType.WALL)) # Floor for col_num in range(*self.y_bounds): self._get_grid_square(Position(col_num, self.y_bounds[0])).fill_with( Element(ElementType.WALL))
def test_should_add_object_when_object_within_bounds(self): # noqa: D102 grid = TetrisGrid(10) # Single element at (5, 5) should fit within wall bounds layout = Layout([Position(0, 0)]) object = TetrisPiece(layout, Position(5, 5)) grid.add_object(object)
def test_defualt_values(self): position = Position() x = position.get_horizontal_position() y = position.get_vertical_position() self.assertEqual(x, 0) self.assertEqual(y, 0)
def test_whenHashingMonsterWithSamePosition_thenHashesAreTheSame(self): monster_1 = NormalMonster(Position(X_1, Y_1), SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME) monster_2 = NormalMonster(Position(X_1, Y_1), SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME) self.assertEqual(hash(monster_1), hash(monster_2))
def test_givenDifferentCoordinates_whenHashingPositions_thenHashesAreNotTheSame( self ): position_1 = Position(X_1, Y_1) position_2 = Position(X_2, Y_1) self.assertNotEqual(hash(position_1), hash(position_2))
class TestCannonGetters(unittest.TestCase): def setUp(self): self.cannon_position = Position(25, 23) self.projectile_position = Position(35, 33) self.angle = 45 self.initial_speed = 100 self.cannon = Cannon(self.cannon_position, self.angle, self.initial_speed, self.projectile_position) def test_horizontal_position(self): true_x = self.cannon_position.get_horizontal_position() cannon_x = self.cannon.get_horizontal_position() self.assertEqual(true_x, cannon_x) def test_vertical_position(self): true_y = self.cannon_position.get_vertical_position() cannon_y = self.cannon.get_vertical_position() self.assertEqual(true_y, cannon_y) def test_width(self): self.assertEqual(self.cannon.get_width(), 150) def test_height(self): self.assertEqual(self.cannon.get_height(), 92) def test_angle(self): self.assertEqual(self.cannon.get_angle(), self.angle) def test_initial_speed(self): self.assertEqual(self.cannon.get_initial_speed(), self.initial_speed)
def test_whenAddingPositions_thenCoordinatesAreAddedToEachOther(self): position_1 = Position(X_1, Y_1) position_2 = Position(X_2, Y_2) actual_position = position_1 + position_2 expected_position = Position(X_1 + X_2, Y_1 + Y_2) self.assertEqual(expected_position, actual_position)
def test_whenSubstractingPositions_thenCoordinatesAreSubstractedToEachOther(self): position_1 = Position(X_1, Y_1) position_2 = Position(X_2, Y_2) actual_position = position_1 - position_2 expected_position = Position(X_1 - X_2, Y_1 - Y_2) self.assertEqual(expected_position, actual_position)
def test_should_calculate_position_relative_to_other_point( self): # noqa: D102 pos = Position(4, 12) other_pos = Position(10, 10) relative_pos = pos.relative_to(other_pos) self.assertEquals(Position(-6, 2), relative_pos)
def test_whenCalculatingDelaPositionFromPosition_thenDeltaPositionIsReturned(self): position_1 = Position(X_1, Y_1) position_2 = Position(X_2, Y_2) world_element = WorldElementTestClass(position_1) delta = world_element.delta_position_from(position_2) expected_delta = position_1 - position_2 self.assertEqual(expected_delta, delta)
def test_should_should_raise_out_of_bounds_exception_when_attempting_to_add_object_overlapping_wall( self): # noqa: D102 grid = TetrisGrid(10) # Single element at (0, 0) overlaps with floor layout = Layout([Position(0, 0)]) object = TetrisPiece(layout, Position(0, 0)) with self.assertRaises(ElementOutOfBoundsException): grid.add_object(object)
def test_should_should_raise_conflict_exception_when_attempting_to_add_overlapping_object( self): # noqa: D102 grid = TetrisGrid(10) layout = Layout([Position(0, 0)]) object = TetrisPiece(layout, Position(5, 5)) grid.add_object(object) with self.assertRaises(ElementConflictException): grid.add_object(object)
def test_whenCalculatingDistanceFromPosition_thenEuclideanDistanceIsReturned(self): position_1 = Position(X_1, Y_1) position_2 = Position(X_2, Y_2) world_element = WorldElementTestClass(position_1) distance = world_element.distance_from(position_2) expected_distance = sqrt((X_2 - X_1) ** 2 + (Y_2 - Y_1) ** 2) self.assertEqual(expected_distance, distance)
def test_grid_should_allow_adding_object_when_object_within_bounds( self): # noqa: D102 grid = TetrisGrid(10) # Single element at (5, 5) should fit within wall bounds layout = Layout([Position(0, 0)]) object = TetrisPiece(layout, Position(5, 5)) can_add_object = grid.can_add_object(object) self.assertTrue(can_add_object)
def process_creature_command_queue(self, creature): actions_to_take = creature.actions_per_turn for action in creature.command_queue[:]: # iterate a copy so we can remove on the fly. if(actions_to_take == 0): return # this creature is out of action points. if(creature.next_action_available > 0): # this creature can't act until x turns from now. creature.next_action_available = creature.next_action_available - 1 return # if we get here we can process a single action if(action.action_type == 'move'): actions_to_take = actions_to_take - 1 # moving costs 1 ap. if(action.args[0] == 'south'): if(self.worldmap.move_object_from_position_to_position(self.players[creature.name], self.players[creature.name].position, Position(self.players[creature.name].position.x, self.players[creature.name].position.y+1, self.players[creature.name].position.z))): self.players[creature.name].position = Position(self.players[creature.name].position.x, self.players[creature.name].position.y+1, self.players[creature.name].position.z) creature.command_queue.remove(action) # remove the action after we process it. if(action.args[0] == 'north'): if(self.worldmap.move_object_from_position_to_position(self.players[creature.name], self.players[creature.name].position, Position(self.players[creature.name].position.x, self.players[creature.name].position.y-1, self.players[creature.name].position.z))): self.players[creature.name].position = Position(self.players[creature.name].position.x, self.players[creature.name].position.y-1, self.players[creature.name].position.z) creature.command_queue.remove(action) # remove the action after we process it. if(action.args[0] == 'east'): if(self.worldmap.move_object_from_position_to_position(self.players[creature.name], self.players[creature.name].position, Position(self.players[creature.name].position.x+1, self.players[creature.name].position.y, self.players[creature.name].position.z))): self.players[creature.name].position = Position(self.players[creature.name].position.x+1, self.players[creature.name].position.y, self.players[creature.name].position.z) creature.command_queue.remove(action) # remove the action after we process it. if(action.args[0] == 'west'): if(self.worldmap.move_object_from_position_to_position(self.players[creature.name], self.players[creature.name].position, Position(self.players[creature.name].position.x-1, self.players[creature.name].position.y, self.players[creature.name].position.z))): self.players[creature.name].position = Position(self.players[creature.name].position.x-1, self.players[creature.name].position.y, self.players[creature.name].position.z) creature.command_queue.remove(action) # remove the action after we process it. if(action.args[0] == 'up'): if(self.worldmap.move_object_from_position_to_position(self.players[creature.name], self.players[creature.name].position, Position(self.players[creature.name].position.x, self.players[creature.name].position.y, self.players[creature.name].position.z+1))): self.players[creature.name].position = Position(self.players[creature.name].position.x, self.players[creature.name].position.y, self.players[creature.name].position.z+1) creature.command_queue.remove(action) # remove the action after we process it. if(action.args[0] == 'down'): if(self.worldmap.move_object_from_position_to_position(self.players[creature.name], self.players[creature.name].position, Position(self.players[creature.name].position.x, self.players[creature.name].position.y, self.players[creature.name].position.z-1))): self.players[creature.name].position = Position(self.players[creature.name].position.x, self.players[creature.name].position.y, self.players[creature.name].position.z-1) creature.command_queue.remove(action) # remove the action after we process it. elif(action.action_type == 'bash'): actions_to_take = actions_to_take - 1 # bashing costs 1 ap. if(action.args[0] == 'south'): self.worldmap.bash(self.players[creature.name], Position(self.players[creature.name].position.x, self.players[creature.name].position.y+1, self.players[creature.name].position.z)) self.localmaps[creature.name] = self.worldmap.get_chunks_near_position(self.players[creature.name].position) creature.command_queue.remove(action) # remove the action after we process it. if(action.args[0] == 'north'): self.worldmap.bash(self.players[creature.name], Position(self.players[creature.name].position.x, self.players[creature.name].position.y-1, self.players[creature.name].position.z)) self.localmaps[creature.name] = self.worldmap.get_chunks_near_position(self.players[creature.name].position) creature.command_queue.remove(action) # remove the action after we process it. if(action.args[0] == 'east'): self.worldmap.bash(self.players[creature.name], Position(self.players[creature.name].position.x+1, self.players[creature.name].position.y, self.players[creature.name].position.z)) self.localmaps[creature.name] = self.worldmap.get_chunks_near_position(self.players[creature.name].position) creature.command_queue.remove(action) # remove the action after we process it. if(action.args[0] == 'west'): self.worldmap.bash(self.players[creature.name], Position(self.players[creature.name].position.x-1, self.players[creature.name].position.y, self.players[creature.name].position.z)) self.localmaps[creature.name] = self.worldmap.get_chunks_near_position(self.players[creature.name].position) creature.command_queue.remove(action) # remove the action after we process it.
def test_deserialization(self): serialized = '(5, 25)' position = Position(2, 22) position.deserialize(serialized) x = position.get_horizontal_position() y = position.get_vertical_position() self.assertEqual(x, 5) self.assertEqual(y, 25)
def get_adjacent_positions(self, position): adjacencies = [] for offset in [-1, 1]: if 0 <= position["x"] + offset < self.x_max: adjacencies.append( Position(position["x"] + offset, position["y"], position["z"]) ) if 0 <= position["y"] + offset < self.y_max: adjacencies.append( Position(position["x"], position["y"] + offset, position["z"]) ) return adjacencies
def test_givenCoordinatesUnderBoundValues_whenBoundingPosition_thenPositionIsBoundedToValues( self ): x_bound = 0 y_bound = 0 x = -1 y = -1 bounded_position = Position(x, y).bound(x_bound, y_bound) expected_position = Position(x_bound, y_bound) self.assertEqual(expected_position, bounded_position)
def test_object_should_have_valid_move(self): # noqa: D102 grid = TetrisGrid(10) layout = Layout( [Position(0, 0), Position(1, 0), Position(0, 1), Position(1, 1)]) object = TetrisPiece(layout, Position(4, 8)) has_valid_move = grid.object_has_valid_move(object) self.assertTrue(has_valid_move)
def test_givenDeltaPosition_whenMovingMonster_thenMonsterIsMovedByDeltaPosition( self): initial_position = Position(X_1, Y_1) delta_position = Position(X_2, Y_2) moved_monster = NormalMonster(initial_position, SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME).move_by(delta_position) expected_monster = NormalMonster(Position(X_1 + X_2, Y_1 + Y_2), SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME) self.assertEqual(hash(expected_monster), hash(moved_monster))
def magnified(self, times): """Return a new verson of this Layout magnified by the given factor.""" if times > 1: positions = [] for position in self.positions: positions.append(position) positions.append(position.add(Position(1, 0))) positions.append(position.add(Position(0, 1))) positions.append(position.add(Position(1, 1))) return Layout(set(positions)).magnified(times - 1) else: return Layout(set(self.positions))
def test_object_should_not_have_valid_move_when_already_on_floor( self): # noqa: D102 grid = TetrisGrid(10) layout = Layout( [Position(0, 0), Position(1, 0), Position(0, 1), Position(1, 1)]) object = TetrisPiece(layout, Position(5, 0)) has_valid_move = grid.object_has_valid_move(object) self.assertFalse(has_valid_move)
def test_updating_values(self): position = Position(2, 22) position.set_horizontal_position(0) position.set_vertical_position(1) position.deserialize('(7, 8)') x = position.get_horizontal_position() y = position.get_vertical_position() self.assertEqual(x, 7) self.assertEqual(y, 8)
def __init__(self): pygame.init() pygame.display.set_caption('Mario') pygame.mixer.music.load(Game.MAIN_THEME_PATH) pygame.mixer.music.play(-1) self.screen = pygame.display.set_mode(RESOLUTION) self.tps_clock = pygame.time.Clock() self.tps_delta = 0.0 self.run = True self.player = Player() self.board = Board(Game.WORLD_PATH) self.position = Position((0, 0)) self.jumpSound = pygame.mixer.Sound(Game.JUMP_SOUND_PATH) self.coinSound = pygame.mixer.Sound(Game.COIN_SOUND_PATH) self.execute()
def __init__(self, x, y, z, chunk_size): self["tiles"] = list() self["weather"] = "WEATHER_NONE" # weather is per chunk. # the tile represented on the over map self["overmap_tile"] = "open_air" # set this to true to have the changes updated on the disk, default is True so worldgen writes it to disk self["is_dirty"] = True self["was_loaded"] = False # start = time.time() for i in range(chunk_size): # 0-13 for j in range(chunk_size): # 0-13 chunkdict = {} # this position is on the worldmap. no position is ever repeated. each chunk tile gets its own position. chunkdict["position"] = Position(i + int(x * chunk_size), j + int(y * chunk_size), z) if int(z) <= 0: # make the earth chunkdict["terrain"] = Terrain("t_dirt") else: # make the air chunkdict["terrain"] = Terrain("t_open_air") # one creature per tile chunkdict["creature"] = None # can be zero to many items in a tile. chunkdict["items"] = [] # single furniture per tile chunkdict["furniture"] = None # one per tile chunkdict["vehicle"] = None # used in lightmap calculations, use 1 for base so we never have total darkness. chunkdict["lumens"] = 1 self["tiles"].append(chunkdict)
def __init__( self, x, y, z, chunk_size): # x, y, z relate to it's position on the world map. self.tiles = [] self.weather = "WEATHER_NONE" # weather is per chunk. self.overmap_tile = "open_air" # the tile represented on the over map self.is_dirty = ( True ) # set this to true to have the changes updated on the disk, default is True so worldgen writes it to disk self.was_loaded = "no" # start = time.time() for i in range(chunk_size): # 0-13 for j in range(chunk_size): # 0-13 chunkdict = {} chunkdict["position"] = Position( i + int(x * chunk_size), j + int(y * chunk_size), z ) # this position is on the worldmap. no position is ever repeated. each chunk tile gets its own position. if int(z) <= 0: chunkdict["terrain"] = Terrain("t_dirt") # make the earth else: chunkdict["terrain"] = Terrain( "t_open_air") # make the air chunkdict[ "creature"] = None # Creature() # one creature per tile chunkdict["items"] = [] # can be more then one item in a tile. chunkdict["furniture"] = None # single furniture per tile chunkdict["vehicle"] = None # one per tile chunkdict["trap"] = None # one per tile chunkdict[ "bullet"] = None # one per tile (TODO: figure out a better way) chunkdict["lumens"] = ( 1 ) # used in lightmap calculations, use 1 for base so we never have total darkness. self.tiles.append(chunkdict)
def flipped_horizontally(self): """Return a new verson of this Layout flipped horizontally about its origin.""" positions = [] for position in self.positions: positions.append(position.dot_product(Position(-1, 1))) return Layout(positions)
def setUp(self): self.cannon_position = Position(25, 23) self.projectile_position = Position(35, 33) self.angle = 45 self.initial_speed = 100 self.cannon = Cannon(self.cannon_position, self.angle, self.initial_speed, self.projectile_position)
def __init__(self): super(Zombie, self).__init__() self.images_esq = [] self.images_esq.append(pygame.image.load("img/zumbi_esquerdo_fechado.png")) self.images_esq.append(pygame.image.load("img/zumbi_esquerdo_aberto.png")) self.images_dir = [] self.images_dir.append(pygame.image.load("img/zumbi_direito_fechado.png")) self.images_dir.append(pygame.image.load("img/zumbi_direito_aberto.png")) self.images = self.images_dir self.index = 0 self.DELAY_TIME = 20 self.delay_time_index = 0 self.image = self.images[self.index] self.width = self.image.get_rect().size[0] self.height = self.image.get_rect().size[1] x = CONST.DISPLAY_SIZE_X/2 - self.width/2 y = CONST.DISPLAY_SIZE_Y/2 - self.height/2 self.pos = Position(x,y) # ver se é necessário depois essa linha self.rect = pygame.Rect(x,y,self.width,self.height) self.speed = 6
def test_setters(self): position = Position() position.set_horizontal_position(21) position.set_vertical_position(42) x = position.get_horizontal_position() y = position.get_vertical_position() self.assertEqual(x, 21) self.assertEqual(y, 42)
def test_to_tuple(self): position = Position(23, 47) self.assertEqual(position.to_tuple(), (23, 47))
def test_serialization(self): position = Position(5, 25) self.assertEqual(position.serialize(), '(5, 25)')