예제 #1
0
    def setup_player_animations(self):
        # Get player sprite sheet
        player_sheet = resourceLoader.get_image("characters\\stormTrooper.png")

        # Idle right
        idle_r = Animation()
        idle_r.frames.append(player_sheet[0])
        idle_r.speed = 0

        # Idle left
        idle_l = Animation()
        idle_l.frames.append(player_sheet[9])
        idle_l.speed = 0

        # Walk right
        walk_r = Animation()
        walk_r.frames = player_sheet[1:5]
        walk_r.speed = 0.15

        # Walk left
        walk_l = Animation()
        walk_l.frames = player_sheet[5:9]
        walk_l.speed = 0.15

        # Add animations
        self.add_animation("idle_r", idle_r)
        self.add_animation("idle_l", idle_l)
        self.add_animation("walk_r", walk_r)
        self.add_animation("walk_l", walk_l)
예제 #2
0
 def load_image(self, name):
     img = resourceLoader.get_image(name)
     r = img.get_rect()
     # w = r[2] * C.PIXEL_SCALE
     # h = r[3] * C.PIXEL_SCALE
     #
     # self.rect = (self.rect[0], self.rect[1], w, h)
     #
     # img = pygame.transform.scale(img, (w, h))
     self.rect = r
     self.image = img
예제 #3
0
    def load_level(level_path):
        path = LEVELS_DIR + "\\" + level_path

        # Open the json file
        json_level = None
        with open(path) as json_file:
            json_level = json.load(json_file)

        # Read level info from json-file
        level_width = int(json_level["width"])
        level_height = int(json_level["height"])
        tile_size = 8*C.PIXEL_SCALE
        y_offset = C.SCREEN_HEIGHT-level_height*tile_size
        tileset = json_level["tilesets"][0]["name"]
        tile_props = json_level["tilesets"][0]["tileproperties"]

        # Decode level data
        data = json_level["layers"][0]["data"]
        data = data.encode('utf-8')
        data = base64.b64decode(bytes(data))
        data = zlib.decompress(data)
        data = bytes(data)
        tiles = array.array('I', data)

        # Assign data to static level props
        Level.level_width = level_width
        Level.level_height = level_height
        Level.tileset = resourceLoader.get_image(tileset)
        Level.y_offset = y_offset

        # Create tile sprites
        from sprite import StaticSprite

        for y in range(level_height):
            for x in range(level_width):

                t_id = int(tiles[y*level_width+x])
                if str(t_id) in tile_props:
                    tp = tile_props[str(t_id)]
                else:
                    tp = {}
                tile = Tile(t_id, tp, y*level_width+x)
                Level._tiles.append(tile)

                if t_id-1 == -1:
                    continue
                rect = (x*tile_size, y*tile_size+y_offset, tile_size, tile_size)
                img = Level.tileset[t_id-1]
                StaticSprite(rect, img)
        for t in Level._tiles:
            print(str(t.get_pos()) + ": ID: " + str(t.get_id()) + " PROPS: " + str(t.properties))
예제 #4
0
# Initialize constants
from constants import Constants as C
C.init_constants()

# Setup the screen
SCREEN = pygame.display.set_mode((C.SCREEN_WIDTH,C.SCREEN_HEIGHT))

# Load resources
resourceLoader.load_resources()

# Load level
Level.load_level("tatooine_1.json")

# Create background
BackgroundSprite(resourceLoader.get_image("backgrounds\\tatooine.png"))

# Create player
player = Player()
player.set_position(300, 0)

# Create camera
camera = Camera((0, 0), (Level.level_width*C.TILE_SIZE - C.SCREEN_WIDTH, Level.level_height*C.TILE_SIZE))

# Test Animation-----------------------------------------------
# anim = Animation()
# anim.frames = resourceLoader.get_image("characters\\stormTrooper.png")[1:5]
# anim.speed = 0.15
#
# animSp = AnimatedSprite()
# animSp.layer = C.HUD_LAYER