all repos — Legends-RPG @ 042b912afd55ebeecab540dc4a44dd070f886b6a

A fantasy mini-RPG built with Python and Pygame.

Began player menu screen
Justin Armstrong justinmeister@gmail.com
Mon, 31 Mar 2014 16:50:21 -0700
commit

042b912afd55ebeecab540dc4a44dd070f886b6a

parent

a2ffdf647695282f3e789bc49cd116ff8b677ad2

M data/constants.pydata/constants.py

@@ -21,6 +21,8 @@ NEAR_BLACK = (1, 0, 0)

WHITE = (255, 255, 255) BLACK_BLUE = (19, 15, 48) NEAR_BLACK_BLUE = (20, 15, 48) +LIGHT_BLUE = (0, 153, 204) +DARK_RED = (118, 27, 12)
M data/main.pydata/main.py

@@ -3,6 +3,7 @@ from data.states.town import town

from data.states.castle import castle from data.states import shop from data.states.house import house +from data.states import player_menu from . import setup, tools from . import constants as c

@@ -16,6 +17,7 @@ ARMOR_SHOP = 'armor shop'

WEAPON_SHOP = 'weapon shop' MAGIC_SHOP = 'magic shop' POTION_SHOP = 'potion shop' +PLAYER_MENU = 'player menu' def main():

@@ -29,7 +31,8 @@ INN: shop.Inn(),

ARMOR_SHOP: shop.ArmorShop(), WEAPON_SHOP: shop.WeaponShop(), MAGIC_SHOP: shop.MagicShop(), - POTION_SHOP: shop.PotionShop() + POTION_SHOP: shop.PotionShop(), + PLAYER_MENU: player_menu.Player_Menu() } run_it.setup_states(state_dict, c.MAIN_MENU)
A data/menugui.py

@@ -0,0 +1,84 @@

+""" +This class controls all the GUI for the player +menu screen. +""" +import pygame as pg +from . import setup +from . import constants as c + + + +class MenuGui(object): + def __init__(self, inventory): + 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() + + + def make_gold_box(self): + """Makes the box that displays gold""" + image = setup.GFX['goldbox'] + rect = image.get_rect(left=10, top=234) + + surface = pg.Surface(rect.size) + surface.set_colorkey(c.BLACK) + 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) + surface.blit(text_render, text_rect) + + sprite = pg.sprite.Sprite() + sprite.image = surface + sprite.rect = rect + + return sprite + + + def make_stat_box(self): + """Make the box for displaying stats and items""" + image = setup.GFX['playerstatsbox'] + rect = image.get_rect(left=285, top=35) + + surface = pg.Surface(rect.size) + surface.set_colorkey(c.BLACK) + surface.blit(image, (0,0)) + + sprite = pg.sprite.Sprite() + sprite.image = surface + sprite.rect = rect + + return sprite + + + def make_selection_box(self): + """Make the menu with selection options""" + image = setup.GFX['goldbox'] + rect = image.get_rect(left=10, top=410) + + surface = pg.Surface(rect.size) + surface.set_colorkey(c.BLACK) + surface.blit(image, (0, 0)) + + sprite = pg.sprite.Sprite() + sprite.image = surface + sprite.rect = rect + + return sprite + + + def make_box_group(self): + """Make the sprite group for each GUI box""" + return pg.sprite.Group(self.gold_box, + self.stat_box, + self.selection_box) + + def update(self, keys): + pass + + def draw(self, surface): + self.box_group.draw(surface)
M data/states/level_state.pydata/states/level_state.py

@@ -28,6 +28,7 @@ self.game_data = game_data

print(game_data['player inventory']) self.current_time = current_time self.state = 'normal' + self.allow_input = False self.background = tm.create_map_layer1(self.name, self.map_width, self.map_height)

@@ -74,6 +75,7 @@ self.player.update(keys, current_time)

self.sprites.update(current_time) self.collision_handler.update(keys, current_time) self.dialogue_handler.update(keys, current_time) + self.check_for_menu(keys) self.viewport_update() self.draw_level(surface)

@@ -90,6 +92,20 @@ self.update_game_data()

self.done = True + def check_for_menu(self, keys): + """Check if player hits enter to go to menu""" + if keys[pg.K_RETURN] and self.allow_input: + if self.player.state == 'resting': + self.player.location = self.player.get_tile_location() + self.next = 'player menu' + self.update_game_data() + self.done = True + + + if not keys[pg.K_RETURN]: + self.allow_input = True + + def update_game_data(self): """Update the persistant game data dictionary""" self.game_data['last location'] = self.player.location

@@ -105,7 +121,9 @@ location = copy.deepcopy(self.game_data['last location'])

direction = self.game_data['last direction'] state = self.game_data['last state'] - if direction == 'up': + if self.next == 'player menu': + pass + elif direction == 'up': location[1] += 1 elif direction == 'down': location[1] -= 1
A data/states/player_menu.py

@@ -0,0 +1,78 @@

+""" +This is the state where the player can look at +his inventory, equip items and check stats. +""" +import pygame as pg +from .. import tools, setup, menugui +from .. import constants as c + + +class Player_Menu(tools._State): + def __init__(self): + super(Player_Menu, self).__init__() + self.get_image = tools.get_image + + + + def startup(self, current_time, game_data): + """Call when state is switched to""" + inventory = game_data['player inventory'] + self.allow_input = False + self.game_data = game_data + self.current_time = current_time + self.background = self.make_background() + self.gui = menugui.MenuGui(inventory) + + + def make_background(self): + """Makes the generic black/blue background""" + background = pg.sprite.Sprite() + surface = pg.Surface(c.SCREEN_SIZE).convert() + surface.fill(c.BLACK_BLUE) + background.image = surface + background.rect = background.image.get_rect() + + player = self.make_sprite('player', 96, 32) + + background.image.blit(player.image, player.rect) + + return background + + + def make_sprite(self, key, coordx, coordy, x=40, y=25): + """Get the image for the player""" + spritesheet = setup.GFX[key] + surface = pg.Surface((32, 32)) + surface.set_colorkey(c.BLACK) + image = self.get_image(coordx, coordy, 32, 32, spritesheet) + rect = image.get_rect() + surface.blit(image, rect) + + surface = pg.transform.scale(surface, (192, 192)) + rect = surface.get_rect(left=x, top=y) + sprite = pg.sprite.Sprite() + sprite.image = surface + sprite.rect = rect + + 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): + surface.blit(self.background.image, self.background.rect) + self.gui.draw(surface) +