7 Replies - 18088 Views - Last Post: 23 November 2009 - 10:21 AM Rate Topic: -----

#1 dkking101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-November 09

Python text-based game

Post icon  Posted 22 November 2009 - 05:43 PM

im currently using python and got pretty far with a text-based game. I've read tons of ways to do a game like this but to no avail. i was wondering if you guys could help me out. I've tried global key, and than giving key the value of zero. and key = key + 1 when the key is discovered however this for some reason if you use a distinct path in the game. i deleted the "key, gun, and ammo" and just wonderin' where to put them. Any input is much appreciated.

well here's what i got:

print
print
print
print"************************************************************************"
print" DOMINIC'S ADVENTURE "
print"************************************************************************"

# intro to text-based game


print
print
print
print "You wake up from a crazy night of shrums only to find"
print "yourself wandering in a ghostly forest with your trustworthy"
print "dog, named Rex. Your dog seems agitated and starts barking"
print "relentlessly. You spot in the distance a vacant cabin."
print "Due to your curiosity you promptly enter. You enter the house"
print "and see a staircase leading downstairs, a rotting door, and a "
print "ladder up to a loft."
print
inpute = raw_input("What would you like to do: ")
def start():
if inpute == "door()":
door()
print
elif inpute == "ladder()":
ladder()
print
elif inpute == "exit_house()":
exit_house()
print
elif inpute == "commands()":
commands()
print
elif inpute == "basement()":
basement()
print


# defining classes

def ladder():
print "You climb the ladder and are now on the loft."
print "Great view from up here."
examine_ladder = raw_input("examine the loft y/n: ")
if examine_ladder == "n":
global key
print "you go down the stairs, and are on the base floor again."
elif examine_ladder == "y":
gun = 0
print "You find a chest that is locked. You need a key."
print
if key > 0:
print
print "You open the chest and find a .357 magnum."
print "You take it."
print
gun = gun + 1
print "You find a chest that is locked. You need a key."
elif gun == 1:
print "The chest is empty."
def door():
print "You enter the door and and get a whiff of some kind"
print "of strong liquor."
examine_door = raw_input("examine this room y/n: ")
if examine_door == "y":
key = 0
print "You see a cabinet and open it to find a key."
print "You take the key. Maybe you can open up something?"
key = key + 1
print
elif examine_door == "n":
print"you exit the door without examination"
print

def basement():
print "You venture downstairs, and smell rotting flesh...."
print "Is this man a murderer?"
print
examine_base = raw_input("Do you wish to examine the basement y/n: ")
print
if examine_base == "n":
print "You don't examine the basement and and return upstairs."
print
elif examine_base == "y":
ammo = 0
print
print "You examine the basement and find some pistol ammo"
print
print " _ "
print " /-\ "
print " (___) "
print " ! ! PISTOL AMMO "
print " ! ! "
print " ! ! - six rounds "
print " ! ! "
print " ! ! "
print " !___! "
print " (_____) "
ammo = ammo + 6
print

def exit_house():
if gun == 0:
print "You exit the house and see a crazy old man that wants to kill you."
print "You're forced to retreat inside."
print
print " &&&&& "
print " @@@@@@@@@ "
print " 7^^_^^^_^^7 "
print " I (O) (O) I "
print " ( / \ ) "
print " \ (*_*) / "
print " \ --- / "
print " \_I_/ "
print
if gun > 0 and ammo < 6:
print "You hit the crazy man on the head with the gun and"
print "retreat inside, locking the door behind you."
print
start()
if gun > 0 and ammo > 0:
print "You shoot the man in the face and steal his wallet."

def commands():
print "door(), ladder(), basement(), exit_house()"

*please need help*

Is This A Good Question/Topic? 0
  • +

Replies To: Python text-based game

#2 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 15
  • View blog
  • Posts: 615
  • Joined: 13-August 05

Re: Python text-based game

Posted 22 November 2009 - 06:33 PM

Well first of all, you really don't need all those empty print commands. If you want blank lines, just insert newline characters: \n.

Anyway, I was wondering what your specific problems were exactly? What errors are you getting?

Also, it would be much easier to read if you placed your code inside [ code] brackets.
Was This Post Helpful? 0
  • +
  • -

#3 dkking101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-November 09

Re: Python text-based game

Posted 22 November 2009 - 08:58 PM

View PostSPlutard, on 22 Nov, 2009 - 05:33 PM, said:

Well first of all, you really don't need all those empty print commands. If you want blank lines, just insert newline characters: \n.

Anyway, I was wondering what your specific problems were exactly? What errors are you getting?

Also, it would be much easier to read if you placed your code inside [ code] brackets.



well if i go global key under door(): it will work fine. but only if i go in the door first and not the ladder(): f i go to the ladder() first it gives me something that says "key" is not defined i will also do a reply with the code properly done.

This post has been edited by dkking101: 22 November 2009 - 09:00 PM

Was This Post Helpful? 0
  • +
  • -

#4 dkking101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-November 09

Re: Python text-based game

Posted 22 November 2009 - 09:06 PM

print
print
print
print"************************************************************************"
print"						 DOMINIC'S ADVENTURE							"
print"************************************************************************"

# intro to text-based game


print
print
print
print "You wake up from a crazy night of shrums only to find"
print "yourself wandering in a ghostly forest with your trustworthy"
print "dog, named Rex. Your dog seems agitated and starts barking"
print "relentlessly. You spot in the distance a vacant cabin."
print "Due to your curiosity you promptly enter. You enter the house"
print "and see a staircase leading downstairs, a rotting door, and a "
print "ladder up to a loft."
print
inpute = raw_input("What would you like to do: ")
def start():
	if inpute == "door()":
		door()
		print
	elif inpute == "ladder()":
		ladder()
		print
	elif inpute == "exit_house()":
		exit_house()
		print
	elif inpute == "commands()":
		commands()
		print
	elif inpute == "basement()":
		basement()
		print
	
		
