def from_json(self, raw):
'''
Load the object from JSON loaded from config file
'''
# First pass make sure all are valid
for key in raw:
if isinstance(raw[key], str):
if not hasattr(pygame, raw[key]):
raise ConfigError(
"Invalid control for {}. Must be a valid pygame key constant".format(key)
)
else:
raw[key] = getattr(pygame, raw[key])
elif isinstance(raw[key], int):
pass
else:
raise ConfigError(
"Controls must be a valid pygame key constant as a string or integer"
)
# Check for duplicates
values = list(raw.values())
values_set = set(values)
if len(values) != len(values_set):
raise ConfigError("Cannot have duplicate controls")
self.up = raw.get('up', getattr(pygame, 'K_w'))
self.down = raw.get('down', getattr(pygame, 'K_s'))
self.left = raw.get('left', getattr(pygame, 'K_a'))
self.right = raw.get('right', getattr(pygame, 'K_d'))
self.jump = raw.get('jump', getattr(pygame, 'K_SPACE'))
self.interact = raw.get('interact', getattr(pygame, 'K_e'))
self.push = raw.get("push", getattr(pygame, "K_LSHIFT"))
self.reset_code = raw.get("reset_code", getattr(pygame, "K_q"))
self.toggle_sound = raw.get("toggle_sound", getattr(pygame, "K_m"))
self.kill_self = raw.get("kill_self", getattr(pygame, "K_k"))
self.reset_game = raw.get("reset_game", getattr(pygame, "K_n"))
评论列表
文章目录