Added Brother house state, refactored tilemap.py
Justin Armstrong justinmeister@gmail.com
Mon, 07 Apr 2014 16:39:50 -0700
18 files changed,
255 insertions(+),
120 deletions(-)
jump to
M
data/constants.py
→
data/constants.py
@@ -14,6 +14,7 @@ WEAPON_SHOP = 'weapon shop'
MAGIC_SHOP = 'magic shop' HOUSE = 'house' OVERWORLD = 'overworld' +BROTHER_HOUSE = 'brother_house' ##Colors
M
data/main.py
→
data/main.py
@@ -4,6 +4,7 @@ from data.states.castle import castle
from data.states import shop from data.states.house import house from data.states.overworld import overworld +from data.states.brother_house import brother_house from . import setup, tools from . import constants as c@@ -19,6 +20,7 @@ MAGIC_SHOP = 'magic shop'
POTION_SHOP = 'potion shop' PLAYER_MENU = 'player menu' OVERWORLD = 'overworld' +BROTHER_HOUSE = 'brother_house' def main():@@ -33,7 +35,8 @@ ARMOR_SHOP: shop.ArmorShop(),
WEAPON_SHOP: shop.WeaponShop(), MAGIC_SHOP: shop.MagicShop(), POTION_SHOP: shop.PotionShop(), - OVERWORLD: overworld.Overworld() + OVERWORLD: overworld.Overworld(), + BROTHER_HOUSE: brother_house.House() } run_it.setup_states(state_dict, c.MAIN_MENU)
A
data/states/brother_house/blockers.txt
@@ -0,0 +1,20 @@
+0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +00000000BBBBBBBBB00000000 +000000B1111111B11B0000000 +000000B1111111B11B0000000 +000000B1111111B11B0000000 +000000B1111111B11B0000000 +000000B1111111BBBB0000000 +000000B1111111111B0000000 +000000B1111111111B0000000 +000000B1111111111B0000000 +000000B1111111111B0000000 +0000000BBBB11BBBB00000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000
A
data/states/brother_house/brother_house.py
@@ -0,0 +1,27 @@
+"""This is the house on the lower left hand +corner of the town. Most of its functionality is inherited +from the level_state.py module. Most of the level data is contained +in the tilemap .txt files in this state's directory. Essentially the +only purpose of house.py is to assign dialogue to each sprite. +""" + + +from .. import level_state +from ... import constants as c + +class House(level_state.LevelState): + def __init__(self): + super(House, self).__init__() + self.name = c.BROTHER_HOUSE + self.map_width = 25 + self.map_height = 19 + + def set_sprite_dialogue(self): + """Sets unique dialogue for each sprite""" + for sprite in self.sprites: + if sprite.location == [14, 6]: + sprite.dialogue = ["I am very sick. cough... cough...", + "Only an ELIXIR can help me.", + "Please go to my brother and obtain one for me.", + "He lives in a house on the NorthEast shores.", + "I will be forever in your debt."]
A
data/states/brother_house/layer1.txt
@@ -0,0 +1,19 @@
+0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000BBBBBBBBBB00000000 +0000000BBBBBBBBBB00000000 +0000000BBBBBBBBBB00000000 +0000000BBBBBBBBBB00000000 +0000000BBBBBBBBBB00000000 +0000000BBBBBBBBBB00000000 +0000000BBBBBBBBBB00000000 +0000000BBBBBBBBBB00000000 +0000000BBBBQEBBBB00000000 +0000000KKKKKKKKKK00000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000
A
data/states/brother_house/layer1extra.txt
@@ -0,0 +1,19 @@
+0000000000000000000000000 +0000000G00000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +00000001111111E1100000000 +00000001J1111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000
A
data/states/brother_house/layer2.txt
@@ -0,0 +1,19 @@
+0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +00000000000NN000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000
A
data/states/brother_house/layer3.txt
@@ -0,0 +1,19 @@
+0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000111111111100000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000
A
data/states/brother_house/portals.txt
@@ -0,0 +1,20 @@
+0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +00000000000JJ000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000
A
data/states/brother_house/sprite_start_pos.txt
@@ -0,0 +1,19 @@
+0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +000000000H000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000
M
data/states/house/house.py
→
data/states/house/house.py
@@ -20,5 +20,8 @@ def set_sprite_dialogue(self):
"""Sets unique dialogue for each sprite""" for sprite in self.sprites: if sprite.location == [14, 6]: - sprite.dialogue = ["There are evil forces surrounding our town.", - "Do not leave if you value your life."] + sprite.dialogue = ["I am very sick. cough... cough...", + "Only an ELIXIR can help me.", + "Please go to my brother and obtain one for me.", + "He lives in a house on the NorthEast shores.", + "I will be forever in your debt."]
M
data/states/overworld/blockers.txt
→
data/states/overworld/blockers.txt
@@ -1,6 +1,6 @@
000000000000000000000000000000 0BBBBBBBBB00000000000000000000 -B000000000B0000000000000000000 +BB00000000B0000000000000000000 B00BBBB000B0000000000000000000 B00B00B000B0000000000000000000 B00B00B000BBBBBBBBBBBBBBBBBB00
M
data/states/overworld/layer1.txt
→
data/states/overworld/layer1.txt
@@ -1,6 +1,6 @@
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM -M7b9ab9ab4MMMMMMMMMMMMMMMMMMMM +MMb9ab9ab4MMMMMMMMMMMMMMMMMMMM M60000000ZMMMMMMMMMMMMMMMMMMMM M700000004MMMMMMMMMMMMMMMMMMMM M60000000ZMMMMMMMMMMMMMMMMMMMM
M
data/states/overworld/portals.txt
→
data/states/overworld/portals.txt
@@ -6,7 +6,7 @@ 000000000000000000000000000000
000000000000000000000000000000 000000000000000000000000000000 000000000000000000000000000000 -000000000000000000000000000000 +0000000000000000000000000K0000 000000000000000000000000000000 000000000000000000000000000000 000000000000000000000000000000
M
data/tilemap.py
→
data/tilemap.py
@@ -127,6 +127,63 @@ }
return tile_dict +def create_layer2_tile_code_dict(): + """Creates a dictionary of tile codes for layer 2""" + tile_dict = {'D': 'house door', + 'F': 'fence', + '$': 'flower1', + '*': 'flower2', + 'T': 'castle tower', + 'W': 'left vert castle wall', + 'M': 'main castle roof', + 'L': 'left castle roof piece', + 'R': 'right castle roof piece', + '#': 'tree', + 'O': 'castle door', + 'Q': 'castle window', + 'A': 'banner1', + 'B': 'banner2', + 'C': 'banner3', + 'E': 'bed', + 'G': 'shelves', + 'H': 'chair', + 'I': 'table', + 'J': 'fancy carpet', + 'K': 'column', + 'N': 'black tile', + 'P': 'houseoverworld', + '4': 'oldmanhouse', + '5': 'evilcastle' + } + + return tile_dict + +def create_layer3_tile_code_dict(): + """Creates a dictionary of tile codes for layer 3""" + tile_dict = {'W': 'sword', + 'A': 'shield', + 'P': 'potion', + 'M': 'gem', + 'I': 'inn sign', + 'B': 'chair'} + + return tile_dict + +def make_level_portals_code_dict(): + """Make the dictionary for codes for level portals""" + portal_dict = {'A': c.CASTLE, + 'B': c.TOWN, + 'C': c.INN, + 'D': c.MAGIC_SHOP, + 'E': c.POTION_SHOP, + 'F': c.WEAPON_SHOP, + 'G': c.ARMOR_SHOP, + 'H': c.HOUSE, + 'J': c.OVERWORLD, + 'K': c.BROTHER_HOUSE + } + + return portal_dict def get_tile(x, y, tileset, width=16, height=16, scale=1): """Gets the surface and rect for a tile"""@@ -202,85 +259,16 @@ def create_map_layer2(state, width, height, file_name='layer2.txt'):
"""Creates doors and other items on top of the rest of the map""" map = make_background(None, width, height) map.set_colorkey(c.BLACK_BLUE) + layer2_tile_dict = create_layer2_tile_code_dict() tile_map = open(os.path.join('data', 'states', state, file_name), 'r') for row, line in enumerate(tile_map): for column, letter in enumerate(line): - if letter == 'D': - tile = tile_dict['house door'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'F': - tile = tile_dict['fence'] - blit_tile_to_map(tile, row, column, map) - elif letter == '$': - tile = tile_dict['flower1'] - blit_tile_to_map(tile, row, column, map) - elif letter == '*': - tile = tile_dict['flower2'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'T': - tile = tile_dict['castle tower'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'W': - tile = tile_dict['left vert castle wall'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'M': - tile = tile_dict['main castle roof'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'L': - tile = tile_dict['left castle roof piece'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'R': - tile = tile_dict['right castle roof piece'] - blit_tile_to_map(tile, row, column, map) - elif letter == '#': - tile = tile_dict['tree'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'O': - tile = tile_dict['castle door'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'Q': - tile = tile_dict['castle window'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'A': - tile = tile_dict['banner1'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'B': - tile = tile_dict['banner2'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'C': - tile = tile_dict['banner3'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'E': - tile = tile_dict['bed'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'G': - tile = tile_dict['shelves'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'H': - tile = tile_dict['chair'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'I': - tile = tile_dict['table'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'J': - tile = tile_dict['fancy carpet'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'K': - tile = tile_dict['column'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'N': - tile = tile_dict['black tile'] - blit_tile_to_map(tile, row, column, map) - elif letter == 'P': - tile = tile_dict['houseoverworld'] - blit_tile_to_map(tile, row, column, map) - elif letter == '4': - tile = tile_dict['oldmanhouse'] - blit_tile_to_map(tile, row, column, map) - elif letter == '5': - tile = tile_dict['evilcastle'] + if letter not in layer2_tile_dict: + pass + else: + tile = tile_dict[layer2_tile_dict[letter]] blit_tile_to_map(tile, row, column, map) tile_map.close()@@ -302,33 +290,19 @@
def create_map_layer3(map, state): """Layers for images that are already 32x32""" tile_map = open(os.path.join('data', 'states', state, 'layer3.txt'), 'r') + layer3_tile_dict = create_layer3_tile_code_dict() for row, line in enumerate(tile_map): for column, letter in enumerate(line): - if letter == 'W': - tile = tile_dict['sword'] + if letter not in layer3_tile_dict: + pass + else: + tile = tile_dict[layer3_tile_dict[letter]] blit_tile_to_map(tile, row, column, map, 32) - elif letter == 'A': - tile = tile_dict['shield'] - blit_tile_to_map(tile, row, column, map, 32) - elif letter == 'P': - tile = tile_dict['potion'] - blit_tile_to_map(tile, row, column, map, 32) - elif letter == 'M': - tile = tile_dict['gem'] - blit_tile_to_map(tile, row, column, map, 32) - elif letter == 'I': - tile = tile_dict['inn sign'] - blit_tile_to_map(tile, row, column, map, 32) - elif letter == 'B': - tile = tile_dict['chair'] - blit_tile_to_map(tile, row, column, map, 32) - tile_map.close() return map - def blit_tile_to_map(tile, row, column, map, side_length=16):@@ -415,31 +389,18 @@
def make_level_portals(state): """Create portals to different levels on doors""" tile_map = open(os.path.join('data', 'states', state, 'portals.txt'), 'r') + portal_code_dict = make_level_portals_code_dict() portal_group = pg.sprite.Group() for row, line in enumerate(tile_map): for column, letter in enumerate(line): - if letter == 'A': - portal_group.add(portal.Portal(column, row, c.CASTLE)) - elif letter == 'B': - portal_group.add(portal.Portal(column, row, c.TOWN)) - elif letter == 'C': - portal_group.add(portal.Portal(column, row, c.INN)) - elif letter == 'D': - portal_group.add(portal.Portal(column, row, c.MAGIC_SHOP)) - elif letter == 'E': - portal_group.add(portal.Portal(column, row, c.POTION_SHOP)) - elif letter == 'F': - portal_group.add(portal.Portal(column, row, c.WEAPON_SHOP)) - elif letter == 'G': - portal_group.add(portal.Portal(column, row, c.ARMOR_SHOP)) - elif letter == 'H': - portal_group.add(portal.Portal(column, row, c.HOUSE)) - elif letter == 'J': - portal_group.add(portal.Portal(column, row, c.OVERWORLD)) + if letter not in portal_code_dict: + pass + else: + portal_group.add(portal.Portal( + column, row, portal_code_dict[letter])) return portal_group - get_image = tools.get_image
M
data/tools.py
→
data/tools.py
@@ -50,11 +50,14 @@ for event in self.events:
if event.type == pg.QUIT: self.done = True elif event.type == pg.KEYDOWN: + if event.key == pg.K_ESCAPE: + self.done = True self.keys = pg.key.get_pressed() self.toggle_show_fps(event.key) + self.state.get_event(event) elif event.type == pg.KEYUP: self.keys = pg.key.get_pressed() - self.state.get_event(event) + self.state.get_event(event) def toggle_show_fps(self, key): if key == pg.K_F5:@@ -174,6 +177,7 @@ 'last direction': 'down',
'town start pos': [12, 49], 'castle start pos': [12, 26], 'house start pos': [12, 13], + 'brother_house start pos': [12, 13], 'overworld start pos': [17, 30], 'king item': {'gold': 500}, 'player inventory': player_items,