Пример #1
0
    def __init__(self, **kwargs):
        GridLayout.__init__(self, cols=kwargs['grid_size'][0], **kwargs)
        self.layout = kwargs['layout']
        self.grid_size = kwargs['grid_size']
        self.effects_propability = kwargs['effects_propability']
        self.tiles = []
        self.changes = []
        self.frozen = False
        self.tile_resized = 0
        self.tiles_amount = self.grid_size[0] * self.grid_size[1]

        for i, tile_type in enumerate(self.layout):
            ix = i % self.grid_size[0]
            iy = i // self.grid_size[0]
            tile = GameTile(type=tile_type, ix=ix, iy=iy)
            self.add_widget(tile)
            self.tiles.append(tile)

        self.aimap = AIMap(self)
Пример #2
0
    def __init__(self, **kwargs):
        GridLayout.__init__(self, cols=kwargs['grid_size'][0], **kwargs)
        self.layout = kwargs['layout']
        self.grid_size = kwargs['grid_size']
        self.effects_propability = kwargs['effects_propability']
        self.tiles = []
        self.changes = []
        self.frozen = False
        self.tile_resized = 0
        self.tiles_amount = self.grid_size[0] * self.grid_size[1]

        for i, tile_type in enumerate(self.layout):
            ix = i % self.grid_size[0]
            iy = i // self.grid_size[0]
            tile = GameTile(type=tile_type, ix=ix, iy=iy)
            self.add_widget(tile)
            self.tiles.append(tile)

        self.aimap = AIMap(self)
Пример #3
0
class GameGrid(GridLayout):
    def __init__(self, **kwargs):
        GridLayout.__init__(self, cols=kwargs['grid_size'][0], **kwargs)
        self.layout = kwargs['layout']
        self.grid_size = kwargs['grid_size']
        self.effects_propability = kwargs['effects_propability']
        self.tiles = []
        self.changes = []
        self.frozen = False
        self.tile_resized = 0
        self.tiles_amount = self.grid_size[0] * self.grid_size[1]

        for i, tile_type in enumerate(self.layout):
            ix = i % self.grid_size[0]
            iy = i // self.grid_size[0]
            tile = GameTile(type=tile_type, ix=ix, iy=iy)
            self.add_widget(tile)
            self.tiles.append(tile)

        self.aimap = AIMap(self)

    def restart(self):
        self.aimap.reset()
        self.frozen = False
        for i, tile_type in enumerate(self.layout):
            self.tiles[i].type = tile_type
            self.tiles[i].restart()

    def on_tile_size(self):
        self.tile_resized += 1
        if self.tile_resized == self.tiles_amount:
            self.on_load()

    def on_load(self):
        pass

    def freeze(self):
        self.frozen = True

    def add_change(self, tile):
        self.changes.append(tile)

    def get_tile(self, ix, iy):
        index = (self.grid_size[0] * iy + ix)
        if index >= len(self.tiles) or index < 0:
            Logger.debug(
                'GameGrid: get_tile(%d,%d) index is %d (min: 0, max: %d)' %
                (ix, iy, index, len(self.tiles) - 1))
            return None
        return self.tiles[index]

    def get_tile_indexes(self, x, y):
        width, height = self.parent.tile_size
        return (int(x / width), int(self.grid_size[1] - (y / height)))

    def find_path(self, from_tuple, to_tuple):
        #print 'from:', from_tuple
        #print 'to:  ', to_tuple

        start = self.get_tile(*from_tuple)
        destination = self.get_tile(*to_tuple)

        if destination is None or destination.is_obstacle():
            return []

        open_set = [start]
        path_map = [[None for x in range(self.grid_size[0])]
                    for y in range(self.grid_size[1])]
        path_map[start.iy][start.ix] = 0

        end = False

        while open_set and not end:
            tile = open_set.pop()
            steps = path_map[tile.iy][tile.ix]

            for neighbor in tile.tiles_around():
                if neighbor is None:
                    continue
                if not neighbor.is_obstacle():
                    current_steps = path_map[neighbor.iy][neighbor.ix]
                    if current_steps is None or current_steps > steps + 1:
                        path_map[neighbor.iy][neighbor.ix] = steps + 1
                        if neighbor == destination:
                            end = True
                            break
                        open_set.insert(0, neighbor)

        if not end:
            return []

        path = [destination]
        tile = destination

        if tile is None:
            return []

        steps = path_map[tile.iy][tile.ix]

        while steps and steps != 1:
            for item in tile.tiles_around():
                if path_map[item.iy][item.ix] == steps - 1:
                    path.append(item)
                    tile = item
                    steps = steps - 1
                    break

        return path
