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 you like the text-based game I made w/ python?

+6 votes
asked Jan 28 by Jeff The Chicken (2,860 points)
reshown Jan 28 by Jeff The Chicken

Do you know any way I could improve it?


2 Answers

+2 votes
answered Jan 29 by Peter Minarik (22,320 points)
selected Jan 29 by Jeff The Chicken
Best answer


I'd make the code more structured, use the object-oriented aspect of the language.

Right now the code is very repetitive, I'd say copy-paste all over the place.

I think with the usage of classes, one could separate the game logic (entering a location, offering choices, acting on the user input) from the data (description of the location, available choices, consequences of the user actions).

This would make the code more readable and easier to extend.


I've added a sample code to show what I mean by separating the game logic from the data using objects and functions.

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
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")

spawnPosition = Location("Spawn position", "You wake up in a sunny morning. After you get out of your bad, you decide to:")
spawnPosition.addAction(Action("go to the house", house))
spawnPosition.addAction(Action("walk the fields", field))
spawnPosition.addAction(Action("visit the stable", stable))

# 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.
commented Jan 29 by Jeff The Chicken (2,860 points)
What about gameplay? And what do you mean by The object-oriented aspect?

P.S. thanks for answering both of my questions.
commented Jan 29 by Peter Minarik (22,320 points)
I've updated my original comment with a sample of how I'd do simple object-oriented text-based games instead of a simple if/else structures copy-pasted around.

Of course, I just made only one location work, you can do the rest. You can build connections between location (and actions) easily.

I like text based games, so I urge you to keep going! ;)
commented Jan 29 by Jeff The Chicken (2,860 points)
I will, and I can see that this is more convenient and organized.  My only problem now is that this is beyond my current understanding of code! Thank you for giving me something to study, though, because this will help me learn more than any coding class. ;)
commented Jan 30 by Jeff The Chicken (2,860 points)
Okay, I think I understand how this works, but how do I use variables (for items) in this?
asked Jan 31 by Jeff The Chicken (2,860 points) How do I add items (variables) to this game for certain game choices?
commented Feb 1 by Peter Minarik (22,320 points)
Python is not my strong-suite either. I find the following site useful to learn the basics: https://www.w3schools.com/python/default.asp
+1 vote
answered Mar 2 by Colin Broyles (440 points)
Instead of having 3 numbers for choices, you could input text
commented Mar 2 by Jeff The Chicken (2,860 points)
That's a good idea too
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.