all repos — Legends-RPG @ 4818df1862ee7d8c27d7775f4b425616bd61f2ee

A fantasy mini-RPG built with Python and Pygame.

Shops can list multiple items to buy or sell. Shops with single items are a bit buggy.
Justin Armstrong justinmeister@gmail.com
Sun, 30 Mar 2014 00:41:02 -0700
commit

4818df1862ee7d8c27d7775f4b425616bd61f2ee

parent

f22745abf2d135ac49c081071eb6a2bcc37b401b

2 files changed, 70 insertions(+), 34 deletions(-)

jump to
M data/shopgui.pydata/shopgui.py

@@ -14,6 +14,7 @@ """Class that controls the GUI of the shop state"""

def __init__(self, level): self.level = level self.sellable_items = level.sell_items + print level.sell_items self.player_inventory = level.game_data['player inventory'] self.name = level.name self.state = 'dialogue'

@@ -22,8 +23,9 @@ self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 22)

self.index = 0 self.timer = 0.0 self.allow_input = False - self.item = level.item + self.items = level.items self.item_to_be_sold = None + self.item_to_be_purchased = None self.dialogue = level.dialogue self.accept_dialogue = level.accept_dialogue self.accept_sale_dialogue = level.accept_sale_dialogue

@@ -41,7 +43,7 @@ self.selection_arrow.rect.topleft = self.arrow_pos1

self.dialogue_box = self.make_dialogue_box(self.dialogue, self.index) self.gold_box = self.make_gold_box() if self.name in self.no_selling: - choices = self.item['dialogue'] + choices = self.items[0]['dialogue'] else: choices = ['Buy', 'Sell', 'Leave'] self.selection_box = self.make_selection_box(choices)

@@ -171,16 +173,16 @@

def make_selection(self, keys, current_time): """Control the selection""" - choices = self.item['dialogue'] + choices = [self.items[0]['dialogue'], self.items[1]['dialogue'], 'Cancel'] self.dialogue_box = self.make_dialogue_box(self.dialogue, self.index) self.selection_box = self.make_selection_box(choices) self.gold_box = self.make_gold_box() - self.selection_arrow.rect.topleft = self.two_arrow_pos_list[self.arrow_index] + self.selection_arrow.rect.topleft = self.arrow_pos_list[self.arrow_index] if keys[pg.K_DOWN] and self.allow_input: - if self.arrow_index < (len(self.two_arrow_pos_list) - 1): + if self.arrow_index < (len(choices) - 1): self.arrow_index += 1 self.allow_input = False elif keys[pg.K_UP] and self.allow_input:

@@ -190,8 +192,13 @@ self.allow_input = False

elif keys[pg.K_SPACE] and self.allow_input: if self.arrow_index == 0: self.state = 'confirmpurchase' + self.item_to_be_purchased = self.items[0] elif self.arrow_index == 1: + self.state = 'confirmpurchase' + self.item_to_be_purchased = self.items[1] + + else: if self.level.name in self.no_selling: self.level.done = True self.level.game_data['last direction'] = 'down'

@@ -276,26 +283,31 @@

