Adding a box around each sprite so they don't wander too far. Added a buy/sell screen to appropriate shops.
@@ -67,6 +67,9 @@ sprite.kill()
if pg.sprite.spritecollideany(sprite, self.sprites): sprite_collided_list.append(sprite) self.sprites.add(sprite) + for blocker in sprite.wander_box: + if sprite.rect.colliderect(blocker): + sprite_collided_list.append(sprite) for sprite in sprite_collided_list:
@@ -34,6 +34,7 @@ self.location = self.get_tile_location()
self.dialogue = ['Location: ' + str(self.location)] self.default_direction = direction self.item = None + self.wander_box = self.make_wander_box() def create_spritesheet_dict(self, sheet_key):@@ -155,6 +156,30 @@ else:
tile_y = 0 return [tile_x, tile_y] + + + def make_wander_box(self): + """Make a list of rects that surround the initial location + of a sprite to limit his/her wandering""" + x = self.location[0] + y = self.location[1] + box_list = [] + box_rects = [] + + for i in range(x-3, x+4): + box_list.append([i, y-3]) + box_list.append([i, y+3]) + + for i in range(y-2, y+3): + box_list.append([x-3, i]) + box_list.append([x+3, i]) + + for box in box_list: + left = box[0]*32 + top = box[1]*32 + box_rects.append(pg.Rect(left, top, 32, 32)) + + return box_rects def resting(self):@@ -366,6 +391,7 @@ self.state = 'resting'
self.direction = 'down' self.default_direction = self.direction self.item = None + self.wander_box = [] def get_location(self): """Get tile location"""
@@ -16,6 +16,7 @@ self.level = level
self.player_inventory = level.game_data['player inventory'] self.name = level.name self.state = 'dialogue' + self.no_selling = ['Inn', 'Magic Shop'] self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 22) self.index = 0 self.timer = 0.0@@ -68,6 +69,9 @@ choices = self.item['dialogue']
elif self.state == 'confirm': choices = ['Yes', 'No'] + elif self.state == 'buysell': + choices = ['Buy', + 'Sell'] else: choices = ['Not', 'assigned']@@ -121,7 +125,8 @@ 'select': self.make_selection,
'confirm': self.confirm_selection, 'reject': self.reject_insufficient_gold, 'accept': self.accept_purchase, - 'hasitem': self.has_item} + 'hasitem': self.has_item, + 'buysell': self.buy_sell} return state_dict@@ -136,8 +141,8 @@ self.index += 1
self.allow_input = False if self.index == (len(self.dialogue) - 1): - self.state = 'select' - self.timer = current_time + self.state = self.begin_new_transaction() + if not keys[pg.K_SPACE]: self.allow_input = True@@ -184,13 +189,24 @@ if self.selection_arrow.rect.topleft == self.arrow_pos1:
self.buy_item() self.allow_input = False else: - self.state = 'select' + 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 begin_new_transaction(self): + """Set state to buysell or select, depending if the shop + is a Inn/Magic shop or not""" + if self.level.name in self.no_selling: + state = 'select' + else: + state = 'buysell' + + return state def buy_item(self):@@ -216,8 +232,7 @@ dialogue = ["You don't have enough gold!"]
self.dialogue_box = self.make_dialogue_box(dialogue, 0) if keys[pg.K_SPACE] and self.allow_input: - self.state = 'select' - self.timer = current_time + self.state = self.begin_new_transaction() self.selection_arrow.rect.topleft = self.arrow_pos1 self.allow_input = False@@ -231,8 +246,7 @@ self.dialogue_box = self.make_dialogue_box(self.accept_dialogue, 0)
self.gold_box = self.make_gold_box() if keys[pg.K_SPACE] and self.allow_input: - self.state = 'select' - self.timer = current_time + self.state = self.begin_new_transaction() self.selection_arrow.rect.topleft = self.arrow_pos1 self.allow_input = False@@ -246,8 +260,7 @@ dialogue = ["You have that item already."]
self.dialogue_box = self.make_dialogue_box(dialogue, 0) if keys[pg.K_SPACE] and self.allow_input: - self.state = 'select' - self.timer = current_time + self.state = self.begin_new_transaction() self.selection_arrow.rect.topleft = self.arrow_pos1 self.allow_input = False@@ -255,6 +268,29 @@ if not keys[pg.K_SPACE]:
self.allow_input = True + def buy_sell(self, keys, current_time): + """Ask player if they want to buy or sell something""" + dialogue = ["Would you like to buy or sell an item?"] + 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 = 'select' + self.allow_input = False + else: + self.state = 'select' + self.allow_input = False + self.selection_arrow.rect.topleft = self.arrow_pos1 + + 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']@@ -277,7 +313,7 @@
def draw(self, surface): """Draw GUI to level surface""" state_list1 = ['dialogue', 'reject', 'accept', 'hasitem'] - state_list2 = ['select', 'confirm'] + state_list2 = ['select', 'confirm', 'buysell'] if self.state in state_list1: surface.blit(self.dialogue_box.image, self.dialogue_box.rect)
@@ -154,7 +154,7 @@
def make_dialogue(self): """Make the list of dialogue phrases""" return ["Welcome to the " + self.name + "!", - "Would you like to buy a weapon?"] + "What weapon would you like to buy?"] def make_purchasable_items(self):@@ -181,7 +181,7 @@
def make_dialogue(self): """Make the list of dialogue phrases""" return ["Welcome to the " + self.name + "!", - "Would you like to buy a piece of armor?"] + "Would piece of armor would you like to buy?"] def make_purchasable_items(self):@@ -208,7 +208,7 @@
def make_dialogue(self): """Make the list of dialogue phrases""" return ["Welcome to the " + self.name + "!", - "Would you like to buy a magic spell?"] + "Would magic spell would you like to buy?"] def make_purchasable_items(self):@@ -235,7 +235,7 @@
def make_dialogue(self): """Make the list of dialogue phrases""" return ["Welcome to the " + self.name + "!", - "Would you like to buy a potion?"] + "What potion would you like to buy?"] def make_purchasable_items(self):