Skip to content

Instantly share code, notes, and snippets.

@alexvorndran
Created June 23, 2019 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexvorndran/48206dae7d6ba325e4c55f8281da0900 to your computer and use it in GitHub Desktop.
Save alexvorndran/48206dae7d6ba325e4c55f8281da0900 to your computer and use it in GitHub Desktop.
"""
See answer to "Story-based adventure with functions and relationships"
on Code Review (https://codereview.stackexchange.com/a/222737/).
Original code (C) 2019 Mikey Akamihe (CC-BY-SA) at
https://codereview.stackexchange.com/q/222720/
"""
import sys
import time
MSG_SYNOPSIS = '''13 December 2027. A year into the zombie apocalypse, you are the young leader of a small, demoralized group in the middle of nowhere, fighting for a chance to see
light at the end of the tunnel. By September of next year, your group has grown greatly but that does not mean that your community on the brink of collapse. You must the make
tough political decisions to determine how your community fares.'''
MSG_WINTER = '''29 January 2029. It is five weeks into winter and the season shows no mercy. A drought happened for a majority of the last fall and it devastated
the food supply. As your community dives deeper into the winter, you realize that your supply will run out if consumption is not altered. You could do one of two options: reduce
consumption among civilians, or ignore the risk and take a chance ([ALTER SUPPLY]X} {B[IGNORE RISK]).'''
MSG_WINTER_ALTER_SUPPLY = '''Your government is now seen as selfish. You took the risk to protect the important people and "do your best with the rest". You have suffered heavy
civilian losses but your army and government losses have been few. As a result, there is division and danger in the streets. Riots breaking out, murders, arson, all happening in
your community. '''
MSG_WINTER_IGNORE_RISK = '''Your community did better than expected during the period. That is until you ran out of food in early March. Now you rely solely on scavenging,
risking getting devoured by zombies in order to go another day. Half your community is either dead or lost with great amount of casualties from civilians and
non-civilians. '''
def long_load():
print('Loading', end='', flush=True)
for _ in range(5):
time.sleep(1)
print('.', end='', flush=True)
print()
def short_load():
print('Loading', end='', flush=True)
for _ in range(3):
time.sleep(1)
print('.', end='', flush=True)
class Factions:
"""Enum to represent the factions found in the game"""
ARMY = "ARMY & GOVERNMENT"
CIVILIANS = "CIVILIANS"
EVERYBODY = "EVERYBODY"
ULTIMATE_SCORE_CHANGE = 15
MAJOR_SCORE_CHANGE = 2
NORMAL_SCORE_CHANGE = 1
SLIGHT_SCORE_CHANGE = 0.5
class RelationshipChanges:
"""Holds templates and modifiers to decribe changes in the relationships"""
HEORISM = {
'message': '{} looks at you as a hero.',
'modifier': ULTIMATE_SCORE_CHANGE
}
GREAT_INCREASE = {
'message': 'This greatly improves your relationship with {}.',
'modifier': MAJOR_SCORE_CHANGE
}
INCREASE = {
'message': 'This improves your relationship with {}.',
'modifier': NORMAL_SCORE_CHANGE
}
SLIGHT_INCREASE = {
'message': 'This slightly improves your relationship with {}.',
'modifier': SLIGHT_SCORE_CHANGE
}
SLIGHT_DECREASE = {
'message': 'This slightly decreases your relationship with {}.',
'modifier': -SLIGHT_SCORE_CHANGE
}
DECREASE = {
'message': 'This worsens your relationship with {}.',
'modifier': -NORMAL_SCORE_CHANGE
}
GREAT_DECREASE = {
'message': 'This greatly worsens your relationship with {}.',
'modifier': -MAJOR_SCORE_CHANGE
}
TREASON = {
'message': '{} wants you dead.',
'modifier': -ULTIMATE_SCORE_CHANGE
}
def change_relation(relationships, faction, type_of_change):
"""Change the standing of the player with a given faction
faction and type_of_change are case insensitive and have to correspond to
class variables of Factions and RelationshipChanges. type_of_change
describes by how much the relationship score is altered.
This function returns a message that describes the change.
"""
type_translation = {
"---": "great_decrease", "--": "decrease", "-": "slight_decrease",
"+++": "great_increase", "++": "increase", "+": "slight_increase"
}
if type_of_change in type_translation:
# only apply the translation if it's own of ---/--/.../+++
type_of_change = type_translation[type_of_change]
change_descr = getattr(RelationshipChanges, type_of_change.upper())
faction_name = getattr(Factions, faction.upper())
relationships[faction_name] += change_descr['modifier']
return change_descr['message'].format(faction_name)
class RelationshipLevels:
"""Class to represent the player's relationship to other factions"""
VENGEFUL = "VENGEFUL"
HATEFUL = "HATEFUL"
DISAPPOINTED = "DISAPPOINTED"
NEUTRAL = "CONFLICTED/NEUTRAL"
SATISFIED = "SATISFIED"
HAPPY = "HAPPY"
PROSPEROUS = "PROSPEROUS"
ALL = [VENGEFUL, HATEFUL, DISAPPOINTED, NEUTRAL, SATISFIED, HAPPY, PROSPEROUS]
def get_final_standing(relation_score, thresholds):
"""Determine how the faction thinks about the player at the end"""
for threshold, feeling in zip(thresholds, RelationshipLevels.ALL):
if relation_score <= threshold:
return feeling
return RelationshipLevels.ALL[-1]
def roadblock():
roadblock = 'Please enter a valid input'
print(roadblock)
def prompt_for_input(prompt, valid_inputs, max_tries=6):
print(prompt)
for _ in range(max_tries):
user_input = input('> ').upper()
if user_input in valid_inputs:
return user_input
if user_input == 'Q':
break
# the input was not valid, show the roadblock
roadblock()
print('Seems like you are not willing to play. Goodbye!')
sys.exit(0)
def story(relationships, choices):
situation_winter = prompt_for_input(MSG_WINTER, ('X', 'B'))
if situation_winter == 'X':
short_load()
print(MSG_WINTER_ALTER_SUPPLY)
print(change_relation(relationships, "civilians", "---"))
choices.append('chose safety over risk')
# spring_alter_supply(relationships, choices)
# We take a shortcut to the end here
the_end(relationships, choices)
elif situation_winter == 'B':
short_load()
print(MSG_WINTER_IGNORE_RISK)
print(change_relation(relationships, "army", "---"))
print(change_relation(relationships, "civilians", "---"))
choices.append('chose risk over safety')
# spring_ignore_risk(relationships, choices)
# We take a shortcut to the end here
the_end(relationships, choices)
def the_end(relationships, choices):
print('THE END')
print('='*80)
final_standing_civ = get_final_standing(
relationships[Factions.CIVILIANS], (-8, -4, -1, 2, 5, 8)
)
print(f'You left the {Factions.CIVILIANS} feeling {final_standing_civ}.')
final_standing_army = get_final_standing(
relationships[Factions.ARMY], (-7, -4, -2, 2, 5, 7)
)
print(f'You left the {Factions.ARMY} feeling {final_standing_army}.')
print('='*80)
print("Decisions:\nYou ...")
for choice in choices:
print(f"... {choice}")
def main():
relationships = {Factions.ARMY: 0, Factions.CIVILIANS: 0}
choices = []
print(MSG_SYNOPSIS)
long_load()
story(relationships, choices)
if __name__ == "__main__":
main()
@tyronewantedsmok
Copy link

UPDATED
gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment