all repos — Legends-RPG @ 88d77458a8099a4f68eaee9b37e58a836efe89bc

A fantasy mini-RPG built with Python and Pygame.

You can now buy a room and gold will be deducted. Your gold total is displayed. You now leave the shop in the proper direction.
Justin Armstrong justinmeister@gmail.com
Thu, 20 Mar 2014 10:18:20 -0700
commit

88d77458a8099a4f68eaee9b37e58a836efe89bc

parent

fcdceff478229e97c1fc26895eeadabefefb02c5

M data/components/person.pydata/components/person.py

@@ -273,7 +273,6 @@ """User controlled character"""

def __init__(self, direction, x=0, y=0): super(Player, self).__init__('player', x, y, direction) - self.item_list = [] def create_vector_dict(self):
M data/components/textbox.pydata/components/textbox.py

@@ -93,8 +93,8 @@ image.set_colorkey(c.BLACK)

image.blit(self.bground, (0, 0)) if self.item: - total = str(self.item['total']) - type = self.item['type'] + type = list(self.item.keys())[0] + total = str(self.item[type]) dialogue = 'You received ' + total + ' ' + type + '.' self.dialogue_list = [dialogue] self.item = None

@@ -119,7 +119,7 @@ self.talking_sprite = None

self.textbox = None self.level = level self.last_textbox_timer = 0.0 - self.game_data = level.persist + self.game_data = level.game_data def update(self, keys, current_time):

@@ -182,7 +182,8 @@ def check_for_item(self):

"""Checks if sprite has an item to give to the player""" item = self.talking_sprite.item if item: - self.player.item_list.append(item) + if 'gold' in item: + self.game_data['player items']['gold'] += item['gold'] self.talking_sprite.item = None if self.talking_sprite.name == 'king': self.game_data['king item'] = None

@@ -215,21 +216,3 @@ textbox = None

return textbox - - def update_for_shops(self, keys, current_time): - """Update text handler when player is in a shop""" - self.textbox.update(keys, current_time) - last_index = len(self.textbox.dialogue_list) - 1 - - if self.textbox.done and (self.textbox.index < last_index): - index = self.textbox.index + 1 - dialogue = self.textbox.dialogue_list - self.textbox = DialogueBox(dialogue, index) - - - - - - - -
M data/states/castle/castle.pydata/states/castle/castle.py

@@ -21,6 +21,6 @@ "An evil sorceror has stolen my magic crown!",

"Without it, our town will be overun by monsters!", "Take this money for supplies.", "Our town's fate is in your hands!"] - sprite.item = self.persist['king item'] + sprite.item = self.game_data['king item'] else: sprite.dialogue = ['Hail to the King!']
M data/states/level_state.pydata/states/level_state.py

@@ -20,9 +20,9 @@ self.map_width = width

self.map_height = height - def startup(self, current_time, persist): + def startup(self, current_time, game_data): """Called when the State object is created""" - self.persist = persist + self.game_data = game_data self.current_time = current_time self.state = 'normal' self.town_map = tm.make_level_map(self.name,

@@ -32,12 +32,12 @@ self.viewport = tm.create_viewport(self.town_map)

self.blockers = tm.create_blockers(self.name) self.level_surface = tm.make_level_surface(self.town_map) self.level_rect = self.level_surface.get_rect() - self.player = person.Player(persist['last direction']) + self.player = person.Player(game_data['last direction']) self.sprites = pg.sprite.Group() self.start_positions = tm.set_sprite_positions(self.player, self.sprites, self.name, - self.persist) + self.game_data) self.set_sprite_dialogue() self.collision_handler = collision.CollisionHandler(self.player, self.blockers,

@@ -86,18 +86,18 @@

def update_game_data(self): """Update the persistant game data dictionary""" - self.persist['last location'] = self.player.location - self.persist['last direction'] = self.player.direction - self.persist['last state'] = self.name + self.game_data['last location'] = self.player.location + self.game_data['last direction'] = self.player.direction + self.game_data['last state'] = self.name self.set_new_start_pos() def set_new_start_pos(self): """Set new start position based on previous state""" - location = copy.deepcopy(self.persist['last location']) - direction = self.persist['last direction'] - state = self.persist['last state'] + location = copy.deepcopy(self.game_data['last location']) + direction = self.game_data['last direction'] + state = self.game_data['last state'] if direction == 'up': location[1] += 1

@@ -108,7 +108,7 @@ location[0] += 1

elif direction == 'right': location[0] -= 1 - self.persist[state + ' start pos'] = location + self.game_data[state + ' start pos'] = location def handling_dialogue(self, surface, keys, current_time):
M data/states/main_menu/main_menu.pydata/states/main_menu/main_menu.py

@@ -16,7 +16,7 @@ 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() self.text_rect.center = self.rect.center - self.persist = tools.create_game_data_dict() + self.game_data = tools.create_game_data_dict() def update(self, surface, keys, current_time):

@@ -26,5 +26,5 @@ surface.blit(self.rendered_text, self.text_rect)

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

@@ -8,12 +8,13 @@ import copy

import pygame as pg from .. import tools, setup from .. import constants as c -from .. components import textbox +from .. components import textbox, person class Gui(object): """Class that controls the GUI of the shop state""" def __init__(self, name, dialogue, level): + self.level = level self.name = name self.state = 'dialogue' self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 22)

