Exemplo n.º 1
0
 def event_check(self, event):
     ''' Event checker. Checks if the event is a key press or release on the arrow keys.
     '''
     if event.key == g.key_dict["move_up"][0]:
         self.y_minus = if_down(event.type)
     elif event.key == g.key_dict["move_down"][0]:
         self.y_plus = if_down(event.type)
     elif event.key == g.key_dict["move_left"][0]:
         self.x_minus = if_down(event.type)
     elif event.key == g.key_dict["move_right"][0]:
         self.x_plus = if_down(event.type)
     
     elif event.key == g.key_dict["place_tile"][0]:
         self.placing_tile = if_down(event.type)
     elif event.key == g.key_dict["remove_tile"][0]:
         self.removing_tile = if_down(event.type)
     elif (event.key == g.key_dict["pick_up_tile"][0] and
         event.type == pgl.KEYDOWN):
         self.toggle_grab = True
     elif (event.key == g.key_dict["plant_megatree"][0]):
         x, y = self.get_tile()
         width, height = c.IMAGES["megatree"].multi_tile
         if tiles.area_is_free(x, y + 2, width, height):
             tiles.make_tile("megatree", x, y + 2)
         print "Tried making megatree"
     elif event.key == g.key_dict["build_structure"][0]:
         pass
Exemplo n.º 2
0
    def check_tile(self, z, x, y):
        returned = json.loads(
            self.client.get('/api/v1/tiles/?d={uuid}.{z}.{x}.{y}'.format(
                uuid=self.cooler.uuid, x=x, y=y, z=z)).content)

        r = base64.decodestring(returned[returned.keys()[0]]['dense'])
        q = np.frombuffer(r, dtype=np.float32)

        with h5py.File(self.cooler.datafile.url) as f:

            mat = [f, cch.get_info(self.cooler.datafile.url)]
            t = tiles.make_tile(z, x, y, mat)

            # test the base64 encoding
            self.assertTrue(np.isclose(sum(q), sum(t)))

            # make sure we're returning actual data
            self.assertGreater(sum(q), 0)
Exemplo n.º 3
0
def generate_map():
    ''' Function that loads a PIL image and reads it, pixel by pixel, to decode it into a tile map. 
        
        Gets the map from the c.IMAGES["map"] object.
        Sets a map (two-dimensional list of Tiles), the width and height of the image loaded 
        and the player starting point, x and y, from the file as variables in the g module.
        (5 items) If no starting point is found, return 0, 0 as the starting point. 
    '''
    # Load the image
    map_image = pygame.image.load("res\\" + c.IMAGES["map"].png)
    g.map = []
    # Variable for holding multi_tiles until after the primary generation, because 
    multi_tiles = []
    width, height = map_image.get_size()
    player_start_x = 0
    player_start_y = 0
    
    for x in range(width):
        # Create a new vertical column for every pixel the image is wide.
        g.map.append([])
        for y in range(height):
            # The pixel variable is the pixel we're currently checking.
            pixel = map_image.get_at((x, y))[:3]
            type = pixel_type(pixel, x, y)
            # If the pixel is the player start tile, save the location of that pixel.
            if type == "start_tile":
                player_start_x = x * c.TILE_SIZE
                player_start_y = y * c.TILE_SIZE
                type = c.DEFAULT_TILE
            # Check to see if it's a multi-tile and, if so, store that in a variable to be done last
            g.map[x].append(None)
            if c.IMAGES[type].multi_tile:
                multi_tiles.append([type, x, y])
                tiles.make_tile(c.DEFAULT_TILE, x, y)
            else:
                # Make a new tile and add it to the map
                tiles.make_tile(type, x, y)
            
    # Sets the values to the global values
    g.width = width
    g.height = height
    g.player_start_x = player_start_x
    g.player_start_y = player_start_y
    
    # Create the multi-tiles
    for multi_tile in multi_tiles:
        type, x, y = multi_tile
        width, height = c.IMAGES[type].multi_tile
        if (g.map[x][y] and g.map[x][y].type == c.DEFAULT_TILE and 
                tiles.area_is_free(x, y, width, height)):
            tiles.make_tile(type, x, y)
Exemplo n.º 4
0
 def update(self, time_diff):
     ''' Calls the super update function as well as check for if the package should be turned into a tile.
     '''
     if self.target_coords and not self.had_target_coords:
         self.had_target_coords = True
         self.target_coords[0] = self.target_coords[0] + (c.TILE_SIZE - self.width) / 2
         self.target_coords[1] = self.target_coords[1] + (c.TILE_SIZE - self.height) / 2
         
     if self.target_coords == [int(self.x), int(self.y)]:
         x, y = self.get_tile()
         g.map[x][y] = tiles.make_tile(self.tile, x, y)
         g.update_map = True
         if self.attached_entity != None:
             g.special_entity_list[self.attached_entity].following_entity = None
         del g.special_entity_list[self.attached_entity + "-" + self.image]
         return "deleted"
     super(Package, self).update(time_diff)
     
     
