Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do I add items (variables) to this game for certain game choices?

+1 vote
asked Jan 30 by Jeff The Chicken (2,860 points)

Recently Somebody helped me to simplify a text-only game I was making, but I don't know how I can make it change variable values based on the options the user chooses:

import time

# Declaration of classes -- game logic
class Action:
    def __init__(self, description, newLocation):
        self.description = description
        self.newLocation = newLocation

class Location:
    def __init__(self, name, description):
        self.name = name
        self.description = description
        self.actions = []
    def addAction(self, action):
    def arrive(self):
        print('You arrived to: {}'.format(self.name))
        i = 0
        print("   (0. Exit game)")
        for action in self.actions:
            i = i + 1
            print("    {}. {}".format(i, action.description))
        validChoice = False
        while not validChoice:
                index = int(input("? : "))
                if index == 0:
                    print("Abandoning your quest... :(")
                if 0 < index and index <= len(self.actions):
                    validChoice = True
            except ValueError:
        self.actions[index - 1].newLocation.arrive() # indexing is 0-based, but we get 1-based index from the user
# Declare your locations here -- data
#initial rooms
house = Location("house", "Inside the house there is a sweet aroma of rolls being baked.")
field = Location("field", "Grass swishes as you walk through it. a solitary horse is grazing in the field.")
stable = Location("stable", "You are greeted by the sound of various animal noises and the smell of various animal odors")
#secondary rooms-house

#declare location actions here
spawnPosition = Location("Spawn position", "You wake up in a sunny morning. After you get out of your bad, you decide to:")
spawnPosition.addAction(Action("for house", house))
spawnPosition.addAction(Action("for field", field))
spawnPosition.addAction(Action("for stable", stable))
#house options
house.addAction(Action("leave the house",field))

# The game starts here
print ("Cool Cousins Studios Presents...")
time.sleep(2) # Sleep for 2 seconds
print ("With use of Python Code...")
time.sleep(2) # Sleep for 2 seconds
print ("Adventure In Saldica")
time.sleep(2) # Sleep for 2 seconds
print ("(cue dramatic music)")
time.sleep(3) # Sleep for 3 seconds

spawnPosition.arrive() # This is all you need to do to initiate the game. When this function returns, your journey has concluded.

1 Answer

+1 vote
answered Feb 1 by Peter Minarik (22,620 points)
selected Feb 1 by Jeff The Chicken
Best answer

Answered here.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.