コード例 #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
コード例 #2
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)