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
7from ..components.player import Player
8
9class Town(tools._State):
10 def __init__(self):
11 super(Town, self).__init__()
12
13
14 def startup(self, current_time, persist):
15 """Called when the State object is created"""
16 self.persist = persist
17 self.current_time = current_time
18 self.get_image = setup.tools.get_image
19 self.town_map_dict = self.create_town_sprite_sheet_dict()
20 self.town_map = self.create_town_map()
21 self.viewport = self.create_viewport()
22 self.level_surface = self.create_level_surface()
23 self.player = Player()
24 self.start_positions = self.set_sprite_positions()
25
26
27 def create_town_sprite_sheet_dict(self):
28 """Create a dictionary of sprite sheet tiles"""
29 dict = {}
30 tileset2 = setup.GFX['tileset2']
31
32 dict['pavement'] = self.get_tile(32, 48, tileset2)
33 dict['house wall'] = self.get_tile(64, 48, tileset2)
34 dict['house roof'] = self.get_tile(0, 144, tileset2)
35 dict['house door'] = self.get_tile(48, 64, tileset2)
36
37 return dict
38
39
40 def get_tile(self, x, y, tileset):
41 """Gets the surface and rect for a tile"""
42 surface = self.get_image(self, x, y, 16, 16, tileset)
43 rect = surface.get_rect()
44
45 dict = {'surface': surface,
46 'rect': rect}
47
48 return dict
49
50
51 def create_town_map(self):
52 """Blits the different layers of the map onto one surface"""
53 map = self.create_background()
54 map = self.create_map_layer1(map)
55 map = self.create_map_layer2(map)
56 map = self.scale_map(map)
57
58 return map
59
60
61 def create_background(self):
62 """Creates the background surface that the rest of
63 the town map will be blitted on"""
64 size = (25*16, 50*16)
65 surface = pg.Surface(size)
66 grass_tile = self.get_image(self, 0, 0, 16, 16, setup.GFX['tileset2'])
67 grass_rect = grass_tile.get_rect()
68
69 for row in range(50):
70 for column in range(25):
71 grass_rect.y = row * 16
72 grass_rect.x = column * 16
73 surface.blit(grass_tile, grass_rect)
74
75 surface_rect = surface.get_rect()
76
77 background_dict = {'surface': surface,
78 'rect': surface_rect}
79
80 return background_dict
81
82
83 def create_map_layer1(self, map):
84 """Creates the town from a tile map and creates a
85 surface on top of the background"""
86 tile_map = open(os.path.join('data', 'states', 'town_map.txt'), 'r')
87
88 for row, line in enumerate(tile_map):
89 for column, letter in enumerate(line):
90 if letter == '1':
91 tile = self.town_map_dict['pavement']
92 self.blit_tile_to_map(tile, row, column, map)
93
94 elif letter == '2':
95 tile = self.town_map_dict['house wall']
96 self.blit_tile_to_map(tile, row, column, map)
97
98 elif letter == '3':
99 tile = self.town_map_dict['house roof']
100 self.blit_tile_to_map(tile, row, column, map)
101
102 tile_map.close()
103
104 return map
105
106
107 def create_map_layer2(self, map):
108 """Creates doors and other items on top of the rest of the map"""
109 tile_map = open(os.path.join('data', 'states', 'town_layer2.txt'), 'r')
110
111 for row, line in enumerate(tile_map):
112 for column, letter in enumerate(line):
113 if letter == 'D':
114 tile = self.town_map_dict['house door']
115 self.blit_tile_to_map(tile, row, column, map)
116
117 tile_map.close()
118
119 return map
120
121
122 def scale_map(self, map):
123 """Double resolution of map to 32x32"""
124 map['surface'] = pg.transform.scale2x(map['surface'])
125 map['rect'] = map['surface'].get_rect()
126
127 return map
128
129
130 def blit_tile_to_map(self, tile, row, column, map):
131 """Places tile to map"""
132 tile['rect'].x = column * 16
133 tile['rect'].y = row * 16
134
135 map['surface'].blit(tile['surface'], tile['rect'])
136
137
138 def create_viewport(self):
139 """Create the viewport to view the level through"""
140 return setup.SCREEN.get_rect(bottom=self.town_map['rect'].bottom)
141
142
143 def create_level_surface(self):
144 """Creates the surface all images are blitted to"""
145 width = self.town_map['rect'].width
146 height = self.town_map['rect'].height
147
148 return pg.Surface((width, height)).convert()
149
150
151 def set_sprite_positions(self):
152 """Set the start positions for all the sprites in the level"""
153 tile_map = open(os.path.join('data', 'states', 'sprite_start_pos.txt'), 'r')
154 dict = {}
155
156 for row, line in enumerate(tile_map):
157 for column, letter in enumerate(line):
158 if letter == 'P':
159 dict['player'] = pg.Rect(column*32, row*32, 32, 32)
160
161 self.player.rect = dict['player']
162
163 return dict
164
165
166 def update(self, surface, keys, current_time):
167 """Updates state"""
168 self.keys = keys
169 self.current_time = current_time
170 self.check_for_player_input()
171 self.player.update()
172
173 self.draw_level(surface)
174
175
176 def draw_level(self, surface):
177 """Blits all images to screen"""
178 self.level_surface.blit(self.town_map['surface'], self.viewport, self.viewport)
179 self.level_surface.blit(self.player.image, self.player.rect)
180
181 surface.blit(self.level_surface, (0,0), self.viewport)
182
183
184 def check_for_player_input(self):
185 """Checks for player input"""
186 if self.keys[pg.K_UP]:
187 self.player.begin_moving('up')
188 elif self.keys[pg.K_DOWN]:
189 self.player.begin_moving('down')
190 elif self.keys[pg.K_LEFT]:
191 self.player.begin_moving('left')
192 elif self.keys[pg.K_RIGHT]:
193 self.player.begin_moving('right')
194
195
196
197
198
199
200
201
202