all repos — Legends-RPG @ ec10a518f3a268b6b589ab4ee9344b1e0e9bef01

A fantasy mini-RPG built with Python and Pygame.

Saving with pickle now seems to work.
Justin Armstrong justinmeister@gmail.com
Sat, 14 Jun 2014 11:29:50 -0700
commit

ec10a518f3a268b6b589ab4ee9344b1e0e9bef01

parent

5bfef920882a049cc06fa2648364428c4e0f1507

M data/constants.pydata/constants.py

@@ -23,6 +23,7 @@ DUNGEON4 = 'dungeon4'

DUNGEON5 = 'dungeon5' INSTRUCTIONS = 'instructions' DEATH_SCENE = 'death scene' +LOADGAME = 'load game' ##Colors
M data/main.pydata/main.py

@@ -23,6 +23,7 @@ DUNGEON4 = 'dungeon4'

DUNGEON5 = 'dungeon5' INSTRUCTIONS = 'instructions' DEATH_SCENE = 'death scene' +LOADGAME = 'load game' def main():

@@ -46,6 +47,7 @@ DUNGEON3: levels.LevelState(DUNGEON3, True),

DUNGEON4: levels.LevelState(DUNGEON4, True), DUNGEON5: levels.LevelState(DUNGEON5, True), INSTRUCTIONS: main_menu.Instructions(), + LOADGAME: main_menu.LoadGame(), DEATH_SCENE: death.DeathScene() }
M data/states/battle.pydata/states/battle.py

@@ -445,6 +445,7 @@

def player_damaged(self, damage): self.game_data['player stats']['health']['current'] -= damage if self.game_data['player stats']['health']['current'] <= 0: + self.game_data['player stats']['health']['current'] = 0 self.state = c.DEATH_FADE def player_healed(self, heal, magic_points=0):
M data/states/death.pydata/states/death.py

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

-import copy, pickle, sys +import copy, pickle, sys, os import pygame as pg from .. import setup, tools from ..components import person

@@ -14,13 +14,13 @@ class Arrow(pg.sprite.Sprite):

""" Arrow to select restart or saved gamed. """ - def __init__(self): + def __init__(self, x, y): super(Arrow, self).__init__() self.image = setup.GFX['smallarrow'] - self.rect = self.image.get_rect(x=300, - y=532) + self.rect = self.image.get_rect(x=x, + y=y) self.index = 0 - self.pos_list = [532, 566] + self.pos_list = [y, y+34] def update(self, keys): """

@@ -53,7 +53,7 @@ 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.message_box = self.make_message_box() - self.arrow = Arrow() + self.arrow = Arrow(300, 532) self.state_dict = self.make_state_dict() self.state = c.TRANSITION_IN self.alpha = 255

@@ -61,6 +61,9 @@ self.name = c.DEATH_SCENE

self.transition_surface = pg.Surface(setup.SCREEN_RECT.size) self.transition_surface.fill(c.BLACK_BLUE) self.transition_surface.set_alpha(self.alpha) + if not os.path.isfile("save.p"): + game_data = tools.create_game_data_dict() + pickle.dump(game_data, open("save.p", "wb")) def make_message_box(self): """

@@ -130,7 +133,6 @@ """

self.transition_surface.set_alpha(self.alpha) self.alpha += c.TRANSITION_SPEED if self.alpha >= 255: - self.game_data['last state'] = self.name self.done = True def normal_update(self, keys):

@@ -144,7 +146,8 @@ or just start from the beginning of the game.

""" if keys[pg.K_SPACE]: if self.arrow.index == 0: - self.next = c.MAIN_MENU + self.next = c.TOWN + self.game_data = pickle.load(open("save.p", "rb")) elif self.arrow.index == 1: self.next = c.MAIN_MENU self.state = c.TRANSITION_OUT
M data/states/levels.pydata/states/levels.py

@@ -6,7 +6,6 @@ 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

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

#Python 2/3 compatibility. if sys.version_info[0] == 2: - import cPickle - pickle = cPickle range = xrange

@@ -112,8 +109,7 @@ def make_player(self):

""" Make the player and sets location. """ - last_state = self.game_data['last state'] - + last_state = self.previous if last_state == 'battle': player = person.Player(self.game_data['last direction'], self.game_data)
M data/states/main_menu.pydata/states/main_menu.py

@@ -1,11 +1,19 @@

+import pickle, sys, os import pygame as pg from .. import setup, tools, tilerender from .. import constants as c +import death + + +#Python 2/3 compatibility. +if sys.version_info[0] == 2: + import cPickle + pickle = cPickle + class Menu(tools._State): def __init__(self): super(Menu, self).__init__() - self.game_data = tools.create_game_data_dict() self.music = setup.MUSIC['kings_theme'] self.volume = 0.4 self.next = c.INSTRUCTIONS

