1,105,585 Community Members

Python Text Based Game

Member Avatar
roshurwill
Newbie Poster
2 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi everyone, recentley new to programming and the language python.
So ive learned an amount of python, i decided to start a projct.
My project is to make a game, which the user plays using only commands, like walk foward, open the door etc, you are in the bedroom... whatever.
Ive started programming some of it but wondering how to link back to a previous line and start from there again, and also once a command has been said it can then progress onto the next stage, for example the user is sat on the bed, but stands up from the users input command, then the next stage of commands that would be available should be loaded and then continue depending on the users input.
Heres what ive got so far

import sys, os
print ("You wake up and you do not remember anything...")

raw_input("Press Enter to continue..")
os.system('cls')

print ("You sit up and find yourself in a bedroom")
raw_input("Press Enter to continue..")
os.system('cls')

#print ("You try to stand up but you are hand cuffed to the bed, what do you do?")
#look around, to the left is..., infront of you is..., to the right of you is, above you is..., behind you is..., below you is...
command = raw_input("What do you do?: ")
 
while 1:
    if command == "look up":
        print "You look up to the ceiling but nothing of any use is up there."
        break
   
    elif command == "look down":
        while 1:
            print "You look down you are sat on a bed."
            break
       
    elif command == "look to the left":
            while 1:
                print "You look to the left and see a cabinet."
                break             
                
    elif command == "look to the right":
            while 1:
                print ("You look to the right and see set of drawers.")
                break
            
    elif command == "look behind yourself":
            while 1:
                print ("You look behind yourself and they is a wall.")
                break
    elif command == "look ahead":
            while 1:
                print ("You look ahead and see a door")
                break
                           
    else:
        print "Invalid action"
        command = raw_input("Try doing something else: ")

so say the user looks up, the command then says what is above the user, then id like it to return back to the choices available as the user hasnt moved, and then making the user move and progress to the next available moves. It sounds a bit complicated to explain but i think someone will understand me.
Cheers
roshurwill

Member Avatar
woooee
Posting Maven
2,798 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
0
 

First, these while() loops and/or "break" are not necessary

if command == "look up":
        print "You look up to the ceiling but nothing of any use is up there."
#        break
    elif command == "look down":
#        while 1:
            print "You look down you are sat on a bed."
#            break

Your code would then loop but would never exit, so you should add an exit option and a break.

while 1:
    command = raw_input("What do you do?: ")
    if command == "look up":
        print "You look up to the ceiling but nothing of any use is up there."
#        break
 
    elif command == "look down":
#        while 1:
            print "You look down you are sat on a bed."
#            break

    elif command == "exit":
        break     ## exits the while loop

print "Bye, Bye"

To get some movement you would use a common function which you would pass a direction to, and possibly distance, and the function would move the player in that direction.

while 1:
    command = raw_input("What do you do?: ")
    if command == "look up":
        print "You look up to the ceiling but nothing of any use is up there."
#        break
 
    elif command == "look down":
#        while 1:
            print "You look down you are sat on a bed."
#            break
    elif command == "move right":
       distance = raw_input("How far do you want to move? ")
       move(player, int(distance), "right")
    elif command == "exit":
        break     ## exits the while loop
Member Avatar
roshurwill
Newbie Poster
2 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

right ive sorted out my breaks, im a bit stumped on how im going to get the character to move, say i wanted the character to turn to the right and walk foward, then everything in the room would move round, surely i would need to have a different set of commands depending on the whereabouts and direction facing? Ive picked a bit of a complicated project for a beginner havent i? haha

Member Avatar
woooee
Posting Maven
2,798 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
0
 

Tic-Tac-Toe or Hangman are more common starting points. Then perhaps poker or blackjack.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: