all repos — Legends-RPG @ 97226ef32d8c0fe20424beca13a494787cc000a4

A fantasy mini-RPG built with Python and Pygame.

Fleshed out menu a little more.
Justin Armstrong justinmeister@gmail.com
Mon, 31 Mar 2014 22:04:47 -0700
commit

97226ef32d8c0fe20424beca13a494787cc000a4

parent

042b912afd55ebeecab540dc4a44dd070f886b6a

M data/menugui.pydata/menugui.py

@@ -7,20 +7,44 @@ from . import setup

from . import constants as c +class SmallArrow(pg.sprite.Sprite): + """Small arrow for menu""" + def __init__(self): + super(SmallArrow, self).__init__() + self.image = setup.GFX['smallarrow'] + self.rect = self.image.get_rect() + class MenuGui(object): - def __init__(self, inventory): + def __init__(self, level, inventory): + self.level = level self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 22) self.inventory = inventory - self.gold_box = self.make_gold_box() - self.stat_box = self.make_stat_box() - self.selection_box = self.make_selection_box() - self.box_group = self.make_box_group() + self.allow_input = False + self.state = 'topmenu' + self.gold_box = None + self.stat_box = None + self.selection_box = None + self.arrow = SmallArrow() + self.arrow_index = 0 + self.arrow_pos = self.make_arrow_pos_list() + self.state_dict = self.make_state_dict() + + + def make_arrow_pos_list(self): + """Make the list of possible arrow positions""" + pos_list = [] + + for i in range(4): + pos = (35, 356 + (i * 50)) + pos_list.append(pos) + + return pos_list def make_gold_box(self): """Makes the box that displays gold""" - image = setup.GFX['goldbox'] + image = setup.GFX['goldbox2'] rect = image.get_rect(left=10, top=234) surface = pg.Surface(rect.size)

@@ -29,7 +53,8 @@ surface.blit(image, (0, 0))

text = "Gold: " + str(self.inventory['gold']) text_render = self.font.render(text, True, c.NEAR_BLACK) - text_rect = text_render.get_rect(x=80, y=60) + text_rect = text_render.get_rect(centerx=130, + centery=35) surface.blit(text_render, text_rect) sprite = pg.sprite.Sprite()

@@ -55,15 +80,21 @@

return sprite - def make_selection_box(self): + def make_selection_box(self, choices): """Make the menu with selection options""" - image = setup.GFX['goldbox'] - rect = image.get_rect(left=10, top=410) + image = setup.GFX['selectionbox'] + rect = image.get_rect(left=10, top=330) surface = pg.Surface(rect.size) surface.set_colorkey(c.BLACK) surface.blit(image, (0, 0)) + for i, choice in enumerate(choices): + choice_image = self.font.render(choice, True, c.NEAR_BLACK) + y_position = 25 + (i * 50) + choice_rect = choice_image.get_rect(x=100, y=y_position) + surface.blit(choice_image, choice_rect) + sprite = pg.sprite.Sprite() sprite.image = surface sprite.rect = rect

@@ -77,8 +108,48 @@ return pg.sprite.Group(self.gold_box,

self.stat_box, self.selection_box) + + def make_state_dict(self): + """Make the dictionary of all menu states""" + state_dict = {'topmenu': self.select_main_options} + + return state_dict + + + def select_main_options(self, keys): + """Allow player to select items, magic, weapons and armor""" + choices = ['Equip', 'Items', 'Magic', 'Exit'] + self.selection_box = self.make_selection_box(choices) + self.gold_box = self.make_gold_box() + self.stat_box = self.make_stat_box() + self.arrow.rect.topleft = self.arrow_pos[self.arrow_index] + + if self.allow_input: + if keys[pg.K_DOWN]: + if self.arrow_index < len(choices) - 1: + self.arrow_index += 1 + self.allow_input = False + elif keys[pg.K_UP]: + if self.arrow_index > 0: + self.arrow_index -= 1 + self.allow_input = False + elif keys[pg.K_SPACE]: + if self.arrow_index == len(choices) - 1: + self.level.done = True + + if not keys[pg.K_DOWN] and not keys[pg.K_UP]: + self.allow_input = True + + + def update(self, keys): - pass + state_function = self.state_dict[self.state] + state_function(keys) + def draw(self, surface): - self.box_group.draw(surface)+ if self.gold_box and self.stat_box and self.selection_box: + surface.blit(self.gold_box.image, self.gold_box.rect) + surface.blit(self.stat_box.image, self.stat_box.rect) + surface.blit(self.selection_box.image, self.selection_box.rect) + surface.blit(self.arrow.image, self.arrow.rect)
M data/shopgui.pydata/shopgui.py

@@ -50,6 +50,7 @@ self.selection_box = self.make_selection_box(choices)

self.state_dict = self.make_state_dict() + def make_dialogue_box(self, dialogue_list, index): """Make the sprite that controls the dialogue""" image = setup.GFX['dialoguebox']
M data/states/main_menu/main_menu.pydata/states/main_menu/main_menu.py

@@ -1,9 +1,6 @@

-__author__ = 'justinarmstrong' - import pygame as pg from ... import setup, tools from ... import constants as c - class Menu(tools._State): def __init__(self):

@@ -11,7 +8,7 @@ super(Menu, self).__init__()

self.next = c.TOWN self.surface = setup.SCREEN self.rect = self.surface.get_rect() - text = 'Main Menu placeholder' + text = 'Arrows for navigation, space for input, Enter/Return for menu' self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 15) self.rendered_text = self.font.render(text, 1, c.BLACK) self.text_rect = self.rendered_text.get_rect()

@@ -28,4 +25,4 @@

def get_event(self, event): if event.type == pg.KEYDOWN: self.game_data['last state'] = self.name - self.done = True+ self.done = True
M data/states/player_menu.pydata/states/player_menu.py

@@ -17,11 +17,12 @@

def startup(self, current_time, game_data): """Call when state is switched to""" inventory = game_data['player inventory'] + self.next = game_data['last state'] self.allow_input = False self.game_data = game_data self.current_time = current_time self.background = self.make_background() - self.gui = menugui.MenuGui(inventory) + self.gui = menugui.MenuGui(self, inventory) def make_background(self):

@@ -58,18 +59,8 @@ return sprite

def update(self, surface, keys, current_time): - self.check_for_quit(keys) self.gui.update(keys) self.draw(surface) - - - def check_for_quit(self, keys): - if keys[pg.K_RETURN] and self.allow_input: - self.done = True - self.next = 'town' - - if not keys[pg.K_RETURN]: - self.allow_input = True def draw(self, surface):