Skip to content

Instantly share code, notes, and snippets.

@Pinacolada64
Created September 18, 2021 03:48
Show Gist options
  • Save Pinacolada64/25a903431081d5aebb19ff72970bd098 to your computer and use it in GitHub Desktop.
Save Pinacolada64/25a903431081d5aebb19ff72970bd098 to your computer and use it in GitHub Desktop.
Learning how to access dicts int he context of an adventure game
class Player:
def __init__(self, name, player_id, stats, silver):
self.name = name
self.player_id = player_id
self.stats = {"constitution": 0, "dexterity": 0, "ego": 0, "intelligence": 0,
"strength": 0, "wisdom": 0} # creates a new stats dict for each Player
self.silver = {"in_hand": 0, "in_bank": 0, "in_bar": 0} # creates a new silver dict for each Player
# test that it works
print("Silver in hand: " + str(self.silver["in_hand"]))
# I think these functions should be outside the class so other code can access it?
def set_stat(self, player, stat, adjustment):
"""
:param self:
:param player: player object
:param stat: statistic to adjust
:param adjustment: adjustment (+x or -x)
:return:
"""
if stat not in player.stats:
print(f"Stat '{stat}' doesn't exist.")
return
# self.stats(constitution, dexterity, ego, intelligence, strength, wisdom):
# dict({'constitution': 0, 'dexterity': 0, 'ego': 0, 'intelligence': 0, 'strength': 0, 'wisdom': 0})
# adjust stat by <adjustment>:
x = player.stats[stat]
print(f"Before: {x}")
x += adjustment
print(f"After: {x}")
player.stats(stat, x)
return player.stats(stat, x)
def get_stat(self, player, stat):
return player.stats[stat]
def print_stat(self, player, stat):
print(f"stat: {get_stat(player, stat)}")
def get_silver(self, player, kind):
"""
get amount of silver player_name has
'kind' is 'in_hand', 'in_bank', or 'in_bar
"""
# if kind not in player_name.silver:
# print(f"Bad kind '{kind}'.")
# return
# return player.silver{kind}
print(f"player.silver[kind]")
return
def set_silver(self, player, kind, adjustment):
x = player.silver[kind]
x = x + adjustment
player.silver(kind, x)
# some (not all) other stats:
# times_played, last_play_date, weapon_percentage, allies,
# ally_positions, horse_name, autoduel_mode, hourglass_mode
# age, sex, constitution, dexterity, ego, intelligence, strength, wisdom,
# guild, player_class, race, expert_mode, more_mode,
# gold_in_hand, gold_in_bank, gold_in_bar, bad_hombre_rating
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
# print_hi('PyCharm')
Rulan = Player(name="Rulan", player_id=1, stats={'intelligence': 5},
silver={'in_hand': 100, 'in_bank': 200, 'in_bar': 300})
print(Rulan.stats)
set_stat(Rulan, "intelligence", 4) # set Rulan's Int to 4
set_silver(Rulan, 'in_hand', 30)
print(get_stat(Rulan, 'int')) # show Int score: should be 4
print(f"Silver in bank: {get_silver(Rulan, 'in_bank')}") # should print 200
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment