Hello. I am attempting to make a text-based game in Python. Although I assumed it would be rather easy, there are not that many examples to go by. Therefore, I've come to ask for some assistance with my code - being the relative newbie to coding that I am. Excuse the lengthy-ness, I just wanted to give a decent-sized excerpt of the game so far.

Basically, I just want to know if there's a simpler way to go about making different rooms for the character/player to explore (maybe using classes?), if there's an easier way to go about using dialog (I am not using PyGame, so I do not think I would be able to use a GUI), and what would be the easiest way to set up coding that works like a traditional text-based game (e.g. the player types in "Examine" and then types in what to examine) instead of using numbers.

gold = 0
invent = [ ]

startgame = 0
pcname = "foo"
wardrobe = 0
ktchn = 0
raldotlk = 0

print "HINT: Press 'H' to view potential options and 'I' for the inventory!"
	
def menu():
	global gold
	global invent
	print "Your current inventory includes:"
	print invent
	print "And you have " + str(gold) + " pieces of gold."
		
def raldos():
	global raldotlk
	chat = 1
	if raldotlk == 0:
		print "Guardsman: Hello there, boy. What are you doing here?"
		print "You: I'm just sight-seeing. This is a really amazing mansion."
		print "Guardsman: It is."
		print "You: Are you the commander of his guards?"
		print "Guardsman: I am. I am called Raldos."
		print "Raldos: Now if you excuse me, I must return to my duties."
		raldotlk = 1
	else:
		print "Raldos: Hello again. If you don't mind, I'm rather busy at the moment."
				
def room1():
	global startgame
	global pcname
	global wardrobe
	global invent
	global gold
	inroom1 = 1
	go1 = ["(1) Examine bed", "(2) Examine wardrobe", "(3) Front door - Town square", "(4) Small door - Kitchen"]
	if startgame == 0:
		print "You lazily slip out of your rather uncomfortable bed. Slipping on a gray wool shirt and tough leather trousers, you stand up and glance about. You think you're being watched."
		print
		print "Suddenly, a man's voice emerges from the stillness of your room!"
		pcname = raw_input("'What is your name!?' the voice asks. ")
		print "'My name is " + pcname + "!' you shout back. 'Who are you?'"
		print "Mysterious voice: 'Do not worry, " + pcname + ". We shall meet soon enough.'"
		print "And then everything was silent again."
		startgame = 1
	else:
		print "You are in your bedroom."
		print "The wood floorboards groan in agony as you walk across them. In the center of the room sits a rather uncomfortable looking bed. There is wardrobe in the left corner of the room, and two doors - one door leading outside, and another leading into the kitchen of your small cottage."

	while inroom1 == 1:
		print "Where do you want to go? "
		roomloc = raw_input("> ")
		if roomloc == "h" or roomloc == "H":
			print go1
		elif roomloc == "i" or roomloc == "I":
			menu()
		elif roomloc == "1":
			print "This bed is made of straw. It was put together in a hurry and it looks rather uncomfortable. There is a cotton pillow at its head, and there is a thick blanket made of animal hide dangling from its side."
		elif roomloc == "2":
			if wardrobe == 0:
				print "This oaken wardrobe is about twice as tall as you are. The craftsmanship on this wardrobe is quite professional in appearance; however, it is locked and is collecting dust. It is obvious that termites have been gnawing at it for quite some time."
				if "lead key" in invent:
					print "You use the lead key you were given to open the wardrobe."
					print "Upon opening the wardrobe, you find 50 gold!"
					wardrobe = 1
					invent.remove("lead key")
					gold += 50
			else:
				print "This oaken wardrobe is about twice as tall as you are. The craftsmanship on this wardrobe is quite professional in appearance. Looking inside, you find it empty. It is obvious that termites have been gnawing at it for quite some time."
		elif roomloc == "3":
			print "You leave through the front door."
			inroom1 = 0
			room6()
		elif roomloc == "4":
			print "You leave through the kitchen door."
			inroom1 = 0
			room2()
		else:
			print "Try again."
			
def room2():
	global ktchn
	global invent
	inroom2 = 1
	go2 = ["(1) Examine ice box", " (2) Examine cupboard", "(3) Small door - Bedroom"]
	print "You are in your kitchen."
	print "The stone walls are whitewashed and the floor is dotted with puddles of water. The room itself is rather small and contains quite a bit of dust. It looks as though it has been out of use for quite some time. There is an icebox in the far corner of the room, small cupboard under the wash-basin, and a door to your bedroom and the garden."
	while inroom2 == 1:
		print "Where do you want to go?"
		roomloc = raw_input("> ")
		if roomloc == "h" or roomloc == "H":
			print go2
		elif roomloc == "i" or roomloc == "I":
			menu()
		if roomloc == "1":
			if ktchn == 0:
				print "You have found a fish in the icebox - a trout, to be specific. It looks like it was caught a few days before."
				if "rugged cloth" in invent:
					print "You wrap the trout in the cloth you found earlier and place the fish in your inventory."
					invent.remove("rugged cloth")
					invent.append("trout")
					ktchn = 1
				else:
					print "If you pick it up, the trout will get messy. You decide to leave it in the ice box for now."
			else:
				print "The ice is slowly melting inside this otherwise empty ice box."
		elif roomloc == "2":
			print "You bend down and peer into the cupboard. You can tell that someone has been rummaging through its contents; however, it is now empty. Not a crumb, dish, or cup remains."
		elif roomloc == "3":
			print "You leave through the small door."
			inroom2 = 0
			room1()
		else:
			print "Try again."
			