Пример #4
0
class GameGrid(GridLayout):
    def __init__(self, **kwargs):
        GridLayout.__init__(self, cols=kwargs['grid_size'][0], **kwargs)
        self.layout = kwargs['layout']
        self.grid_size = kwargs['grid_size']
        self.effects_propability = kwargs['effects_propability']
        self.tiles = []
        self.changes = []
        self.frozen = False
        self.tile_resized = 0
        self.tiles_amount = self.grid_size[0] * self.grid_size[1]

        for i, tile_type in enumerate(self.layout):
            ix = i % self.grid_size[0]
            iy = i // self.grid_size[0]
            tile = GameTile(type=tile_type, ix=ix, iy=iy)
            self.add_widget(tile)
            self.tiles.append(tile)

        self.aimap = AIMap(self)

    def restart(self):
        self.aimap.reset()
        self.frozen = False
        for i, tile_type in enumerate(self.layout):
            self.tiles[i].type = tile_type
            self.tiles[i].restart()

    def on_tile_size(self):
        self.tile_resized += 1
        if self.tile_resized == self.tiles_amount:
            self.on_load()
            
    def on_load(self):
        pass

    def freeze(self):
        self.frozen = True

    def add_change(self, tile):
        self.changes.append(tile)

    def get_tile(self, ix, iy):
        index = (self.grid_size[0]*iy + ix)
        if index >= len(self.tiles) or index < 0:
            Logger.debug(
                    'GameGrid: get_tile(%d,%d) index is %d (min: 0, max: %d)'
                    % (ix, iy, index, len(self.tiles) - 1)
            )
            return None
        return self.tiles[index]

    def get_tile_indexes(self, x, y):
        width, height = self.parent.tile_size
        return (int(x/width), int(self.grid_size[1] - (y/height)))

    def find_path(self, from_tuple, to_tuple):
        #print 'from:', from_tuple
        #print 'to:  ', to_tuple

        start = self.get_tile(*from_tuple)
        destination = self.get_tile(*to_tuple)

        if destination is None or destination.is_obstacle():
            return []

        open_set = [start]
        path_map = [[None for x in range(self.grid_size[0])] for y in range(self.grid_size[1])]
        path_map[start.iy][start.ix] = 0

        end = False

        while open_set and not end:
            tile = open_set.pop()
            steps = path_map[tile.iy][tile.ix]

            for neighbor in tile.tiles_around():
                if neighbor is None:
                    continue
                if not neighbor.is_obstacle():
                    current_steps = path_map[neighbor.iy][neighbor.ix]
                    if current_steps is None or current_steps > steps + 1:
                        path_map[neighbor.iy][neighbor.ix] = steps + 1
                        if neighbor == destination:
                            end = True
                            break
                        open_set.insert(0, neighbor)

        if not end:
            return []

        path = [destination]
        tile = destination

        if tile is None:
            return []

        steps = path_map[tile.iy][tile.ix]

        while steps and steps != 1:
            for item in tile.tiles_around():
                if path_map[item.iy][item.ix] == steps - 1:
                    path.append(item)
                    tile = item
                    steps = steps - 1 
                    break
    
        return path