|
| 1 | +import json |
| 2 | +import os |
| 3 | +import pygame |
| 4 | + |
| 5 | +DEFAULT_SETTINGS = { |
| 6 | + "resolution": [0, 0], |
| 7 | + "fullscreen": True, |
| 8 | + "weather": "sunny", |
| 9 | + "theme": "default", # New: theme option |
| 10 | + "controls": { |
| 11 | + "up": pygame.K_UP, |
| 12 | + "down": pygame.K_DOWN, |
| 13 | + "left": pygame.K_LEFT, |
| 14 | + "right": pygame.K_RIGHT, |
| 15 | + "pit_stop": pygame.K_p, |
| 16 | + "drs": pygame.K_d |
| 17 | + }, |
| 18 | + "music_volume": 0.5, |
| 19 | + "sfx_volume": 0.5, |
| 20 | + "language": "en", |
| 21 | + "ai_difficulty": "medium", |
| 22 | + "minimap_enabled": True, |
| 23 | + "default_track": "Monaco", |
| 24 | + "default_opponents": 1, |
| 25 | + "default_laps": 3, |
| 26 | + "car_type": "balanced", |
| 27 | + "car_collisions": True, |
| 28 | + "fuel_consumption": "medium", |
| 29 | + "practice_enabled": True, |
| 30 | + "practice_duration": 3, |
| 31 | + "qualifying_enabled": True, |
| 32 | + "qualifying_duration": 2 |
| 33 | +} |
| 34 | + |
| 35 | +def load_game(): |
| 36 | + home = os.path.expanduser("~") |
| 37 | + save_dir = os.path.join(home, '.f1game', 'saves') |
| 38 | + if not os.path.exists(save_dir): |
| 39 | + os.makedirs(save_dir) |
| 40 | + save_file = os.path.join(save_dir, 'save.json') |
| 41 | + if os.path.exists(save_file): |
| 42 | + with open(save_file, 'r') as f: |
| 43 | + data = json.load(f) |
| 44 | + settings = data.get('settings', DEFAULT_SETTINGS) |
| 45 | + for key, value in DEFAULT_SETTINGS.items(): |
| 46 | + if key not in settings: |
| 47 | + settings[key] = value |
| 48 | + elif key == "controls" and isinstance(settings[key], dict): |
| 49 | + for control_key, control_value in DEFAULT_SETTINGS["controls"].items(): |
| 50 | + if control_key not in settings["controls"]: |
| 51 | + settings["controls"][control_key] = control_value |
| 52 | + return ( |
| 53 | + data.get('high_score', 0), |
| 54 | + data.get('championship_standings', {}), |
| 55 | + settings |
| 56 | + ) |
| 57 | + return 0, {}, DEFAULT_SETTINGS |
| 58 | + |
| 59 | +def save_game(high_score, championship_standings, settings): |
| 60 | + home = os.path.expanduser("~") |
| 61 | + save_dir = os.path.join(home, '.f1game', 'saves') |
| 62 | + if not os.path.exists(save_dir): |
| 63 | + os.makedirs(save_dir) |
| 64 | + save_file = os.path.join(save_dir, 'save.json') |
| 65 | + data = { |
| 66 | + 'high_score': high_score, |
| 67 | + 'championship_standings': championship_standings, |
| 68 | + 'settings': settings |
| 69 | + } |
| 70 | + with open(save_file, 'w') as f: |
| 71 | + json.dump(data, f) |
| 72 | + |
| 73 | +def save_game_state(game_state): |
| 74 | + home = os.path.expanduser("~") |
| 75 | + save_dir = os.path.join(home, '.f1game', 'saves') |
| 76 | + if not os.path.exists(save_dir): |
| 77 | + os.makedirs(save_dir) |
| 78 | + save_file = os.path.join(save_dir, 'game_state.json') |
| 79 | + with open(save_file, 'w') as f: |
| 80 | + json.dump(game_state, f) |
| 81 | + |
| 82 | +def load_game_state(): |
| 83 | + home = os.path.expanduser("~") |
| 84 | + save_dir = os.path.join(home, '.f1game', 'saves') |
| 85 | + save_file = os.path.join(save_dir, 'game_state.json') |
| 86 | + if os.path.exists(save_file): |
| 87 | + with open(save_file, 'r') as f: |
| 88 | + return json.load(f) |
| 89 | + return None |
0 commit comments