# defining classes

def ladder():
	print "You climb the ladder and are now on the loft."
	print "Great view from up here."
	examine_ladder = raw_input("examine the loft y/n: ")
	if examine_ladder == "n":
		global key
		print "you go down the stairs, and are on the base floor again."
	elif examine_ladder == "y":
		gun = 0
		print "You find a chest that is locked. You need a key."
		print
	if key > 0:
		print
		print "You open the chest and find a .357 magnum."
		print "You take it."
		print
		gun = gun + 1
		print "You find a chest that is locked. You need a key."
	elif gun == 1:
		print "The chest is empty."
def door():
	print "You enter the door and and get a whiff of some kind"
	print "of strong liquor."
	examine_door = raw_input("examine this room y/n: ")
	if examine_door == "y":
		key = 0
		print "You see a cabinet and open it to find a key."
		print "You take the key. Maybe you can open up something?"
		key = key + 1
		print
	elif examine_door == "n":
		print"you exit the door without examination"
		print

def basement():
	print "You venture downstairs, and smell rotting flesh...."
	print "Is this man a murderer?"
	print
	examine_base = raw_input("Do you wish to examine the basement y/n: ")
	print
	if examine_base == "n":
		print "You don't examine the basement and and return upstairs."
		print
	elif examine_base == "y":
		ammo = 0
		print
		print "You examine the basement and find some pistol ammo"
		print
		print "		   _									 "
		print "		  /-\									"
		print "		 (___)								   "
		print "		 !   !	 PISTOL AMMO				   "
		print "		 !   !								   "
		print "		 !   !	- six rounds				   "
		print "		 !   !								   "
		print "		 !   !								   "
		print "		 !___!								   "
		print "		(_____)								  "
		ammo = ammo + 6
		print

def exit_house():
	if gun == 0:
		print "You exit the house and see a crazy old man that wants to kill you."
		print "You're forced to retreat inside."
		print
		print "				 &&&&&				"
		print "			   @@@@@@@@@			  "
		print "			  7^^_^/>^^_^/>^7			 "
		print "			  I (O) (O) I			 "
		print "			  (   / \   )			 "
		print "			   \ (*_*) /			  "
		print "				\ --- /			   "
		print "				 \_I_/				"
		print
	if gun > 0 and ammo < 6: 
		print "You hit the crazy man on the head with the gun and"
		print "retreat inside, locking the door behind you."
		print
		start()
	if gun > 0 and ammo > 0:
		print "You shoot the man in the face and steal his wallet."

def commands():
	print "door(), ladder(), basement(), exit_house()"

Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

Re: Python text-based game

Posted 22 November 2009 - 09:07 PM

Your last line of executed code is this is it not?
inpute = raw_input("What would you like to do: ")



because all the rest of your code is in function blocks. When the computer reaches this point, it doesn't know what to do. My advice is to check what the user entered, and if it works then call the appropriate function...
Was This Post Helpful? 0
  • +
  • -

#6 dkking101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-November 09

Re: Python text-based game

Posted 22 November 2009 - 09:15 PM

View PostDogstopper, on 22 Nov, 2009 - 08:07 PM, said:

Your last line of executed code is this is it not?
inpute = raw_input("What would you like to do: ")



because all the rest of your code is in function blocks. When the computer reaches this point, it doesn't know what to do. My advice is to check what the user entered, and if it works then call the appropriate function...



i have it to where the user punches in function's and it gives output, true it does end after the first user input if it is not in idle, so should i loop tht
inpute = raw_input("What would you like to do: ")

or what. well tht is the least of my concerns my main concern is how to get the "key to work with all function's" along with the ammo and and gun. for some reason global(object) is'nt working for me....it has to have a set course like enter door ladder than downstairs otherwise i'll get an error saying "key" is not defined.
Was This Post Helpful? 0
  • +
  • -

#7 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 15
  • View blog
  • Posts: 615
  • Joined: 13-August 05

Re: Python text-based game

Posted 23 November 2009 - 03:19 AM

View PostDogstopper, on 22 Nov, 2009 - 08:07 PM, said:

Your last line of executed code is this is it not?
inpute = raw_input("What would you like to do: ")



because all the rest of your code is in function blocks. When the computer reaches this point, it doesn't know what to do. My advice is to check what the user entered, and if it works then call the appropriate function...


What he's saying is that you need to tell the script what to do with the input after you gather it with raw_input(). Right now, your program should barely do anything, given what you've shown us. Looking at your code, I think you just need to call start(), but in order for it to know what the contents of the variable 'inpute' are, you'll either have to make it a global variable or pass it as an argument of the 'start()' function (which is preferred, stylistically speaking).

I also noticed, looking at other parts of your code, that you use many variables without defining them (for example, incrementing a variable for which you haven't set an initial value). Be careful, because this will also break your code.
Was This Post Helpful? 0
  • +
  • -

#8 dkking101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-November 09

Re: Python text-based game

Posted 23 November 2009 - 10:21 AM

thx alot guys for the input, this game ad all was really a trial version i most likely will delete someday, but really the only reason i posted this is because i need to know about the term "global". I've looked up a demonstration of it and they used it for gold however that is very easy but i am unable to run this program fluently with 3 "global" objects.
i.e. gun, ammo, and a key. strictly because i may be putting the global(object) in the wrong spot. that's really myhelp, after building this i've become more familiar and fluent with the python terms such as: if, elif. Hmmm although i've never used else. Have to look that up some time. but again thanks for the input guys. very much apreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1