def buy_item(self): """Attempt to allow player to purchase item""" - self.player_inventory['gold'] -= self.item['price'] + self.player_inventory['gold'] -= self.item_to_be_purchased['price'] if self.player_inventory['gold'] < 0: - self.player_inventory['gold'] += self.item['price'] + self.player_inventory['gold'] += self.item_to_be_purchased['price'] self.state = 'reject' else: - if (self.item['type'] == 'Fire Spell' and + if (self.item_to_be_purchased['type'] == 'Fire Spell' and 'Fire Spell' in self.player_inventory): self.state = 'hasitem' - self.player_inventory['gold'] += self.item['price'] + self.player_inventory['gold'] += self.item_to_be_purchased['price'] else: self.state = 'accept' - self.add_player_item(self.item) + self.add_player_item(self.item_to_be_purchased) def sell_item(self): """Allow player to sell item to shop""" - self.player_inventory['gold'] += (self.item['price'] / 2) + item_price = self.item_to_be_sold['price'] + item_name = self.item_to_be_sold['type'] + self.player_inventory['gold'] += (item_price / 2) self.state = 'acceptsell' - del self.player_inventory[self.item['type']] + if self.player_inventory[item_name]['quantity'] > 1: + self.player_inventory[item_name]['quantity'] -= 1 + else: + del self.player_inventory[self.item_to_be_sold['type']]

@@ -314,11 +326,6 @@ self.allow_input = True

- - - - - def reject_insufficient_gold(self, keys, current_time): """Reject player selection if they do not have enough gold""" dialogue = ["You don't have enough gold!"]

@@ -406,16 +413,28 @@

def sell_items(self, keys, current_time): """Have player select items to sell""" dialogue = ["What would you like to sell?"] - choices = [self.item_to_be_sold, 'Cancel'] + choices = [] + item_list = [] + for item in self.items: + if item['type'] in self.player_inventory: + name = item['type'] + price = " (" + str(item['price'] / 2) + " gold)" + choices.append(name + price) + item_list.append(name) + choices.append('Cancel') self.dialogue_box = self.make_dialogue_box(dialogue, 0) self.selection_box = self.make_selection_box(choices) - self.selection_arrow.rect.topleft = self.two_arrow_pos_list[self.arrow_index] + + if len(choices) == 2: + self.selection_arrow.rect.topleft = self.two_arrow_pos_list[self.arrow_index] + elif len(choices) == 3: + self.selection_arrow.rect.topleft = self.arrow_pos_list[self.arrow_index] if keys[pg.K_DOWN] and self.allow_input: if self.arrow_index < (len(self.arrow_pos_list) - 1): self.arrow_index += 1 self.allow_input = False - elif keys[pg.K_UP]: + elif keys[pg.K_UP] and self.allow_input: if self.arrow_index > 0: self.arrow_index -= 1 self.allow_input = False

@@ -423,6 +442,16 @@ elif keys[pg.K_SPACE] and self.allow_input:

if self.arrow_index == 0: self.state = 'confirmsell' self.allow_input = False + for item in self.items: + if item['type'] == item_list[0]: + self.item_to_be_sold = item + + elif self.arrow_index == 1 and len(choices) == 3: + self.state = 'confirmsell' + self.allow_input = False + for item in self.items: + if item['type'] == choices[1]: + self.item_to_be_sold = item else: self.state = 'buysell' self.allow_input = False

@@ -434,11 +463,8 @@

def check_for_sellable_items(self): """Check for sellable items""" - for item in self.player_inventory: if item in self.sellable_items: - sell_price = self.player_inventory[item]['value'] / 2 - self.item_to_be_sold = item + " (" + str(sell_price) + " gold)" return True else: return False
M data/states/shop.pydata/states/shop.py

@@ -29,7 +29,7 @@ self.get_image = tools.get_image

self.dialogue = self.make_dialogue() self.accept_dialogue = self.make_accept_dialogue() self.accept_sale_dialogue = self.make_accept_sale_dialogue() - self.item = self.make_purchasable_items() + self.items = self.make_purchasable_items() self.background = self.make_background() self.gui = shopgui.Gui(self)

@@ -156,7 +156,7 @@ def __init__(self):

super(WeaponShop, self).__init__() self.name = 'Weapon Shop' self.key = 'weaponman' - self.sell_items = ['Long Sword'] + self.sell_items = ['Long Sword', 'Rapier'] def make_dialogue(self):

@@ -167,15 +167,20 @@

def make_purchasable_items(self): """Make list of items to be chosen""" - dialogue = ['Long Sword (100 gold)', - 'Cancel'] + longsword_dialogue = 'Long Sword (100 gold)' + rapier_dialogue = 'Rapier (50 gold)' item = {'type': 'Long Sword', 'price': 100, 'quantity': 1, - 'dialogue': dialogue} + 'dialogue': longsword_dialogue} + + item2 = {'type': 'Rapier', + 'price': 50, + 'quantity': 1, + 'dialogue': rapier_dialogue} - return item + return [item, item2] class ArmorShop(Shop):

@@ -184,7 +189,7 @@ def __init__(self):

super(ArmorShop, self).__init__() self.name = 'Armor Shop' self.key = 'armorman' - self.sell_items = ['Chain Mail'] + self.sell_items = ['Chain Mail', 'Wooden Shield'] def make_dialogue(self):

@@ -195,15 +200,20 @@

def make_purchasable_items(self): """Make list of items to be chosen""" - dialogue = ['Chain Mail (50 gold)', - 'Cancel'] + chainmail_dialogue = 'Chain Mail (50 gold)' + shield_dialogue = 'Wooden Shield (75 gold)' item = {'type': 'Chain Mail', 'price': 50, 'quantity': 1, - 'dialogue': dialogue} + 'dialogue': chainmail_dialogue} + + item2 = {'type': 'Wooden Shield', + 'price': 75, + 'quantity': 1, + 'dialogue': shield_dialogue} - return item + return [item, item2] class MagicShop(Shop):