all repos — Legends-RPG @ a3ef5e6b4ee7fab5e9532a97a89dcf999279f5df

A fantasy mini-RPG built with Python and Pygame.

Menu system for inn is now fully functional
Justin Armstrong justinmeister@gmail.com
Fri, 21 Mar 2014 22:34:02 -0700
commit

a3ef5e6b4ee7fab5e9532a97a89dcf999279f5df

parent

88d77458a8099a4f68eaee9b37e58a836efe89bc

2 files changed, 75 insertions(+), 11 deletions(-)

jump to
M data/states/shop.pydata/states/shop.py

@@ -19,26 +19,27 @@ self.name = name

self.state = 'dialogue' self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 22) self.index = 0 + self.timer = 0.0 self.dialogue = dialogue self.arrow = textbox.NextArrow() self.selection_arrow = textbox.NextArrow() self.arrow_pos1 = (50, 485) self.arrow_pos2 = (50, 535) self.selection_arrow.rect.topleft = self.arrow_pos1 - self.dialogue_box = self.make_dialogue_box() + self.dialogue_box = self.make_dialogue_box(self.dialogue, self.index) self.gold_box = self.make_gold_box() self.selection_box = self.make_selection_box() self.state_dict = self.make_state_dict() - def make_dialogue_box(self): + def make_dialogue_box(self, dialogue_list, index): """Make the sprite that controls the dialogue""" image = setup.GFX['dialoguebox'] rect = image.get_rect() surface = pg.Surface(rect.size) surface.set_colorkey(c.BLACK) surface.blit(image, rect) - dialogue = self.font.render(self.dialogue[self.index], + dialogue = self.font.render(dialogue_list[index], True, c.NEAR_BLACK) dialogue_rect = dialogue.get_rect(left=50, top=50)

@@ -59,8 +60,15 @@

surface = pg.Surface(rect.size) surface.set_colorkey(c.BLACK) surface.blit(image, (0, 0)) - choices = ['Rent a room. (30 Gold)', - 'Leave.'] + if self.state == 'select': + choices = ['Rent a room. (30 Gold)', + 'Leave.'] + elif self.state == 'confirm': + choices = ['Yes', + 'No'] + else: + choices = ['Not', + 'assigned'] choice1 = self.font.render(choices[0], True, c.NEAR_BLACK) choice1_rect = choice1.get_rect(x=200, y=25) choice2 = self.font.render(choices[1], True, c.NEAR_BLACK)

@@ -106,14 +114,17 @@

def make_state_dict(self): """Make the state dictionary for the GUI behavior""" state_dict = {'dialogue': self.control_dialogue, - 'select': self.make_selection} + 'select': self.make_selection, + 'confirm': self.confirm_selection, + 'reject': self.reject_insufficient_gold, + 'accept': self.accept_purchase} return state_dict def control_dialogue(self, keys, current_time): """Control the dialogue boxes""" - self.dialogue_box = self.make_dialogue_box() + self.dialogue_box = self.make_dialogue_box(self.dialogue, self.index) if self.index < (len(self.dialogue) - 1): if keys[pg.K_SPACE]:

@@ -121,10 +132,12 @@ self.index += 1

elif self.index == (len(self.dialogue) - 1): self.state = 'select' + self.timer = current_time def make_selection(self, keys, current_time): """Control the selection""" + self.dialogue_box = self.make_dialogue_box(self.dialogue, self.index) self.selection_box = self.make_selection_box() self.gold_box = self.make_gold_box()

@@ -132,15 +145,62 @@ 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]: + elif keys[pg.K_SPACE] and (current_time - self.timer) > 200: 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.timer = current_time + + + + def confirm_selection(self, keys, current_time): + """Confirm selection state for GUI""" + dialogue = ['Are you sure?'] + self.selection_box = self.make_selection_box() + self.gold_box = self.make_gold_box() + self.dialogue_box = self.make_dialogue_box(dialogue, 0) + + 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 (current_time - self.timer) > 200: + if self.selection_arrow.rect.topleft == self.arrow_pos1: self.level.game_data['player items']['gold'] -= 30 + if self.level.game_data['player items']['gold'] < 0: + self.level.game_data['player items']['gold'] += 30 + self.state = 'reject' + else: + self.state = 'accept' + else: + self.state = 'select' + self.timer = current_time + self.selection_arrow.rect.topleft = self.arrow_pos1 + 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!"] + self.dialogue_box = self.make_dialogue_box(dialogue, 0) + if keys[pg.K_SPACE] and (current_time - self.timer) > 200: + self.state = 'select' + self.timer = current_time + self.selection_arrow.rect.topleft = self.arrow_pos1 + + + def accept_purchase(self, keys, current_time): + """Accept purchase and confirm with message""" + dialogue = ["Your health has been restored!"] + self.dialogue_box = self.make_dialogue_box(dialogue, 0) + self.gold_box = self.make_gold_box() + + if keys[pg.K_SPACE] and (current_time - self.timer) > 200: + self.state = 'select' + self.timer = current_time + self.selection_arrow.rect.topleft = self.arrow_pos1 def update(self, keys, current_time):

@@ -151,14 +211,18 @@

def draw(self, surface): """Draw GUI to level surface""" - if self.state == 'dialogue': + state_list1 = ['dialogue', 'reject', 'accept'] + state_list2 = ['select', 'confirm'] + + if self.state in state_list1: surface.blit(self.dialogue_box.image, self.dialogue_box.rect) surface.blit(self.gold_box.image, self.gold_box.rect) - elif self.state == 'select': + elif self.state in state_list2: surface.blit(self.dialogue_box.image, self.dialogue_box.rect) surface.blit(self.selection_box.image, self.selection_box.rect) surface.blit(self.selection_arrow.image, self.selection_arrow.rect) surface.blit(self.gold_box.image, self.gold_box.rect) +
M data/tools.pydata/tools.py

@@ -153,7 +153,7 @@

def create_game_data_dict(): """Create a dictionary of persistant values the player carries between states""" - player_items = {'gold': 0} + player_items = {'gold': 100} data_dict = {'last location': None,