Changeset 52:a18bf35519f3


Ignore:
Timestamp:
Apr 3, 2011, 7:25:43 PM (9 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
rebase_source:
d24d10ca8e15a7b17d962c02435849fe077b7656
Message:

Multiple layered backgrounds.

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • data/levels/level1.json

    r50 r52  
    11{
    2     "background": "dummy.png",
     2    "backgrounds": [
     3        "dummy.png",
     4        "dummy-fg.png"
     5    ],
    36    "tileset": "dummy",
    47    "tiles": [
  • data/levels/level2.json

    r34 r52  
    11{
    2     "background": "dummy.png",
     2    "backgrounds": [
     3        "dummy.png",
     4        "dummy-fg.png"
     5    ],
    36    "tileset": "dummy",
    47    "tiles": [
  • data/levels/level3.json

    r36 r52  
    11{
    2     "background": "dummy.png",
     2    "backgrounds": [
     3        "dummy.png",
     4        "dummy-fg.png"
     5    ],
    36    "tileset": "dummy",
    47    "tiles": [
  • data/levels/level4.json

    r41 r52  
    11{
    2     "background": "dummy.png",
     2    "backgrounds": [
     3        "dummy.png",
     4        "dummy-fg.png"
     5    ],
    36    "tileset": "dummy",
    47    "tiles": [
  • skaapsteker/level.py

    r50 r52  
    3535        self.level_data = json.loads(data.load('levels/' + leveldef + '.json').read())
    3636        self.sprites = Group()
    37         self.build_background()
     37        self.build_backgrounds()
    3838        self.build_tiles()
    3939        self.setup_enemies()
    4040
    4141
    42     def build_background(self):
    43         self.background = data.load_image('backgrounds/' + self.level_data['background'])
    44         self.background_size = self.background.get_size()
    45         print "background:", self.background_size
     42    def build_backgrounds(self):
     43        self.backgrounds = []
     44        for background in self.level_data['backgrounds']:
     45            self.backgrounds.append(data.load_image('backgrounds/' + background))
    4646
    4747
     
    8080
    8181
    82     def get_background_pos(self, pos, display_size):
    83         bg_scalable_x = self.background_size[0] - display_size[0]
    84         bg_scalable_y = self.background_size[1] - display_size[1]
    85         lev_scalable_x = self.pixel_size[0] - display_size[0]
    86         lev_scalable_y = self.pixel_size[1] - display_size[1]
    87         scaled_x = (pos[0] * bg_scalable_x) / lev_scalable_x
    88         scaled_y = (pos[1] * bg_scalable_y) / lev_scalable_y
    89         return (scaled_x, scaled_y)
     82    def get_background_pos(self, background, viewport):
     83        vp_x, vp_y = viewport.size
     84        bg_scalable = background.get_rect().inflate(-vp_x, -vp_y).size
     85        lev_scalable = Rect((0, 0), self.pixel_size).inflate(-vp_x, -vp_y).size
     86        return ((viewport.left * bg_scalable[0]) / lev_scalable[0],
     87                (viewport.top * bg_scalable[1]) / lev_scalable[1])
    9088
    9189
    9290    def draw_background(self, surface):
    9391        clip_rect = surface.get_clip()
    94         bg_pos = self.get_background_pos(clip_rect.topleft, clip_rect.size)
    95         surface.blit(self.background, clip_rect.topleft, Rect(bg_pos, clip_rect.size))
     92        for background in self.backgrounds:
     93            bg_pos = self.get_background_pos(background, clip_rect)
     94            surface.blit(background, clip_rect.topleft, Rect(bg_pos, clip_rect.size))
    9695
    9796
Note: See TracChangeset for help on using the changeset viewer.