Jump to content

  • Log In with Google      Sign In   
  • Create Account

[Solved]How to make variable for item[Adventure Text Game]


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Kurai Tsubasa   Members   -  Reputation: 207

Like
0Likes
Like

Posted 04 January 2014 - 01:36 AM

Hi everyone biggrin.png i tried to make item in adventure text based game but it seems not working i make the variable for key so when the variable go to number 1 it would open the door but when i tried it.It show me an error : Name:Global name key_work_room is not defined

 

here my script(fixed for global variable):

from sys import exit

global key_work_room

key_work_room = 0

def work_room():
	global key_work_room
	if key_work_room == 1:
		print "There is a book."
		print"""Choices:
		1.Read
		2.Leave
		"""
		
		next = raw_input(">")
		
		if next == "1":
			print """
			Go to the Dark corridor and there is a door that
			keep a magic sword.It can used to kill demon 
			in that door there is a key
			541
			"""
		elif next== "2":
			stair()
		else:
			print "Check your spelling."
			print " "
			work_room()
	else:
			print "Check your spelling."
			print " "	
			work_room()
def bed_room():
	print "there is a key in the desk."
	print """choices:
	1.Take.
	2.leave.
	"""
	
	next = raw_input("> ")

	if next == "1":
		global key_work_room
		key_work_room += 1
		stair()
	
	elif next == "2":
		stair()
	else:
		print "Check your spelling."
		print " "
		bed_room()

def second_floor():
	print "There is so many torch in this room but there is a dark side."
	print """Choices:
	1.Take the torch.
	2.Go to the dark side.
	3.back
	"""
	
	next = raw_input("> ")
	
	if next == "1":
		print "You get the torch"
		torch = 1
		second_floor()
	elif next == "2":
		print "Something lurking in the dark attack you..."
	elif next == "3":
		stair()
	else:
		print "Check your spelling."
		print " "
		second_floor()

def stair():
	print key_work_room
	print "Now you're in the 2nd floor."
	print "There are 4 room."
	print "which one do you choose? or Do you want take a look around?"
	print "1. to look door 1."
	print "2. to look door 2."
	print "3. to look door 3."
	print "4. to look door 4."
	print "5. to look around."
	
	next = raw_input("> ")
	
	if next == "1":
		king_throne()
	elif next == "2":
		bed_room()
	elif next == "3":
		veranda()
	elif next == "4":
		work_room()
	elif next == "5":
		second_floor()
	elif next == "exit":
		exit(0)
	else:
		print "Check your spelling."
		print " "
		stair()
		
def living_room():
	print "you find yourself in living room."
	print "You don't remember anything it looks like something hit your head."
	print "You now fully regain your consciousness."
	print "You start to walking and you found a stair and dark corridor."
	
	next = raw_input("> ")
	
	if next == "stair":
		stair()
	elif next == "dark corridor":
		dark_corridor
	elif next == "exit":
		exit(0)
	else:
		print "Check your spelling."
		print " "
		living_room()

def start():
	print "Welcome in Fantasy World."
	print "This is simple game type 'exit' to exit."
	print "choose your destination by type the destination name."
	print "Are you boy or girl?"
	character = raw_input("> ")
	
	if character == "boy" in character or "girl" in character:
		living_room()
	elif character == "exit":
		exit(0)
	else:
		print "Check your spelling."
		print " "
		start()

def dead():
	print "you died and the mystery still uncovered."
	print """Choices:
	1.Play again
	2.Take a rest.
	"""
	choice = raw_input("> ")
	
	if choice == "1":
		start()
	elif choice == "2":
		exit(0)
	else:
		print "Check your spelling."
		print " "
		dead()

start()

it haven't finished yet but i need key to know how make the variable for item work also if you find anything weird in my script please tell mesmile.png .Thank youbiggrin.png


Edited by Kurai Tsubasa, 05 January 2014 - 01:54 AM.


Sponsor:

#2 Oralordos   Members   -  Reputation: 510

Like
1Likes
Like

Posted 04 January 2014 - 02:00 PM

Your problem is that variables are local to the function by default. So other functions cannot see the variable. To fix this, add a line to make the variable global in the first line in the function.

global key_work_room


#3 shadowisadog   Crossbones+   -  Reputation: 2563

Like
1Likes
Like

Posted 04 January 2014 - 05:16 PM

I think a good place to go from here would be to revise your code so that you store the rooms and descriptions in a configuration file (Such as JSON or XML).

 

You could store what items you have using a python dictionary.

 

Doing these things will allow you to expand to your game without having to increase the size of your code.



#4 Kurai Tsubasa   Members   -  Reputation: 207

Like
0Likes
Like

Posted 05 January 2014 - 12:54 AM

 

Your problem is that variables are local to the function by default. So other functions cannot see the variable. To fix this, add a line to make the variable global in the first line in the function.



global key_work_room

Thank you now it works



#5 Kurai Tsubasa   Members   -  Reputation: 207

Like
0Likes
Like

Posted 05 January 2014 - 01:00 AM

I think a good place to go from here would be to revise your code so that you store the rooms and descriptions in a configuration file (Such as JSON or XML).

 

You could store what items you have using a python dictionary.

 

Doing these things will allow you to expand to your game without having to increase the size of your code.

Thank you i t really helping mesmile.png 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS