all repos — Legends-RPG @ d86d464a61c9589bc003720a2afaf1c008717036

A fantasy mini-RPG built with Python and Pygame.

data/states/town.py (view raw)

  1__author__ = 'justinarmstrong'
  2
  3import os
  4import pygame as pg
  5from .. import setup, tools
  6from .. import constants as c
  7
  8class Town(tools._State):
  9    def __init__(self):
 10        super(Town, self).__init__()
 11
 12
 13    def startup(self, current_time, persist):
 14        """Called when the State object is created"""
 15        self.persist = persist
 16        self.current_time = current_time
 17        self.get_image = setup.tools.get_image
 18        self.sprite_sheet_dict = self.create_sprite_sheet_dict()
 19        self.town_map = self.create_town_map()
 20        self.viewport = self.create_viewport()
 21
 22
 23    def create_sprite_sheet_dict(self):
 24        """Create a dictionary of sprite sheet tiles"""
 25        dict = {}
 26        tileset2 = setup.GFX['tileset2']
 27
 28        dict['pavement'] = self.get_tile(32, 48, tileset2)
 29        dict['house wall'] = self.get_tile(64, 48, tileset2)
 30        dict['house roof'] = self.get_tile(0, 144, tileset2)
 31        dict['house door'] = self.get_tile(48, 64, tileset2)
 32
 33        return dict
 34
 35
 36    def get_tile(self, x, y, tileset):
 37        """Gets the surface and rect for a tile"""
 38        surface = self.get_image(self, x, y, 16, 16, tileset)
 39        rect = surface.get_rect()
 40
 41        dict = {'surface': surface,
 42                'rect': rect}
 43
 44        return dict
 45
 46
 47    def create_town_map(self):
 48        """Blits the different layers of the map onto one surface"""
 49        map = self.create_background()
 50        map = self.create_map_layer1(map)
 51        map = self.create_map_layer2(map)
 52        map = self.scale_map(map)
 53
 54        return map
 55
 56
 57    def create_background(self):
 58        """Creates the background surface that the rest of
 59        the town map will be blitted on"""
 60        size = (25*16, 50*16)
 61        surface = pg.Surface(size)
 62        grass_tile = self.get_image(self, 0, 0, 16, 16, setup.GFX['tileset2'])
 63        grass_rect = grass_tile.get_rect()
 64
 65        for row in range(50):
 66            for column in range(25):
 67                grass_rect.y = row * 16
 68                grass_rect.x = column * 16
 69                surface.blit(grass_tile, grass_rect)
 70
 71        surface_rect = surface.get_rect()
 72
 73        background_dict = {'surface': surface,
 74                           'rect': surface_rect}
 75
 76        return background_dict
 77
 78
 79    def create_map_layer1(self, map):
 80        """Creates the town from a tile map and creates a
 81        surface on top of the background"""
 82        tile_map = open(os.path.join('data', 'states', 'town_map.txt'), 'r')
 83
 84        for row, line in enumerate(tile_map):
 85            for column, letter in enumerate(line):
 86                if letter == '1':
 87                    tile = self.sprite_sheet_dict['pavement']
 88                    self.blit_tile_to_map(tile, row, column, map)
 89
 90                elif letter == '2':
 91                    tile = self.sprite_sheet_dict['house wall']
 92                    self.blit_tile_to_map(tile, row, column, map)
 93
 94                elif letter == '3':
 95                    tile = self.sprite_sheet_dict['house roof']
 96                    self.blit_tile_to_map(tile, row, column, map)
 97
 98        tile_map.close()
 99
100        return map
101
102
103    def create_map_layer2(self, map):
104        """Creates doors and other items on top of the rest of the map"""
105        tile_map = open(os.path.join('data', 'states', 'town_layer2.txt'), 'r')
106
107        for row, line in enumerate(tile_map):
108            for column, letter in enumerate(line):
109                if letter == 'D':
110                    print 'hi'
111                    tile = self.sprite_sheet_dict['house door']
112                    self.blit_tile_to_map(tile, row, column, map)
113
114        tile_map.close()
115
116        return map
117
118
119    def scale_map(self, map):
120        """Doubles the size of map to fit a (800x600) aspect ratio"""
121        map['surface'] = pg.transform.scale2x(map['surface'])
122        map['rect'] = map['surface'].get_rect()
123
124        return map
125
126
127    def blit_tile_to_map(self, tile, row, column, map):
128        """Places tile to map"""
129        tile['rect'].x = column * 16
130        tile['rect'].y = row * 16
131
132        map['surface'].blit(tile['surface'], tile['rect'])
133
134
135    def create_viewport(self):
136        """Create the viewport to view the level through"""
137        return setup.SCREEN.get_rect(bottom=self.town_map['rect'].bottom)
138
139
140    def update(self, surface, keys, current_time):
141        """Updates state"""
142        self.keys = keys
143        self.current_time = current_time
144        surface.blit(self.town_map['surface'], (0,0), self.viewport)
145
146
147
148
149
150
151
152
153