Question:
Text Based Python Game?
?
2013-08-24 08:12:03 UTC
So basically I'm writing a text based Python game but I'm a little stuck... Here's the code:

Command = input('what would you like to do?: ')

if Command == 2:
print'''

You look right too see a creased photo of a family on a wall...
You have a strange feeling you recognise them, but your mind is too blury from
memory loss so you shrug and look away...
'''
time.sleep(2)

elif Command == 3:
print '''

You go to stand up, but are immediately pulled back by something attached
to your wrists. You then realize you have been handcuffed to the bed you've been
sleeping on...
'''

Basically, I need it so when Command is equal to 2 for example, something gets printed then Command goes back to being equal to input... Any help?

Thanks!
Three answers:
husoski
2013-08-24 10:10:46 UTC
@Jay: It's not Python 3 or else Command would never compare equal to an integer 2. Also, triple quotes are for any string that has newlines in it, not just a docstring.



EddieJ has the correct approach. You do need to change your descriptions and responses to inputs according to both the player's location and also the game state. There are a TON of different ways to do this. The best ones, in terms of game development, encode the entire game in data structures that can be initialized from external files at startup. That way, you can have a game designer work on the text and responses independently of programmers working on the code.



On a one-hat project, where you are the programmer, game designer, build technician, head cook and bottle washer, that might be overkill. Also, you have to learn a lot before you can get started.



A simpler way, that will get you started right away is to package each "location" in the game in a defined function. That function remains in control until a command causes movement to another location.



Example (using .... for indents):



------------- cut here -----------------



def maze_1():

.... desc = '''This is a twisty maze of little passages, all alike.'''

.... loc = maze_1 # stay here until loc is changed

.... print desc

.... while loc == maze_1:

.... .... cmd = raw_input("Which way now? ");

.... .... cmd = cmd.lower();

.... .... if cmd in ("n", "north"):

.... .... .... loc = maze_2

.... .... elif cmd in ("s", "south", "e", "east", "w", "west"):

.... .... .... print desc # repeat description and stay here

.... .... else:

.... .... .... print "That's not a direction"



.... return loc # let caller know where to go



def maze_2():

.... desc = '''This is a little maze of twisty passages, all alike.'''

.... loc = maze_1 # stay here until loc is changed

.... print desc

.... while loc == maze_1:

.... .... cmd = raw_input("Which way now? ");

.... .... cmd = cmd.lower();

.... .... if cmd in ("e", "east"):

.... .... .... loc = maze_2

.... .... elif cmd in ("s", "south", "n", "north", "w", "west"):

.... .... .... pass # stay here

.... .... elif cmd in ("sw", "southwest"):

.... .... .... loc = None # end of game if secret exit found

.... .... else:

.... .... .... print "That's not a direction"



.... return loc # let caller know where to go



loc = maze_1

while (loc != None):

.... loc = loc()



print "Congratulations! You found the way out!!"



------------- cut here -----------------



That's a trivial two-room maze. Notice that the "driver" code at the bottom is extremely simple. This will let you deal with each location on its one. You'll want to add state variables for the game, the player and each location. A cool way to do that is with dictionaries:

locitems = { maze_1 : ["hammer"], maze_2 : ["nail", "bandage"]}



...will let you see that locitems[maze_2] is the list ["nail", "bandage"], possibly listing what's present and visible at that location. Something like this is needed because local variables disappear whenever a function returns, so you need globals or an object to hold state information.



Like I said, there's a bunch of other ways, so feel free to improvise with what works best for your game and for you coding style.
?
2013-08-24 08:22:24 UTC
if you did not make sure that you import time or at least from time import sleep. and if you are programming in python 3 you need to use print lime this:



print("”)



it also looks like you are trying to use print with triple quotes. triple quotes are for comments.
anonymous
2016-11-11 06:03:54 UTC
the following is a existence device for a kind depending sport you purely replace the int and also the ifs if u might want to prefer to to be textual content depending sorry this changed into previous code and don't have time to regulate :( sorry lives = 2 count number = 3 count1 = 2 answer = 5 even as genuine: print(count1, "+", count number) adder = int(enter("style answer the following: ")) if adder == answer: print(" nicely accomplished") elif adder < answer: print("You lost a existence") lives -= a million print(lives) elif adder > answer: print("You lost a existence") lives -= a million print(lives) if lives == 0: print("you free") stop()


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...