Player can sell items, it works, but code is really ugly
Justin Armstrong justinmeister@gmail.com
Thu, 27 Mar 2014 21:51:01 -0700
2 files changed,
145 insertions(+),
12 deletions(-)
M
data/shopgui.py
→
data/shopgui.py
@@ -13,6 +13,7 @@ class Gui(object):
"""Class that controls the GUI of the shop state""" def __init__(self, level): self.level = level + self.sellable_items = level.sell_items self.player_inventory = level.game_data['player inventory'] self.name = level.name self.state = 'dialogue'@@ -22,8 +23,10 @@ self.index = 0
self.timer = 0.0 self.allow_input = False self.item = level.item + self.item_to_be_sold = None self.dialogue = level.dialogue self.accept_dialogue = level.accept_dialogue + self.accept_sale_dialogue = level.accept_sale_dialogue self.arrow = textbox.NextArrow() self.selection_arrow = textbox.NextArrow() self.arrow_pos1 = (50, 485)@@ -66,12 +69,15 @@ surface.blit(image, (0, 0))
if self.state == 'select': choices = self.item['dialogue'] - elif self.state == 'confirm': + elif self.state == 'confirmpurchase' or self.state == 'confirmsell': choices = ['Yes', 'No'] elif self.state == 'buysell': choices = ['Buy', 'Sell'] + elif self.state == 'sell': + choices = [self.item_to_be_sold, + 'Cancel'] else: choices = ['Not', 'assigned']@@ -122,11 +128,15 @@ def make_state_dict(self):
"""Make the state dictionary for the GUI behavior""" state_dict = {'dialogue': self.control_dialogue, 'select': self.make_selection, - 'confirm': self.confirm_selection, + 'confirmpurchase': self.confirm_purchase, + 'confirmsell': self.confirm_sell, 'reject': self.reject_insufficient_gold, 'accept': self.accept_purchase, + 'acceptsell': self.accept_sale, 'hasitem': self.has_item, - 'buysell': self.buy_sell} + 'buysell': self.buy_sell, + 'sell': self.sell_items, + 'cantsell': self.cant_sell} return state_dict@@ -164,7 +174,7 @@ if self.selection_arrow.rect.topleft == self.arrow_pos2:
self.level.done = True self.level.game_data['last direction'] = 'down' elif self.selection_arrow.rect.topleft == self.arrow_pos1: - self.state = 'confirm' + self.state = 'confirmpurchase' self.timer = current_time self.allow_input = False@@ -173,7 +183,7 @@ self.allow_input = True
- def confirm_selection(self, keys, current_time): + def confirm_purchase(self, keys, current_time): """Confirm selection state for GUI""" dialogue = ['Are you sure?'] self.selection_box = self.make_selection_box()@@ -191,7 +201,29 @@ self.allow_input = False
else: self.state = self.begin_new_transaction() self.allow_input = False - self.timer = current_time + self.selection_arrow.rect.topleft = self.arrow_pos1 + + if not keys[pg.K_SPACE]: + self.allow_input = True + + + def confirm_sell(self, keys, current_time): + """Confirm player wants to sell item""" + dialogue = ['Are you sure?'] + self.dialogue_box = self.make_dialogue_box(dialogue, 0) + self.selection_box = self.make_selection_box() + + if keys[pg.K_DOWN]: + self.selection_arrow.rect.topleft = self.arrow_pos2 + elif keys[pg.K_UP]: + self.selection_arrow.rect.topleft = self.arrow_pos1 + elif keys[pg.K_SPACE] and self.allow_input: + if self.selection_arrow.rect.topleft == self.arrow_pos1: + self.sell_item() + self.allow_input = False + else: + self.state = self.begin_new_transaction() + self.allow_input = False self.selection_arrow.rect.topleft = self.arrow_pos1 if not keys[pg.K_SPACE]:@@ -226,6 +258,34 @@ self.state = 'accept'
self.add_player_item(self.item) + def sell_item(self): + """Allow player to sell item to shop""" + self.player_inventory['gold'] += (self.item['price'] / 2) + self.state = 'acceptsell' + del self.player_inventory[self.item['type']] + + + + def accept_sale(self, keys, current_time): + """Confirm to player that item was sold""" + self.dialogue_box = self.make_dialogue_box(self.accept_sale_dialogue, 0) + self.gold_box = self.make_gold_box() + + if keys[pg.K_SPACE] and self.allow_input: + self.state = self.begin_new_transaction() + self.selection_arrow.rect.topleft = self.arrow_pos1 + self.allow_input = False + + if not keys[pg.K_SPACE]: + 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!"]@@ -283,24 +343,87 @@ if self.selection_arrow.rect.topleft == self.arrow_pos1:
self.state = 'select' self.allow_input = False else: - self.state = 'select' + if self.check_for_sellable_items(): + self.state = 'sell' + self.allow_input = False + else: + self.state = 'cantsell' + self.allow_input = False + self.selection_arrow.rect.topleft = self.arrow_pos1 + + if not keys[pg.K_SPACE]: + self.allow_input = True + + + def sell_items(self, keys, current_time): + """Have player select items to sell""" + dialogue = ["What would you like to sell?"] + self.dialogue_box = self.make_dialogue_box(dialogue, 0) + self.selection_box = self.make_selection_box() + + if keys[pg.K_DOWN]: + self.selection_arrow.rect.topleft = self.arrow_pos2 + elif keys[pg.K_UP]: + self.selection_arrow.rect.topleft = self.arrow_pos1 + elif keys[pg.K_SPACE] and self.allow_input: + if self.selection_arrow.rect.topleft == self.arrow_pos1: + self.state = 'confirmsell' + self.allow_input = False + else: + + self.state = 'buysell' self.allow_input = False + self.selection_arrow.rect.topleft = self.arrow_pos1 + if not keys[pg.K_SPACE]: self.allow_input = True + 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 + + + def cant_sell(self, keys, current_time): + """Do not allow player to sell anything""" + dialogue = ["You don't have anything to sell!"] + self.dialogue_box = self.make_dialogue_box(dialogue, 0) + + if keys[pg.K_SPACE] and self.allow_input: + self.state = 'buysell' + self.allow_input = False + + + if not keys[pg.K_SPACE]: + self.allow_input = True + + + + def add_player_item(self, item): """Add item to player's inventory""" item_type = item['type'] quantity = item['quantity'] - player_item = self.level.game_data['player inventory'] + value = item['price'] + player_items = self.level.game_data['player inventory'] - if item_type in player_item: - player_item[item_type] += quantity + item_to_add = {'quantity': quantity, + 'value': value} + + if item_type in player_items: + player_items[item_type]['quantity'] += quantity elif quantity > 0: - player_item[item_type] = quantity + player_items[item_type] = item_to_add +@@ -313,7 +436,7 @@
def draw(self, surface): """Draw GUI to level surface""" state_list1 = ['dialogue', 'reject', 'accept', 'hasitem'] - state_list2 = ['select', 'confirm', 'buysell'] + state_list2 = ['select', 'confirmpurchase', 'buysell', 'sell', 'confirmsell'] surface.blit(self.dialogue_box.image, self.dialogue_box.rect) surface.blit(self.gold_box.image, self.gold_box.rect)
M
data/states/shop.py
→
data/states/shop.py
@@ -17,6 +17,7 @@ """Basic shop state"""
def __init__(self): super(Shop, self).__init__() self.key = None + self.sell_items = None def startup(self, current_time, game_data): """Startup state"""@@ -27,6 +28,7 @@ self.next = c.TOWN
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.background = self.make_background() self.gui = shopgui.Gui(self)@@ -42,6 +44,11 @@ """Make the dialogue for when the player buys an item"""
return ['Item purchased.'] + def make_accept_sale_dialogue(self): + """Make the dialogue for when the player sells an item""" + return ['Item sold.'] + + def make_purchasable_items(self): """Make the list of items to be bought at shop""" raise NotImplementedError@@ -149,6 +156,7 @@ def __init__(self):
super(WeaponShop, self).__init__() self.name = 'Weapon Shop' self.key = 'weaponman' + self.sell_items = ['Long Sword'] def make_dialogue(self):@@ -176,6 +184,7 @@ def __init__(self):
super(ArmorShop, self).__init__() self.name = 'Armor Shop' self.key = 'armorman' + self.sell_items = ['Chain Mail'] def make_dialogue(self):@@ -230,6 +239,7 @@ def __init__(self):
super(PotionShop, self).__init__() self.name = 'Potion Shop' self.key = 'potionlady' + self.sell_items = 'Healing Potion' def make_dialogue(self):