def room6():
	inroom6 = 1
	go6 = ["(1) Front door - Your house", "(2) Manor"]
	print "You have entered the town square of Kal. Amidst the hustle and bustle of the townsfolk, you can barely make out the stone walkway that encompasses the fountain at the town center. You can even see a few merchant shops and traveling salesmen trying to set up shop in the commotion. You can also see your house and the alley behind it, the forge, the pub, an exquisite manor, and - in the distance - the town's gates."
	while inroom6 == 1:
		print "Where do you want to go?"
		roomloc = raw_input("> ")
		if roomloc == "h" or roomloc == "H":
			print go6
		elif roomloc == "i" or roomloc == "I":
			menu()
		elif roomloc == "1":
			print "You enter your house through the front door."
			inroom6 = 0
			room1()
		elif roomloc == "2":
			print "You walk toward the colossal mansion in the distance."
			inroom6 = 0
			room9()
		else:
			print "Try again."
			
def room9():
	inroom9 = 1
	go9 = ["(1) Talk to Raldos", "(2) Examine gate", "(3) North - Town square", "(4) South - Town walls"]
	print "You approach the extravagent manor toward the edge of the town. In contrast to its surroudnings, this manor is beautifully adorned, has a massive, beautiful garden, and is protected by a large gateway and several guardsmen. You can travel toward the town's edge or town's center from here."
	while inroom9 == 1:
		print "Where do you want to go?"
		roomloc = raw_input("> ")
		if roomloc == "h" or roomloc == "H":
			print go9
		elif roomloc == "i" or roomloc == "I":
			menu()
		elif roomloc == "1":
			raldos()
		elif roomloc == "2":
			print "The metal gates that surround the manor are large and imposing. They encircle the entire estate, barring entry and keeping those inside from leaving. A guard passes you by every few minutes; it would be near-impossible to climb this fence even if you wanted to."
		elif roomloc == "3":
			print "You walk north toward the center of town."
			inroom9 = 0
			room6()
		elif roomloc == "4":
			print "You walk south toward the edge of town."
			inroom9 = 0
			room10()
		else:
			print "Try again."
			
def room10():
	inroom10 = 1
	go10 = ["(1) City gates - Plains of Aen", "(2) Manor"]
	print "You approach the town gates of Barson. Matching the decour of the majority of the town, these gates are made of wood and are not very tall. Several archers stand atop these towers to defend the small town from attack. You can exit the town from here, or head closer closer to the town square."
	while inroom10 == 1:
		print "Where do you want to go?"
		roomloc = raw_input("> ")
		if roomloc == "h" or roomloc == "H":
			print go10
		elif roomloc == "i" or roomloc == "I":
			menu()
		if roomloc == "1":
			print "You cannot leave Kal at this time."
		if roomloc == "2":
			print "You head toward the colossal manor in the distance."
			inroom10 = 0
			room9()
		else:
			print "Try again."
			
room1()

Greetings, welcome. Wondering what version of Python you're using and on which platform?

First a tip: Don't EVER use tabs for Python indentation. It's pretty much standard practice to use 4 spaces instead. Most text editors can be set up to handle this. Here: Read over PEP 8, and then use it like your style bible.

Second: try to avoid using global variables. It's not really a good idea. This is the most common mistake that beginners make, as it just seems so easy. It's better to actually have proper structure and functions so that your code is more.... stable.

And finally: There's plenty of examples of budding Python masters text-based adventure games on this very site. Try searching for 'Python adventure' in the 'Site Search' box. Read over our responses, tips and suggestions and see if any of the information that you seek has already been answered. If you have any specific road blocks on the way to refining your program, post the related code and error/difficulty and we'll try to help.

Thank you for your help. The "help" feature did answer most of my questions, and provided me with a few good examples.

I do have one remaining question, however. In my current version of the game, I use numbers to determine where to travel. What would be the easiest way to alter that and make it function more like a traditional text game (entering ">Examine flower pot" would allow a character to examine the flower pot in room x, for example)?

Well really that would require more work on your part. You've already got the concept of if structures and conditional actions, as well as user input... so really you just need to expand upon that.

I suggest next learning how to make use of classes. With classes you could make something that is more refined. Having a player class would also allow your game to have multiple players. Each with their own instance of the class that could keep track of HP, Stats, Inventory, location, etc.

You could make a room class with the "items" in a room and ther associated uses, and the number of doors/windows, whatever. Really you can get creative with it, it's your game afterall!

Mate I love this code, would it be alright if I were to use this to muck around with?

Have you finished it? I would love to see the final version

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.