Saving with pickle now seems to work.
Justin Armstrong justinmeister@gmail.com
Sat, 14 Jun 2014 11:29:50 -0700
9 files changed,
109 insertions(+),
26 deletions(-)
M
data/constants.py
→
data/constants.py
@@ -23,6 +23,7 @@ DUNGEON4 = 'dungeon4'
DUNGEON5 = 'dungeon5' INSTRUCTIONS = 'instructions' DEATH_SCENE = 'death scene' +LOADGAME = 'load game' ##Colors
M
data/main.py
→
data/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.py
→
data/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.py
→
data/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.py
→
data/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
resources/tmx/overworld.tmx
→
resources/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.tmx
→
resources/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>