@@ -25,10 +26,9 @@ 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.gold_box = 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):

@@ -57,7 +57,7 @@ image = setup.GFX['shopbox']

rect = image.get_rect(bottom=608) surface = pg.Surface(rect.size) - #surface.set_colorkey(c.BLACK) + surface.set_colorkey(c.BLACK) surface.blit(image, (0, 0)) choices = ['Rent a room. (30 Gold)', 'Leave.']

@@ -82,7 +82,25 @@

def make_gold_box(self): """Make the box to display total gold""" - return None + image = setup.GFX['goldbox'] + rect = image.get_rect(bottom=608, right=800) + + surface = pg.Surface(rect.size) + surface.set_colorkey(c.BLACK) + surface.blit(image, (0, 0)) + gold = self.level.game_data['player items']['gold'] + text = 'Gold: ' + str(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_state_dict(self):

@@ -108,6 +126,7 @@

def make_selection(self, keys, current_time): """Control the selection""" self.selection_box = self.make_selection_box() + self.gold_box = self.make_gold_box() if keys[pg.K_DOWN]: self.selection_arrow.rect.topleft = self.arrow_pos2

@@ -116,6 +135,10 @@ 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 + self.level.game_data['last direction'] = 'down' + elif self.selection_arrow.rect.topleft == self.arrow_pos1: + self.level.game_data['player items']['gold'] -= 30 +

@@ -130,10 +153,12 @@ def draw(self, surface):

"""Draw GUI to level surface""" if self.state == 'dialogue': surface.blit(self.dialogue_box.image, self.dialogue_box.rect) + surface.blit(self.gold_box.image, self.gold_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) + surface.blit(self.gold_box.image, self.gold_box.rect)

@@ -144,19 +169,15 @@ super(Shop, self).__init__(name)

self.map_width = 13 self.map_height = 10 - def startup(self, current_time, persist): + def startup(self, current_time, game_data): """Startup state""" - self.persist = persist + self.game_data = game_data 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.gui = Gui('Inn', self.dialogue, self) - - def make_dialogue(self):

@@ -214,6 +235,8 @@ sprite.image = pg.transform.scale2x(sprite.image)

sprite.rect = sprite.image.get_rect(left=550, top=225) return sprite + + def update(self, surface, keys, current_time):
M data/tools.pydata/tools.py

@@ -84,19 +84,19 @@ self.done = False

self.quit = False self.next = None self.previous = None - self.persist = {} + self.game_data = {} self.name = name def get_event(self, event): pass - def startup(self, current_time, persistant): - self.persist = persistant + def startup(self, current_time, game_data): + self.game_data = game_data self.start_time = current_time def cleanup(self): self.done = False - return self.persist + return self.game_data def update(self, surface, keys, current_time): pass

@@ -153,13 +153,16 @@

def create_game_data_dict(): """Create a dictionary of persistant values the player carries between states""" + player_items = {'gold': 0} + + data_dict = {'last location': None, 'last state': None, 'last direction': 'up', 'town start pos': [12, 49], 'castle start pos': [12, 26], - 'king item': {'total': 500, - 'type': 'gold'} + 'king item': {'gold': 500}, + 'player items': player_items } return data_dict