Exemplo n.º 5
0
def make_cooler_tile(cooler_filepath, tile_position):
    '''Create a tile from a cooler file.

    Args:
        cooler_filepath (str): The location of the cooler file that we'll
            that we'll extract the tile data from.
        tile_position (list): The position of the tile ([z,x,y])

    Returns:
        dict: The tile data consisting of a 'dense' member containing
            the data array as well as 'min_value' and 'max_value' which
            contain the minimum and maximum values in the 'dense' array.
    '''

    tile_data = {}

    if cooler_filepath not in mats:
        make_mats(cooler_filepath)

    tileset_file_and_info = mats[cooler_filepath]

    if tile_position[0] > tileset_file_and_info[1]['max_zoom']:
        # we don't have enough zoom levels
        return None
    if tile_position[1] >= 2 ** tile_position[0]:
        # tile is out of bounds
        return None
    if tile_position[2] >= 2 ** tile_position[0]:
        # tile is out of bounds
        return None

    tile = make_tile(
        tile_position[0],
        tile_position[1],
        tile_position[2],
        mats[cooler_filepath]
    )
    tile_data["min_value"] = float(np.min(tile))
    tile_data["max_value"] = float(np.max(tile))
    tile_data['dense'] = base64.b64encode(tile)

    return tile_data
Exemplo n.º 6
0
    def update(self, time_diff):
        ''' Calls the superclass update and updates the state of the aim marker.
            Manages if the player is placing or destroying a block.
        '''
        super(Player, self).update(time_diff)
        # Update screen whenever aim marker changes states
        if self.removing_tile != self.last_removing_tile:
            self.last_removing_tile = self.removing_tile
            g.force_update = True
        
        # Get which tile the player is looking at
        aim_tile = self.get_aim_tile()
        x, y = aim_tile
        # If the aim tile has changed
        if aim_tile != self.last_aim_tile or self.update_aim_tile:
            self.update_aim_tile = False
            # Checks if the aim tile has a remove time (can be destroyed).
            # If so, assign that value to self.remove_timer.
            try:
                if c.IMAGES[g.map[x][y].type].destroy != None:
#                     print "Standard timer set"
                    self.remove_timer = c.IMAGES[g.map[x][y].type].destroy[0]
                elif type(g.map[x][y]) == tiles.MultiTilePointer:
#                     print "Pointer timer set"
                    # Finding out which tile the pointer is pointing to, and if that has a destroy value
                    head_x, head_y = g.map[x][y].target
                    multi_tile_head = g.map[head_x][head_y]
                    if c.IMAGES[multi_tile_head.type].destroy != None:
                        self.remove_timer = c.IMAGES[multi_tile_head.type].destroy[0]
                    else:
                        self.remove_timer = None
                else:
#                     print "Indestructible tile"
                    self.remove_timer = None
            except IndexError:
#                 print "IndexError"
                self.remove_timer = None
            except:
                raise
        self.last_aim_tile = aim_tile
        
        # Placing tile
        if self.placing_tile and not self.removing_tile:
            try:
                if c.IMAGES[g.map[x][y].type].placeable:
                    # If there is a special case for placing tiles, use that. Otherwise, use the default
                    if g.map[x][y].type in c.SPECIAL_PLACE_TILES.keys():
                        tiles.make_tile(c.SPECIAL_PLACE_TILES[g.map[x][y].type], x, y)
                    else:
                        tiles.make_tile(c.DEFAULT_PLACE_TILE, x, y)
            # Ignore IndexErrors because the indices might be outside of the map
            except IndexError:
                pass
            except:
                raise
            
        # Removing tile
        if self.removing_tile and not self.placing_tile:
            # If the timer isn't None (is removable)
            if self.remove_timer != None:
                # If remove_timer has reached 0 which means the countdown is done
                if self.remove_timer < 1:
                    tiles.destroy_tile(x, y)
                    self.update_aim_tile = True
                    self.remove_timer = None
                    return
                
        # Grabbing tile
        if self.toggle_grab:
            # If the grab button is pressed
            self.toggle_grab = False
            if self.following_entity != None:
                x, y = g.special_entity_list[self.following_entity].get_tile()
                if c.IMAGES[g.map[x][y].type].placeable:
                    g.special_entity_list[self.following_entity].target_coords = [x*c.TILE_SIZE,
                                                                                        y*c.TILE_SIZE]
            else:
                if g.map[x][y].type == c.PACKAGE_TILE_NAME:
                    g.map[x][y] = tiles.make_tile(c.DEFAULT_TILE, x, y)
                    g.update_map = True
                    units.Package(x*c.TILE_SIZE, y*c.TILE_SIZE, "player")