@@ -86,7 +94,6 @@ """

self.transition_surface.set_alpha(self.alpha) self.alpha += c.TRANSITION_SPEED if self.alpha >= 255: - self.game_data['last state'] = self.name self.done = True def normal_update(self):

@@ -99,21 +106,21 @@ Instructions page.

""" def __init__(self): super(Instructions, self).__init__() - self.next = c.OVERWORLD self.tmx_map = setup.TMX['title'] self.music = None - + def startup(self, *args): self.renderer = tilerender.Renderer(self.tmx_map) self.map_image = self.renderer.make_2x_map() self.map_rect = self.map_image.get_rect() self.viewport = self.make_viewport(self.map_image) self.level_surface = pg.Surface(self.map_rect.size) - self.title_box = setup.GFX['instructions_box'] + self.title_box = self.set_image() self.title_rect = self.title_box.get_rect() self.title_rect.midbottom = self.viewport.midbottom self.title_rect.y -= 30 self.game_data = tools.create_game_data_dict() + self.next = self.set_next_scene() self.state_dict = self.make_state_dict() self.name = c.MAIN_MENU self.state = c.TRANSITION_IN

@@ -122,6 +129,24 @@ self.transition_surface = pg.Surface(setup.SCREEN_RECT.size)

self.transition_surface.fill(c.BLACK_BLUE) self.transition_surface.set_alpha(self.alpha) + def set_next_scene(self): + """ + Check if there is a saved game. If not, start + game at begining. Otherwise go to load game scene. + """ + if not os.path.isfile("save.p"): + next_scene = c.OVERWORLD + else: + next_scene = c.LOADGAME + + return next_scene + + def set_image(self): + """ + Set image for message box. + """ + return setup.GFX['instructions_box'] + def make_viewport(self, map_image): """ Create the viewport to view the level through.

@@ -139,12 +164,12 @@ c.NORMAL: self.normal_update}

return state_dict - def update(self, surface, *args): + def update(self, surface, keys, *args): """ Update scene. """ update_level = self.state_dict[self.state] - update_level() + update_level(keys) self.draw_level(surface) def draw_level(self, surface):

@@ -153,14 +178,18 @@ Blit tmx map and title box onto screen.

""" self.level_surface.blit(self.map_image, self.viewport, self.viewport) self.level_surface.blit(self.title_box, self.title_rect) + self.draw_arrow() surface.blit(self.level_surface, (0,0), self.viewport) surface.blit(self.transition_surface, (0,0)) + + def draw_arrow(self): + pass def get_event(self, event): if event.type == pg.KEYDOWN: self.state = c.TRANSITION_OUT - def transition_in(self): + def transition_in(self, *args): """ Transition into scene with a fade. """

@@ -169,19 +198,52 @@ self.alpha -= c.TRANSITION_SPEED

if self.alpha <= 0: self.alpha = 0 self.state = c.NORMAL - - def transition_out(self): + def transition_out(self, *args): """ 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['last state'] = self.name self.done = True - def normal_update(self): + def normal_update(self, *args): + pass + + +class LoadGame(Instructions): + def __init__(self): + super(LoadGame, self).__init__() + self.arrow = death.Arrow(200, 260) + self.arrow.pos_list[1] += 34 + + def set_image(self): + """ + Set image for message box. + """ + return setup.GFX['loadgamebox'] + + def draw_arrow(self): + self.level_surface.blit(self.arrow.image, self.arrow.rect) + + def get_event(self, event): pass + + def normal_update(self, keys): + if keys[pg.K_DOWN]: + self.arrow.index = 1 + elif keys[pg.K_UP]: + self.arrow.index = 0 + elif keys[pg.K_SPACE]: + if self.arrow.index == 0: + self.game_data = pickle.load(open("save.p", "rb")) + self.next = c.TOWN + self.state = c.TRANSITION_OUT + else: + self.next = c.OVERWORLD + self.state = c.TRANSITION_OUT + + self.arrow.rect.y = self.arrow.pos_list[self.arrow.index]
M resources/tmx/overworld.tmxresources/tmx/overworld.tmx

@@ -161,7 +161,7 @@ </object>

<object name="start point" gid="123" x="368" y="560"> <properties> <property name="direction" value="down"/> - <property name="state" value="main menu"/> + <property name="state" value="instructions"/> </properties> </object> <object name="portal" type="brotherhouse" gid="139" x="400" y="144"/>

@@ -193,5 +193,11 @@ <object name="blocker" gid="120" x="400" y="528"/>

<object name="blocker" gid="120" x="400" y="544"/> <object name="blocker" gid="120" x="400" y="560"/> <object name="blocker" gid="120" x="400" y="576"/> + <object name="start point" gid="123" x="368" y="560"> + <properties> + <property name="direction" value="down"/> + <property name="state" value="load game"/> + </properties> + </object> </objectgroup> </map>
M resources/tmx/town.tmxresources/tmx/town.tmx

@@ -416,5 +416,17 @@ <object name="blocker" gid="120" x="144" y="784"/>

<object name="blocker" gid="120" x="128" y="784"/> <object name="blocker" gid="120" x="112" y="768"/> <object name="blocker" gid="120" x="112" y="752"/> + <object name="start point" gid="123" x="320" y="688"> + <properties> + <property name="direction" value="down"/> + <property name="state" value="death scene"/> + </properties> + </object> + <object name="start point" gid="123" x="320" y="688"> + <properties> + <property name="direction" value="down"/> + <property name="state" value="load game"/> + </properties> + </object> </objectgroup> </map>