def __init__(self, **kwargs): super().__init__(**kwargs) # List of current objects that we know of self.objects: List[GameObject] = [] # Batches for efficient drawing (each batch can be drawn at once # instead of needing to draw every single object individually) self.main_batch = Batch() self.player_batch = Batch() self.ui_batch = Batch() self.background_batch = Batch() # FPS display in bottom left corner self.fps_display = FPSDisplay(window=self) # Call self.tick() approx 60 times a sec clock.schedule_interval(self.tick, 1 / TPS) # Locate setting directory # This will be somewhere in AppData on windows and ~/.config on linux etc. settings_dir = resource.get_settings_path('WelcomeToHell') # Make sure it exists otherwise create it if not os.path.exists(settings_dir): os.makedirs(settings_dir) self.highscores_filename = os.path.join(settings_dir, "highscores.pickle") # Try loading our highscores or assume we have done if we fail try: with open(self.highscores_filename, "rb") as f: self.highscores = pickle.load(f) # Sort the highscores just to be safe self.highscores.sort(key=lambda highscore: highscore.score, reverse=True) except (OSError, IOError): self.highscores = [] # Vars assigned to later in self.reset self.space = None self.player = None self.level = None self.ui = None self.menu = None
recipes = None smelting_recipes = None # # Timer # TIMER_INTERVAL = 1 main_timer = None # # Global files & directories # game_dir = get_settings_path(APP_NAME) if not os.path.exists(game_dir): os.makedirs(game_dir) config = ConfigParser() config_file = os.path.join(game_dir, 'game.cfg') config.read(config_file) LAUNCH_OPTIONS = argparse.Namespace() ANCHOR_NONE = 0 ANCHOR_LEFT = 1 ANCHOR_TOP = 1 << 1 ANCHOR_RIGHT = 1 << 2 ANCHOR_BOTTOM = 1 << 3 ICONS_PATH = os.path.join('resources', 'textures', 'icons')
class Options(): """Provides loading and saving options to file. Options.options will be a dict of the current options. If no options file is found on disk, the default options will be loaded instead. Add functions to Options.listeners to have them called when options change. This is a static class. """ filename = get_settings_path("g-one") + "/options.p" default_controls = [{ key.UP: key.UP, key.RIGHT: key.RIGHT, key.DOWN: key.DOWN, key.LEFT: key.LEFT, key.SPACE: key.SPACE }, { key.UP: key.W, key.RIGHT: key.D, key.DOWN: key.S, key.LEFT: key.A, key.SPACE: key.LSHIFT }] default_options = { 'fullscreen': False, 'music': 100, 'sound effects': 100, 'controls': default_controls } listeners = [] @staticmethod def load(): """Loads the options from file. Loads default options if there is no file. """ Options.options = Options.options_from_file(Options.filename) if Options.options is None: Options.options = Options.default_options @staticmethod def save(): """Saves the options to file""" Options.options_to_file(Options.filename) @staticmethod def options_from_file(filename): """Load options from filename and return them. None is returned on error. """ try: with open(filename, 'rb') as f: return pickle.load(f) except Exception: return None @staticmethod def options_to_file(filename): """Save the current options to the specified filename""" os.makedirs(os.path.dirname(filename), exist_ok=True) with open(filename, 'wb') as f: pickle.dump(Options.options, f) @staticmethod def changed(): """Call this when the options change""" for listener in Options.listeners: listener()
# Recipes recipes = None smelting_recipes = None # Timer TIMER_INTERVAL = 1 main_timer = None CHAT_FADE_TIME = 8 # Localization LANGUAGE = 'default' _ = lambda x: x # Global files & directories game_dir = get_settings_path(APP_NAME) if not os.path.exists(game_dir): os.makedirs(game_dir) worlds_dir = os.path.join(game_dir, 'worlds') config = ConfigParser() config_file = os.path.join(game_dir, 'game.cfg') config.read(config_file) LAUNCH_OPTIONS = argparse.Namespace() ANCHOR_NONE = 0 ANCHOR_LEFT = 1 ANCHOR_TOP = 1 << 1 ANCHOR_RIGHT = 1 << 2 ANCHOR_BOTTOM = 1 << 3
def test_settings_path_linux_xdg_config_home(self): """Settings path on Linux with XDG_CONFIG_HOME available.""" self.assertEqual(get_settings_path('myapp'), os.path.join('pyglet', 'myapp'))
def test_settings_path_darwin(self): """Settings path on OSX.""" self.assertEqual( get_settings_path('myapp'), os.path.join('pyglet', 'Library', 'Application Support', 'myapp'))
def test_settings_path_windows_no_appdata(self): """Settings path on cygwin without APPDATA set.""" self.assertEqual(get_settings_path('myapp'), os.path.join('pyglet', 'myapp'))
def get_filename(statenum): """Returns the absolute path and filename to a particular savestate""" return get_settings_path("g-one") + "/save_" + str(statenum) + ".p"
from sqlite3 import connect from os import path, makedirs from shutil import copyfile from hashlib import sha512 from typing import Final, final from keyring import set_password, delete_password, set_keyring from keyring.errors import PasswordDeleteError from keyring.backends import Windows from pyglet.resource import get_settings_path set_keyring(Windows.WinVaultKeyring()) # determine if user launches app for the first time, if yes - create game DB USER_DB_LOCATION: Final = get_settings_path("Railway Station Simulator") _user_db_full_path = path.join(USER_DB_LOCATION, 'user.db') if not path.exists(USER_DB_LOCATION): makedirs(USER_DB_LOCATION) if not path.exists(_user_db_full_path): copyfile('db/default.db', _user_db_full_path) try: delete_password(sha512('user_db'.encode('utf-8')).hexdigest(), sha512('user_db'.encode('utf-8')).hexdigest()) except PasswordDeleteError: pass with open(_user_db_full_path, 'rb') as f1: _data = f1.read()[::-1] set_password( sha512('user_db'.encode('utf-8')).hexdigest(), sha512('user_db'.encode('utf-8')).hexdigest(), sha512(_data[::3] + _data[1::3] + _data[2::3]).hexdigest()
# # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # import pickle import os from pyglet.resource import get_settings_path filename = get_settings_path("g-one") + "/highscores.p" def add_highscore(earth, name, difficulty, score): e = 0 if earth else 1 highscores = load_highscores() highscores[e].append((name, difficulty, score)) highscores[e].sort(key=lambda x: x[2], reverse=True) while len(highscores[e]) > 10: del highscores[e][-1] save_highscores(highscores) return highscores def is_highscore(earth, score): e = 0 if earth else 1