forked from davidpendergast/pygame-summer-team-jam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.py
229 lines (193 loc) · 7.98 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
import pygame
import json
import pathlib
import traceback
import platform
class Display:
fps = 60
width = 960
height = 540
title = "TEMPEST RUN"
camera_bob = True
use_player_art = True
depth_shade = False
class FontSize:
# These are normalized to a display height of 540 px
title = 64
option = 36
info = 24
score = 30
class Music:
enabled = True
volume = 0.4
class Sound:
enabled = True
volume = 0.2
class Debug:
use_neon = True
fps_test = False
jumping_enemies = False
flag = False
class KeyBinds:
class Game:
jump = [pygame.K_w, pygame.K_UP, pygame.K_SPACE]
left = [pygame.K_a, pygame.K_LEFT]
right = [pygame.K_d, pygame.K_RIGHT]
slide = [pygame.K_s, pygame.K_DOWN]
reset = [pygame.K_r]
class Menu:
up = [pygame.K_w, pygame.K_UP]
down = [pygame.K_s, pygame.K_DOWN]
right = [pygame.K_d, pygame.K_RIGHT]
left = [pygame.K_a, pygame.K_LEFT]
accept = [pygame.K_RETURN, pygame.K_SPACE]
cancel = [pygame.K_ESCAPE, pygame.K_AC_BACK]
class Toogle:
neon = [pygame.K_n]
profiler = [pygame.K_F1]
fps = [pygame.K_F2]
flag = [pygame.K_F3]
class Platform:
IS_ANDROID = "ANDROID_ARGUMENT" in os.environ
IS_WINDOWS = platform.system() == "Windows"
IS_LINUX = platform.system() == "Linux"
IS_MAC = platform.system() == "Darwin"
if Platform.IS_ANDROID:
info = pygame.display.Info()
Display.width, Display.height = info.current_w, info.current_h
_default_configs = {
"Display": {
"fps": 60,
"width": Display.width,
"height": Display.height,
"title": "TEMPEST RUN",
"camera_bob": True,
"use_player_art": True,
"depth_shade": False,
},
"FontSize": {"title": 64, "option": 36, "info": 24, "score": 30},
"Music": {"enabled": True, "volume": 0.5},
"Sound": {"enabled": True, "volume": 0.5},
"Debug": {"use_neon": True, "fps_test": False, "jumping_enemies": False},
"KeyBinds": {
"Game": {
"jump": [pygame.K_w, pygame.K_UP, pygame.K_SPACE],
"left": [pygame.K_a, pygame.K_LEFT],
"right": [pygame.K_d, pygame.K_RIGHT],
"slide": [pygame.K_s, pygame.K_DOWN],
"reset": [pygame.K_r],
},
"Menu": {
"up": [pygame.K_w, pygame.K_UP],
"down": [pygame.K_s, pygame.K_DOWN],
"right": [pygame.K_d, pygame.K_RIGHT],
"left": [pygame.K_a, pygame.K_LEFT],
"accept": [pygame.K_RETURN, pygame.K_SPACE],
"cancel": [pygame.K_ESCAPE, pygame.K_AC_BACK],
},
"Toogle": {
"neon": [pygame.K_n],
"profiler": [pygame.K_F1],
"fps": [pygame.K_F2],
},
},
# No need to "configure" the OS, it should be detected by default
# "Platform": {
# "IS_ANDROID": Platform.IS_ANDROID,
# "IS_WINDOWS": platform.system() == "Windows",
# "IS_LINUX": platform.system() == "Linux",
# "IS_MAC": platform.system() == "Darwin",
# }
}
def _apply_configs_from_json(configuration):
Display.fps = configuration["Display"]["fps"]
Display.width = configuration["Display"]["width"]
Display.height = configuration["Display"]["height"]
Display.title = configuration["Display"]["title"]
Display.camera_bob = configuration["Display"]["camera_bob"]
Display.use_player_art = configuration["Display"]["use_player_art"]
Display.depth_shade = configuration["Display"]["depth_shade"]
FontSize.title = configuration["FontSize"]["title"]
FontSize.option = configuration["FontSize"]["option"]
FontSize.info = configuration["FontSize"]["info"]
FontSize.score = configuration["FontSize"]["score"]
Music.enabled = configuration["Music"]["enabled"]
Music.volume = configuration["Music"]["volume"]
Sound.enabled = configuration["Sound"]["enabled"]
Sound.volume = configuration["Sound"]["volume"]
Debug.use_neon = configuration["Debug"]["use_neon"]
Debug.fps_test = configuration["Debug"]["fps_test"]
Debug.jumping_enemies = configuration["Debug"]["jumping_enemies"]
KeyBinds.Game.jump = configuration["KeyBinds"]["Game"]["jump"]
KeyBinds.Game.left = configuration["KeyBinds"]["Game"]["left"]
KeyBinds.Game.right = configuration["KeyBinds"]["Game"]["right"]
KeyBinds.Game.slide = configuration["KeyBinds"]["Game"]["slide"]
KeyBinds.Game.reset = configuration["KeyBinds"]["Game"]["reset"]
KeyBinds.Menu.up = configuration["KeyBinds"]["Menu"]["up"]
KeyBinds.Menu.down = configuration["KeyBinds"]["Menu"]["down"]
KeyBinds.Menu.right = configuration["KeyBinds"]["Menu"]["right"]
KeyBinds.Menu.left = configuration["KeyBinds"]["Menu"]["left"]
KeyBinds.Menu.accept = configuration["KeyBinds"]["Menu"]["accept"]
KeyBinds.Menu.cancel = configuration["KeyBinds"]["Menu"]["cancel"]
KeyBinds.Toogle.neon = configuration["KeyBinds"]["Toogle"]["neon"]
KeyBinds.Toogle.profiler = configuration["KeyBinds"]["Toogle"]["profiler"]
KeyBinds.Toogle.fps = configuration["KeyBinds"]["Toogle"]["fps"]
_apply_configs_from_json(_default_configs) # initialize configs to defaults
def _get_configs_as_json_dict():
configuration = json.loads(json.dumps(_default_configs)) # making a new copy
configuration["Display"]["fps"] = Display.fps
configuration["Display"]["width"] = Display.width
configuration["Display"]["height"] = Display.height
configuration["Display"]["title"] = Display.title
configuration["Display"]["camera_bob"] = Display.camera_bob
configuration["Display"]["use_player_art"] = Display.use_player_art
configuration["Display"]["depth_shade"] = Display.depth_shade
configuration["FontSize"]["title"] = FontSize.title
configuration["FontSize"]["option"] = FontSize.option
configuration["FontSize"]["info"] = FontSize.info
configuration["FontSize"]["score"] = FontSize.score
configuration["Music"]["enabled"] = Music.enabled
configuration["Music"]["volume"] = Music.volume
configuration["Sound"]["enabled"] = Sound.enabled
configuration["Sound"]["volume"] = Sound.volume
configuration["Debug"]["use_neon"] = Debug.use_neon
configuration["Debug"]["fps_test"] = Debug.fps_test
configuration["Debug"]["jumping_enemies"] = Debug.jumping_enemies
configuration["KeyBinds"]["Game"]["jump"] = KeyBinds.Game.jump
configuration["KeyBinds"]["Game"]["left"] = KeyBinds.Game.left
configuration["KeyBinds"]["Game"]["right"] = KeyBinds.Game.right
configuration["KeyBinds"]["Game"]["slide"] = KeyBinds.Game.slide
configuration["KeyBinds"]["Game"]["reset"] = KeyBinds.Game.reset
configuration["KeyBinds"]["Menu"]["up"] = KeyBinds.Menu.up
configuration["KeyBinds"]["Menu"]["down"] = KeyBinds.Menu.down
configuration["KeyBinds"]["Menu"]["accept"] = KeyBinds.Menu.accept
configuration["KeyBinds"]["Menu"]["cancel"] = KeyBinds.Menu.cancel
configuration["KeyBinds"]["Toogle"]["neon"] = KeyBinds.Toogle.neon
configuration["KeyBinds"]["Toogle"]["profiler"] = KeyBinds.Toogle.profiler
return configuration
def get_config_path():
return pathlib.Path("config.json")
def load_configs_from_disk():
_apply_configs_from_json(_default_configs)
path = get_config_path()
try:
if path.exists():
with open(path, "r") as f:
_apply_configs_from_json(json.load(f))
print("INFO: loaded configs from: {}".format(path))
except Exception:
print("ERROR: failed to load configs from: {}".format(path))
traceback.print_exc()
def save_configs_to_disk():
path = pathlib.Path("config.json")
try:
if not path.exists():
path.touch()
as_json_dict = _get_configs_as_json_dict()
with open(path, "w") as f:
json.dump(as_json_dict, f, indent=" ")
print("INFO: saved configs to: {}".format(path))
except Exception:
print("ERROR: failed to save configs to: {}".format(path))
traceback.print_exc()