Changeset 13:b83f2db218e6


Ignore:
Timestamp:
Apr 3, 2011, 2:33:27 PM (9 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

Draw level background with scaled offset.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • data/levels/level1.json

    r8 r13  
    11{
     2    "background": "dummy.png",
    23    "tileset": "dummy",
    34    "tiles": [
    4         "..........",
    5         "..XX--XX..",
    6         ".........."
     5        "..........................................................................",
     6        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     7        "..........................................................................",
     8        "..XXXX--------------------------------------------------------------XXXX..",
     9        "..........................................................................",
     10        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     11        "..........................................................................",
     12        "..XXXX--------------------------------------------------------------XXXX..",
     13        "..........................................................................",
     14        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     15        "..........................................................................",
     16        "..XXXX--------------------------------------------------------------XXXX..",
     17        "..........................................................................",
     18        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     19        "..........................................................................",
     20        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     21        "..........................................................................",
     22        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     23        "..........................................................................",
     24        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     25        "..........................................................................",
     26        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     27        "..........................................................................",
     28        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     29        "..........................................................................",
     30        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     31        "..........................................................................",
     32        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     33        "..........................................................................",
     34        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     35        "..........................................................................",
     36        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     37        "..........................................................................",
     38        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     39        "..........................................................................",
     40        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     41        "..........................................................................",
     42        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     43        "..........................................................................",
     44        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     45        "..........................................................................",
     46        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     47        "..........................................................................",
     48        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     49        "..........................................................................",
     50        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     51        "..........................................................................",
     52        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     53        "..........................................................................",
     54        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     55        "..........................................................................",
     56        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     57        "..........................................................................",
     58        "..XXXX--------------------------------------------------------------XXXX..",
     59        "..........................................................................",
     60        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     61        "..........................................................................",
     62        "..XXXX--------------------------------------------------------------XXXX..",
     63        "..........................................................................",
     64        "..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..",
     65        ".........................................................................."
    766    ]
    867}
  • skaapsteker/__main__.py

    r11 r13  
    2525
    2626
    27 def draw_level(leveldef, display):
    28     import level
    29     level.Level(leveldef).draw(display)
    30 
    31 
    3227def main():
    3328    """Launch Nine Tales.
     
    5247    pygame.display.set_caption("Nine Tales")
    5348
    54     draw_level('level1.json', display)
    55     pygame.display.flip()
    56 
    5749    engine = Engine()
    5850    try:
  • skaapsteker/level.py

    r10 r13  
    11import json
     2
     3from pygame import Rect, Surface, constants
    24
    35import data
     
    3739    def __init__(self, leveldef):
    3840        self.level_data = json.loads(data.load('levels/' + leveldef).read())
    39         self.tileset = TileSet(self.level_data['tileset'])
     41        self.build_background()
    4042        self.build_tiles()
    4143
    4244
     45    def build_background(self):
     46        self.background = data.load_image('backgrounds/' + self.level_data['background'])
     47        self.background_size = self.background.get_size()
     48        print "background:", self.background_size
     49
     50
    4351    def build_tiles(self):
     52        self.tileset = TileSet(self.level_data['tileset'])
    4453        self.tiles = []
    4554        for row in self.level_data['tiles']:
    4655            self.tiles.append([self.tileset.get_tile(char) for char in row])
     56        self.level_size = (len(self.tiles[0]) * TILE_SIZE[0], len(self.tiles) * TILE_SIZE[1])
     57        print "level:", self.level_size
    4758
    4859
     
    5162
    5263
    53     def draw(self, surface):
     64    def get_backgound_pos(self, pos, display_size):
     65        bg_scalable_x = self.background_size[0] - display_size[0]
     66        bg_scalable_y = self.background_size[1] - display_size[1]
     67        lev_scalable_x = self.level_size[0] - display_size[0]
     68        lev_scalable_y = self.level_size[1] - display_size[1]
     69        scaled_x = (pos[0] * bg_scalable_x) / lev_scalable_x
     70        scaled_y = (pos[1] * bg_scalable_y) / lev_scalable_y
     71        return (scaled_x, scaled_y)
     72
     73
     74    def draw_background(self, surface, pos):
     75        bg_pos = self.get_backgound_pos(pos, surface.get_size())
     76        surface.blit(self.background.subsurface(Rect(bg_pos, surface.get_size())), (0, 0))
     77
     78
     79    def draw_tiles(self, surface, pos):
     80        whole_level = Surface(self.level_size, flags=constants.SRCALPHA)
    5481        for y, row in enumerate(self.tiles):
    5582            for x, tile in enumerate(row):
    56                 surface.blit(tile.image, self.tile_pos(x, y))
     83                whole_level.blit(tile.image, self.tile_pos(x, y))
     84        surface.blit(whole_level.subsurface(Rect(pos, surface.get_size())), (0, 0))
     85
     86
     87    def draw(self, surface, pos):
     88        self.draw_background(surface, pos)
     89        self.draw_tiles(surface, pos)
     90
     91
Note: See TracChangeset for help on using the changeset viewer.