all repos — Legends-RPG @ b2a05726a71ed7ced758c45885cf792002b0ff51

A fantasy mini-RPG built with Python and Pygame.

Made a Death Scene, began using pickle to store saved game data.
Justin Armstrong justinmeister@gmail.com
Wed, 04 Jun 2014 10:25:41 -0700
commit

b2a05726a71ed7ced758c45885cf792002b0ff51

parent

680235e09a98538dcc6f7165b3f8186889ed6ba5

M data/constants.pydata/constants.py

@@ -22,6 +22,7 @@ DUNGEON3 = 'dungeon3'

DUNGEON4 = 'dungeon4' DUNGEON5 = 'dungeon5' INSTRUCTIONS = 'instructions' +DEATH_SCENE = 'death scene' ##Colors

@@ -68,6 +69,7 @@ SHOW_EXPERIENCE = 'show experience'

LEVEL_UP = 'level up' TWO_ACTIONS = 'two actions' SHOW_GOLD = 'show gold' +DEATH_FADE = 'death fade' #EVENTS

@@ -85,6 +87,7 @@ POWERUP = 'powerup'

TALK = 'talk' TRANSITION_SPEED = 35 +DEATH_TRANSITION_SPEED = 5 #LEVEL STATES
M data/main.pydata/main.py

@@ -1,4 +1,4 @@

-from data.states import shop, levels, battle, main_menu +from data.states import shop, levels, battle, main_menu, death from . import setup, tools from . import constants as c

@@ -22,6 +22,7 @@ DUNGEON3 = 'dungeon3'

DUNGEON4 = 'dungeon4' DUNGEON5 = 'dungeon5' INSTRUCTIONS = 'instructions' +DEATH_SCENE = 'death scene' def main():

@@ -44,7 +45,8 @@ DUNGEON2: levels.LevelState(DUNGEON2, True),

DUNGEON3: levels.LevelState(DUNGEON3, True), DUNGEON4: levels.LevelState(DUNGEON4, True), DUNGEON5: levels.LevelState(DUNGEON5, True), - INSTRUCTIONS: main_menu.Instructions() + INSTRUCTIONS: main_menu.Instructions(), + DEATH_SCENE: death.DeathScene() } run_it.setup_states(state_dict, c.MAIN_MENU)
M data/shopgui.pydata/shopgui.py

@@ -2,11 +2,18 @@ """

This class controls the textbox GUI for any shop state. A Gui object is created and updated by the shop state. """ - +import sys +import pickle import pygame as pg from . import setup, observer from . components import textbox from . import constants as c + + +#Python 2/3 compatibility. +if sys.version_info[0] == 2: + import cPickle + pickle = cPickle class Gui(object):

@@ -337,6 +344,7 @@ player_items[item_type] = item_to_add

elif item_type == 'room': player_health['current'] = player_health['maximum'] player_magic['current'] = player_magic['maximum'] + pickle.dump(self.game_data, open( "save.p", "wb")) def confirm_sell(self, keys, current_time): """
M data/states/battle.pydata/states/battle.py

@@ -432,8 +432,20 @@ self.transition_alpha += c.TRANSITION_SPEED

if self.transition_alpha >= 255: self.done = True + elif self.state == c.DEATH_FADE: + transition_image = pg.Surface(self.transition_rect.size) + transition_image.fill(c.TRANSITION_COLOR) + transition_image.set_alpha(self.transition_alpha) + surface.blit(transition_image, self.transition_rect) + self.transition_alpha += c.DEATH_TRANSITION_SPEED + if self.transition_alpha >= 255: + self.done = True + self.next = c.DEATH_SCENE + def player_damaged(self, damage): self.game_data['player stats']['health']['current'] -= damage + if self.game_data['player stats']['health']['current'] <= 0: + self.state = c.DEATH_FADE def player_healed(self, heal, magic_points=0): """
A data/states/death.py

@@ -0,0 +1,91 @@

+import copy, pickle, sys +import pygame as pg +from .. import setup, tools +from ..components import person +from .. import constants as c + +#Python 2/3 compatibility. +if sys.version_info[0] == 2: + import cPickle + pickle = cPickle + + +class DeathScene(tools._State): + """ + Scene when the player has died. + """ + def __init__(self): + super(DeathScene, self).__init__() + self.next = c.TOWN + self.music = None + + def startup(self, current_time, game_data): + self.game_data = game_data + self.background = pg.Surface(setup.SCREEN_RECT.size) + self.background.fill(c.BLACK_BLUE) + self.player = person.Player('down', self.game_data, 1, 1, 'resting', 1) + self.player.image = pg.transform.scale2x(self.player.image) + self.player.rect = self.player.image.get_rect() + self.player.rect.center = setup.SCREEN_RECT.center + self.state_dict = self.make_state_dict() + self.state = c.TRANSITION_IN + self.alpha = 255 + self.name = c.DEATH_SCENE + self.transition_surface = copy.copy(self.background) + self.transition_surface.fill(c.BLACK_BLUE) + self.transition_surface.set_alpha(self.alpha) + + def make_state_dict(self): + """ + Make the dicitonary of state methods for the scene. + """ + state_dict = {c.TRANSITION_IN: self.transition_in, + c.TRANSITION_OUT: self.transition_out, + c.NORMAL: self.normal_update} + + return state_dict + + def update(self, surface, *args): + """ + Update scene. + """ + update_level = self.state_dict[self.state] + update_level() + self.draw_level(surface) + + def transition_in(self): + """ + Transition into scene with a fade. + """ + self.transition_surface.set_alpha(self.alpha) + self.alpha -= c.TRANSITION_SPEED + if self.alpha <= 0: + self.alpha = 0 + self.state = c.NORMAL + + def transition_out(self): + """ + Transition out of scene with a fade. + """ + self.transition_surface.set_alpha(self.alpha) + self.alpha += c.TRANSITION_SPEED + if self.alpha >= 255: + self.game_data = pickle.load(open("save.p", "rb")) + self.game_data['last state'] = self.name + self.done = True + + def normal_update(self): + pass + + def draw_level(self, surface): + """ + Draw background, player, and message box. + """ + surface.blit(self.background, (0, 0)) + surface.blit(self.player.image, self.player.rect) + + + + + +
M data/states/levels.pydata/states/levels.py

@@ -6,6 +6,7 @@ This class inherits from the generic state class

found in the tools.py module. """ import copy, sys +import pickle import pygame as pg from .. import tools, collision from .. import constants as c

@@ -17,6 +18,8 @@

#Python 2/3 compatibility. if sys.version_info[0] == 2: + import cPickle + pickle = cPickle range = xrange
M data/tools.pydata/tools.py

@@ -200,7 +200,7 @@ ('power', 9)]),

'equipped weapon': 'Rapier', 'equipped armor': []} - player_health = {'current': 70, + player_health = {'current': 10, 'maximum': 70} player_magic = {'current': 70,