all repos — Legends-RPG @ fcdceff478229e97c1fc26895eeadabefefb02c5

A fantasy mini-RPG built with Python and Pygame.

Added selection menu to shop. You can enter and leave the shop now.
Justin Armstrong justinmeister@gmail.com
Thu, 20 Mar 2014 00:44:07 -0700
commit

fcdceff478229e97c1fc26895eeadabefefb02c5

parent

0283cc01f0a6676c7ec039186b4b1338718b5f12

3 files changed, 145 insertions(+), 15 deletions(-)

jump to
M data/components/textbox.pydata/components/textbox.py

@@ -40,7 +40,7 @@ image.set_colorkey(c.BLACK)

image.blit(self.bground, (0, 0)) dialogue_image = self.font.render(self.dialogue_list[self.index], - False, + True, c.NEAR_BLACK) dialogue_rect = dialogue_image.get_rect(left=50, top=50) image.blit(dialogue_image, dialogue_rect)
M data/states/shop.pydata/states/shop.py

@@ -8,7 +8,133 @@ import copy

import pygame as pg from .. import tools, setup from .. import constants as c -from .. components import person, textbox +from .. components import textbox + + +class Gui(object): + """Class that controls the GUI of the shop state""" + def __init__(self, name, dialogue, level): + self.name = name + self.state = 'dialogue' + self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 22) + self.index = 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.gold = self.make_gold_box() + self.selection_box = self.make_selection_box() + self.state_dict = self.make_state_dict() + self.level = level + + + def make_dialogue_box(self): + """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], + True, + c.NEAR_BLACK) + dialogue_rect = dialogue.get_rect(left=50, top=50) + surface.blit(dialogue, dialogue_rect) + sprite = pg.sprite.Sprite() + sprite.image = surface + sprite.rect = rect + self.check_to_draw_arrow(sprite) + + return sprite + + + def make_selection_box(self): + """Make the box for the player to select options""" + image = setup.GFX['shopbox'] + rect = image.get_rect(bottom=608) + + surface = pg.Surface(rect.size) + #surface.set_colorkey(c.BLACK) + surface.blit(image, (0, 0)) + choices = ['Rent a room. (30 Gold)', + 'Leave.'] + 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) + choice2_rect = choice2.get_rect(x=200, y=75) + surface.blit(choice1, choice1_rect) + surface.blit(choice2, choice2_rect) + sprite = pg.sprite.Sprite() + sprite.image = surface + sprite.rect = rect + + return sprite + + + def check_to_draw_arrow(self, sprite): + """Blink arrow if more text needs to be read""" + if self.index < len(self.dialogue) - 1: + sprite.image.blit(self.arrow.image, self.arrow.rect) + + + def make_gold_box(self): + """Make the box to display total gold""" + return None + + + def make_state_dict(self): + """Make the state dictionary for the GUI behavior""" + state_dict = {'dialogue': self.control_dialogue, + 'select': self.make_selection} + + return state_dict + + + def control_dialogue(self, keys, current_time): + """Control the dialogue boxes""" + self.dialogue_box = self.make_dialogue_box() + + if self.index < (len(self.dialogue) - 1): + if keys[pg.K_SPACE]: + self.index += 1 + + elif self.index == (len(self.dialogue) - 1): + self.state = 'select' + + + def make_selection(self, keys, current_time): + """Control the selection""" + 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]: + if self.selection_arrow.rect.topleft == self.arrow_pos2: + self.level.done = True + + + + + def update(self, keys, current_time): + """Updates the shop GUI""" + state_function = self.state_dict[self.state] + state_function(keys, current_time) + + + def draw(self, surface): + """Draw GUI to level surface""" + if self.state == 'dialogue': + surface.blit(self.dialogue_box.image, self.dialogue_box.rect) + elif self.state == 'select': + 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) + class Shop(tools._State):

@@ -24,11 +150,22 @@ self.persist = persist

self.current_time = current_time self.state = 'normal' self.get_image = tools.get_image + self.dialogue = self.make_dialogue() self.background = self.make_background() self.player = None self.sprites = None - self.text_handler = textbox.TextHandler(self) - self.make_greeting_text() + self.gui = Gui('Inn', self.dialogue, self) + + + + + def make_dialogue(self): + """Make the list of dialogue phrases""" + dialogue = ["Welcome to the " + self.name + "!", + "Would you like to rent a room to restore your health?"] + + return dialogue + def make_background(self):

@@ -68,15 +205,6 @@

return sprite - def make_greeting_text(self): - """Make the textbox for the shop owner's greeting""" - dialogue = ["Welcome to the " + self.name + "!", - "Would you like to spend 30 gold on a room tonight?"] - textbox = self.text_handler.make_textbox('dialoguebox', dialogue) - - self.text_handler.textbox = textbox - - def make_counter(self): """Make the counter to conduct business""" sprite_sheet = copy.copy(setup.GFX['house'])

@@ -90,11 +218,13 @@

def update(self, surface, keys, current_time): """Update level state""" - self.text_handler.update_for_shops(keys, current_time) + self.gui.update(keys, current_time) self.draw_level(surface) + if self.done: + self.next = c.TOWN def draw_level(self, surface): """Blit graphics to game surface""" surface.blit(self.background.image, self.background.rect) - self.text_handler.draw(surface)+ self.gui.draw(surface)