Changeset 447:4bec05fed6c7


Ignore:
Timestamp:
04/09/11 18:03:18 (12 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Phase:
public
Message:

Load and save.

Location:
skaapsteker
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/__main__.py

    r262 r447  
    22   """
    33
     4import os
    45import sys
    56import optparse
     
    1617
    1718
     19def _get_default_save_location():
     20    """Return a default save game location."""
     21    app = "ninetales"
     22    if sys.platform.startswith("win") and "APPDATA" in os.environ:
     23        return os.path.join(os.environ["APPDATA"], app)
     24    else:
     25        return os.path.join(os.path.expanduser("~"), ".%s" % app)
     26
     27
    1828def parse_args(args):
    1929    parser = optparse.OptionParser()
     
    2232    parser.add_option("--dvorak", action="store_true", default=False,
    2333            dest="dvorak", help="Dvorak keyboard layout")
     34    parser.add_option("--save-location", action="store", default=_get_default_save_location(),
     35            dest="save_location", help="Saved game location")
    2436    if DEBUG:
    2537        parser.add_option("--level", type="str", default=None,
     
    3042    options['dvorak'] = opts.dvorak
    3143    options['sound'] = opts.sound
     44    options['save_location'] = opts.save_location
    3245    if DEBUG:
    3346        options['debug_rects'] = opts.rects
  • skaapsteker/engine.py

    r383 r447  
    11"""Top-level engine for switching scenes."""
     2
     3import os
    24
    35import pygame.display
     
    57import pygame.event
    68from pygame.locals import QUIT, USEREVENT
     9
     10from . import options
    711
    812
     
    1620        self._fpss = [self._framerate] * 100
    1721        self._cur_frame = 0
    18         self.game_state = GameState("game.json")
     22        self.game_state = GameState(os.path.join(options['save_location'], 'savegame.json'))
     23        if self.game_state.can_resume():
     24            self.game_state.load_game()
     25        else:
     26            self.game_state.new_game()
    1927        self.soundsystem = soundsystem
    2028
    2129    def change_scene(self, next_scene):
     30        self.game_state.save_game()
    2231        self.soundsystem.stop_music()
    2332        if self._current_scene is not None:
  • skaapsteker/gamestate.py

    r248 r447  
     1import os
    12import json
    23
     
    3536class GameState(object):
    3637
    37     def __init__(self, game_file, saved=False):
    38         if saved:
    39             raw_data = open(game_file, "rb").read()
    40         else:
    41             raw_data = data.load(game_file).read()
     38    def __init__(self, game_file):
     39        self._game_file = game_file
     40
     41    def can_resume(self):
     42        return os.path.exists(self._game_file)
     43
     44    def load_game(self, game_file=None):
     45        if game_file is None:
     46            game_file = self._game_file
     47        raw_data = open(game_file, "rb").read()
    4248        self.data = json.loads(raw_data)
    4349        self.world = StateProxy(self.data)
    4450
    45     def save(self, save_game_file):
    46         json.dumps(self.data, open(save_game_file, "wb"))
     51    def new_game(self):
     52        self.load_game(data.filepath("game.json"))
     53        self.save_game()
     54
     55    def save_game(self):
     56        save_dir, _ = os.path.split(self._game_file)
     57        if not os.path.exists(save_dir):
     58            try:
     59                os.makedirs(save_dir)
     60            except:
     61                print "Cannot create save game directory."
     62                return
     63        try:
     64            json.dump(self.data, open(self._game_file, "wb"))
     65        except:
     66            print "Cannot create save game file."
    4767
    4868    def create_sprites(self, level):
     
    5878                    sprites.append(find_sprite(sprite_dict, stype))
    5979        return sprites
     80
     81
Note: See TracChangeset for help on using the changeset viewer.