예제 #1
0
    def test_grid_to_string_returns_correct_string(self):
        tile_grid = []
        for row in range(0, 9):
            row_array = []
            for col in range(0, 9):
                tile = Tile(row, col)
                tile.value = self.grid[row][col]
                row_array.append(tile)
            tile_grid.append(row_array)

        self.assertEqual(self.formatter.grid_to_string(tile_grid), self.string)
예제 #2
0
    def gen_terrain(self):
        #TODO: calculate jump and alpha based on planet properties
        size = Planet.sizes[str(self.size)]

        # Jump determines how closely the perlin noise is sampled,
        # and therefore, how small or large mountains and valleys are.
        # It's a normal distribution whose mean varies according to
        # solar distance. The farther from the sun the planet is,
        # the larger mountains and valleys, so the easier it is for
        # starting players.
        jump = normalvariate(0.07 - ((self.solar_distance * 2 - 1) * 0.02),
                             0.014)
        heights = utils.gen_height_map(size, jump)
        minerals = self.get_minerals(heights)
        humidities = [[round((1 - i) * self.water_richness, 2) for i in j]
                      for j in heights]

        #how many layers of abstraction are u on?
        terrain = [[
            Tile(height=tile[0], minerals=tile[1], humidity=tile[2])
            for tile in row
        ] for row in [
            list(zip(heights[i], minerals[i], humidities[i]))
            for i in range(len(heights))
        ]]
        return terrain
 def __create_board(self, height, width):
     board = []
     for i in range(height):
         board.append([])
         for j in range(width):
             board[i].append(Tile(i, j))
     
     return board
예제 #4
0
    def _int_array_to_tile_array(self, int_grid):
        """[summary]

        Args:
            int_grid (2-dimensional array of int): Sudoku grid with int values

        Returns:
            [2-dimensional array of Tile]: Sudoku grid with Tile objects
        """
        tile_grid = []
        for row in range(0, 9):
            row_array = []
            for col in range(0, 9):
                tile = Tile(row, col)
                tile.value = int_grid[row][col]
                row_array.append(tile)
            tile_grid.append(row_array)
        return tile_grid
예제 #5
0
    def _set_tiles(self, sudoku):
        """Formats an array of integers into an array of Tiles.

        Args:
            sudoku (2-dimensional array of int): array representation of a sudoku

        Returns:
            [2-dimensional array of Tiles]: an array of Tiles
        """
        sudoku_array = []
        for row in range(self.board_length):
            column_array = []
            for column in range(self.board_length):
                tile = Tile(row, column)
                tile.value = sudoku[row][column]
                if tile.value > 0:
                    tile.initial = True
                column_array.append(tile)
            sudoku_array.append(column_array)
        return sudoku_array
예제 #6
0
class TestTile(unittest.TestCase):
    def setUp(self):
        self.tile = Tile(3, 4)

    def test_tile_initialisation(self):
        self.assertEqual(self.tile.row, 3)
        self.assertEqual(self.tile.column, 4)
        self.assertEqual(self.tile.value, 0)

    def test_tile_value_change_works_on_empty_tile(self):
        self.assertTrue(self.tile.change_value(5))
        self.assertEqual(self.tile.value, 5)

    def test_tile_value_change_does_not_work_on_initial_value(self):
        self.tile.value = 5
        self.tile.initial = True
        self.assertFalse(self.tile.change_value(2))
        self.assertEqual(self.tile.value, 5)

    def test_tile_string_returns_expected_value(self):
        self.assertEqual(str(self.tile), "")
        self.tile.value = 5
        self.assertEqual(str(self.tile), "5")
예제 #7
0
 def setUp(self):
     self.tile = Tile(3, 4)
