Changeset 35:38d2f7c43d86


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

Sprite-based tiles. \o/

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • scripts/level-editor

    r33 r35  
    4444
    4545        position[0] = max(position[0], 0)
    46         position[0] = min(position[0], level.level_size[0] - SCREEN[0])
     46        position[0] = min(position[0], level.pixel_size[0] - SCREEN[0])
    4747        position[1] = max(position[1], 0)
    48         position[1] = min(position[1], level.level_size[1] - SCREEN[1])
     48        position[1] = min(position[1], level.pixel_size[1] - SCREEN[1])
    4949
    5050        level.draw(surface, position)
  • skaapsteker/level.py

    r28 r35  
    55import data
    66from sprites import enemies
    7 from sprites.base import find_sprite
    8 
    9 
    10 TILE_SIZE = (64, 64)
    11 
    12 
    13 class Tile(object):
    14     "This is temporary until we have a proper tile class."
    15 
    16     def __init__(self, tilefile):
    17         self.image = data.load_image(tilefile)
     7from sprites.base import TILE_SIZE, find_sprite, Geography
    188
    199
     
    2414
    2515
    26     def get_tile(self, tilechar):
    27         tilefile = self.dirname + '/' + self.char_to_name(tilechar)
    28         return Tile(tilefile)
     16    def get_tile(self, tilechar, pos):
     17        return Geography(pos, self.get_image(tilechar))
    2918
    3019
    31     def char_to_name(self, tilechar):
    32         return {
     20    def get_image(self, tilechar):
     21        image_name = {
    3322            '.': 'blank.png',
    3423            '-': 'floor.png',
    3524            'X': 'solid.png',
    3625            }[tilechar]
     26        image_file = self.dirname + '/' + image_name
     27        return data.load_image(image_file)
    3728
    3829
     
    5647        self.tileset = TileSet(self.level_data['tileset'])
    5748        self.tiles = []
    58         for row in self.level_data['tiles']:
    59             self.tiles.append([self.tileset.get_tile(char) for char in row])
    60         self.level_size = (len(self.tiles[0]) * TILE_SIZE[0], len(self.tiles) * TILE_SIZE[1])
    61         print "level:", self.level_size
     49        tile_data = self.level_data['tiles']
     50        self.tile_size = (len(tile_data[0]), len(tile_data))
     51        self.pixel_size = (self.tile_size[0] * TILE_SIZE[0], self.tile_size[1] * TILE_SIZE[1])
     52        for y, row in enumerate(tile_data):
     53            for x, char in enumerate(row):
     54                tile = self.tileset.get_tile(char, (x, y))
     55                if tile:
     56                    self.tiles.append(tile)
     57        print "level:", self.tile_size, self.pixel_size
    6258
    6359
     
    7369        print self.sprites
    7470
    75     def tile_pos(self, x, y):
    76         return (x * TILE_SIZE[0], y * TILE_SIZE[1])
    77 
    7871
    7972    def get_background_pos(self, pos, display_size):
    8073        bg_scalable_x = self.background_size[0] - display_size[0]
    8174        bg_scalable_y = self.background_size[1] - display_size[1]
    82         lev_scalable_x = self.level_size[0] - display_size[0]
    83         lev_scalable_y = self.level_size[1] - display_size[1]
     75        lev_scalable_x = self.pixel_size[0] - display_size[0]
     76        lev_scalable_y = self.pixel_size[1] - display_size[1]
    8477        scaled_x = (pos[0] * bg_scalable_x) / lev_scalable_x
    8578        scaled_y = (pos[1] * bg_scalable_y) / lev_scalable_y
     
    9386
    9487    def draw_tiles(self, surface, pos):
    95         whole_level = Surface(self.level_size, flags=constants.SRCALPHA)
    96         for y, row in enumerate(self.tiles):
    97             for x, tile in enumerate(row):
    98                 whole_level.blit(tile.image, self.tile_pos(x, y))
    99         surface.blit(whole_level.subsurface(Rect(pos, surface.get_size())), (0, 0))
     88        colliderect = surface.get_rect().move(pos)
     89        for tile in self.tiles:
     90            if colliderect.colliderect(tile.rect):
     91                surface.blit(tile.image, (tile.rect.left - pos[0], tile.rect.top - pos[1]))
    10092
    10193
  • skaapsteker/sprites/base.py

    r28 r35  
    11"""Basic sprite classes."""
    22
     3from pygame import Rect
     4
    35from skaapsteker.physics import Sprite
     6
     7
     8TILE_SIZE = (64, 64)
    49
    510
     
    3742
    3843
     44    def __init__(self, pos, image):
     45        Sprite.__init__(self)
     46        self.tile_pos = pos
     47        self.image = image
     48        self.rect = Rect((pos[0] * TILE_SIZE[0], pos[1] * TILE_SIZE[1]), TILE_SIZE)
     49
     50
     51
    3952def find_sprite(descr):
    4053    """Create a sprite object from a dictionary describing it."""
     
    4659    cls = getattr(mod, cls_name)
    4760    return cls(**descr)
     61
Note: See TracChangeset for help on using the changeset viewer.