예제 #8
0
def initialise_level(level_map,
                     all_sprites,
                     tiles_group,
                     barriers_group,
                     enemies_group,
                     doors_group,
                     torches_group,
                     end_of_level,
                     user_seed=None):
    """
    Функция для инициализации уровня
    Проходит по переданной ей карте уровня и на каждый символ карты создает тайл и что-то на нем, если есть
    Если передается сид, монстры будут такие, как записано в сиде
    Разные тайлы пола, предметов на уровне (бочек, коробок) будут всегда разные (только текстура)

    :param level_map: Уровень
    :param all_sprites: Группа со всеми спрайтами
    :param barriers_group: Группа для спрайтов с тайлами, сквозь которые нельзя ходить
    :param enemies_group: Группа врагов
    :param doors_group: Группа дверей
    :param torches_group: Группа с факелами
    :param end_of_level: Группа тайла лестницы вниз, при касании с которым произойдет переход на следующий уровень
    :param user_seed: Сид, по которому будут расставлены монстры

    :return Player: Игрок, размещённый в нужном месте
    :return monster_seed: Враги, размещённые в нужном месте
    """
    new_player = None
    seed = []
    level_map = [list(i) for i in level_map]

    # Установка общих физических объектов для всех сущностей
    Entity.set_global_collisions_group(barriers_group)
    # Установка общих физических объектов для всех заклинаний
    Spell.set_global_collisions_group(barriers_group, doors_group)

    for y in range(len(level_map)):
        for x in range(len(level_map[y])):
            if level_map[y][x] == 'P':
                if true_with_chance(CRACKED_FLOOR_CHANCE):
                    Tile(choice(['.0', '.1', '.2', '.3']), x, y, all_sprites,
                         tiles_group)
                else:
                    Tile('.', x, y, all_sprites, tiles_group)
                # Помещаем игрока в центр текущего тайла
                new_player = Player(x * TILE_SIZE + TILE_SIZE * 0.5,
                                    y * TILE_SIZE + TILE_SIZE * 0.5,
                                    all_sprites)
                Tile(level_map[y][x], x, y, all_sprites, tiles_group)
            elif level_map[y][x] in 'M':
                if true_with_chance(CRACKED_FLOOR_CHANCE):
                    Tile(choice(['.0', '.1', '.2', '.3']), x, y, all_sprites,
                         tiles_group)
                else:
                    Tile('.', x, y, all_sprites, tiles_group)
                random_monster(x, y, all_sprites, enemies_group, seed,
                               user_seed)
            elif level_map[y][x] in 'F.':
                if true_with_chance(CRACKED_FLOOR_CHANCE):
                    Tile(choice(['.0', '.1', '.2', '.3']), x, y, all_sprites,
                         tiles_group)
                else:
                    Tile('.', x, y, all_sprites, tiles_group)
            elif level_map[y][x] in 'BC':
                if true_with_chance(CRACKED_FLOOR_CHANCE):
                    Tile(choice(['.0', '.1', '.2', '.3']), x, y, all_sprites,
                         tiles_group)
                else:
                    Tile('.', x, y, all_sprites, tiles_group)
                Tile(('B', 'B1',
                      'C')[true_with_chance(50) + true_with_chance(40)], x, y,
                     all_sprites, barriers_group)
            # Если добавим сундуки
            #
            # elif level_map[y][x] == 'C':
            #     if true_with_chance(CRACKED_FLOOR_CHANCE):
            #         Tile(choice(['.0', '.1', '.2', '.3']), x, y, all_sprites)
            #     else:
            #         Tile('.', x, y, all_sprites)
            #     Tile(level_map[y][x], x, y, all_sprites, barriers_group)

            elif level_map[y][x] in '1234567890-=':
                Tile(level_map[y][x], x, y, all_sprites, barriers_group)
            elif level_map[y][x] in ['r', 'b', 'l', 't', 'T']:
                if true_with_chance(CRACKED_FLOOR_CHANCE):
                    Tile(choice(['.0', '.1', '.2', '.3']), x, y, all_sprites,
                         tiles_group)
                else:
                    Tile('.', x, y, all_sprites, tiles_group)

                if level_map[y][x] == 'l':
                    Door(x - 0.5, y, all_sprites, doors_group)
                elif level_map[y][x] == 't':
                    Door(x, y - 0.5, all_sprites, doors_group)
                elif level_map[y][x] == 'T':
                    Torch(x + 0.12, y, all_sprites, torches_group)
            elif level_map[y][x] == 'E':
                Tile('E', x, y, all_sprites, end_of_level)
            elif level_map[y][x] != ' ':
                Tile(level_map[y][x], x, y, all_sprites, tiles_group)
    # вернем игрока и сид монстров
    return new_player, seed