Jump to content
  • Advertisement
Sign in to follow this  
Mr Tyro

Defining error in Python with my game

This topic is 4518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am making this text game in python. It works fine for the most part. YOu play along and it gives you options. After you play for a little while it pops up an error message in the console. It says that one of my variable names was not defined. Even though I clearly started it back in the program. My game is made in python help is apreciated
#Main menu function

def mainMenu():
	print """
	TAKE OUT
	_________________
	
	
	MAIN MENU
	________________________________
	Play
	Instructions
	Version
	Credits
	Password
	--------------------------------"""
	
#function for when game is over	
def gameOver():
	print """
	__________________
	GAME OVER
	------------------"""
	
#password being worked on	
#def password():
#	pswrd = raw_input("Password:")

#beggining of the Take Out game
welcome = """Welcome to Take Out the text adventure of a hitman. Try to please your clients while trying not to get caught.
							READY?
						ENTER YOUR CODE NAME"""

print mainMenu()
pick = raw_input("Pick:")


#what each option in the main menu does
if pick == "Instructions":
	print """To play just type in the number of the action you 
	want to take.
	for example if there is
	1.wait
	2.shoot
	you just need to type in 1 to take the action of waiting"""
elif pick == "Version":
	print "Version 0.1"
elif pick == "Credits":
	print "Made by people\n Started on July 2\n Project HITMAN\nALIEN INTERTAINMENT PRODUCTION"
elif pick == "Play":
	print welcome
else:
	print "ERROR"

#code name the name your stuck with for the whole game	
codeName = raw_input("NAME:")
if codeName == "":
	print "You have not entered a name. Please enter a name to begin"
else:
	print """Welcome " + codeName + " You are a hitman, your code name . You have been to\n take out a guy by the name of Xio Lynn. Avoid being caught by people\n and don't attract to much attention."
1.Learn More
2.Start
3.Inventory"""
begin = input("What next:")

if begin == 1:
	print  " Xio Lynn is a drug lord with alot of power in china.\nHe controls part of Hong Kong, China and has his own gang called The Black Tiger.\nYour client has asked you to take him out any means neccessery with out getting cought.\nYour client will pay more if you make it look like an accident or if\n the bodies are not found.\n You will have only 12 hours to take Xio Lynn out.\n After the 12 hours are up Xio Lynn will board an airplane to\n Russia to make drug deals.\n Good Luck " + codeName + ""
elif begin == 3:
	print """Inventory
	_______________
	rope
	---------------
	Sniper
	---------------
	Slienced Pistol
	---------------
	Syringe Posion
	---------------
	Binoculars
	---------------
	$100
	---------------"""
elif begin == 2:
	print """ You are drinking your coffe and reading the paper
	when you see Xio Lynn walking past you
	1.Finish Breakfast
	2.Follow Him
	3.Ask for time"""
	morning = input("What do you do:")
else:
	print "ERROR"
	
if morning == 1:
	print """You finish your breakfast
	1.Go to Xio's hideout
	2.Go to Airport"""
	breakfast = input("What next:")
elif morning == 2:
	print """You follow him to the harbor. Xio stands there and waits.
	You see a guy walking towards Xio they start talking
	1.Wait
	2.Cap them"""
	harbor = input("Your next move:")
elif morning == 3:
	print """ You ask Xio for the time. He reconizes your face from some were.
	He knows your game and he knows your after himHe takes out his gun and 	
	shoots you  two times to the head at point blank"""
	print gameOver()
else:
	print "ERROR"
	
if breakfast == 1:
	print """You go to Xio Lynn's hideout in the warehouse district.
	The only visible entrance is through a semi-dark ally.
	There is some lighting along the way but the rest is all dark.
	1. Climb roof
	2. Hide in ally
	3. Sneak into hideout"""
	hideout = input("Your Action:")
elif breakfast == 2:
	print """You go to the airport and find which flight Xio is 
	expected to go on.
	1.Wait
	2.Go to hideout"""
	airport = input("What's your next move:")
else:
	print "Error"

	
if harbor == 1:
	print """You wait for Xio. The other guy jumps off the dock onto his boat.
	he speeds away on the boat. Xio walk towards you
	1. Cap him
	2. Posion him
	3. Follow him"""
	dock = input("What next:")
elif harbor == 2:
	print """you wait untill no one is looking. Nows your chance which 
	gun do you want to use?
	1.Sniper
	2.Silenced Pistol"""
	gun = input("Which gun:")
else:
	print "Error"

Share this post


Link to post
Share on other sites
Advertisement
"Breakfast" is assigned a value only if you have selected "Finish Breakfast". Later on, you check "if Breakfast==1" anyway. If you haven't previously selected "Finish Breakfast", then the variable "Breakfast" doesn't exist.

The design is not very good, and it will surely confuse you if you go on this way. There are many ways you can do it a lot better, but a start would be to clearly divide the game into seperate "rooms" like Airport,Harbor,etc. Each room is expressed with a function. It prints the text, gets the player's selection and according to that it returns the next room(don't forget, functions in Python are object, so you can manipulate them as variables). GameOver is also a room that prints "GameOver",but instead of returning the next room as every other function, it returns None, so the game knows we have no other rooms to go to and it just ends.

Here is your game translated in this design(only the actual games, I skipped the main menu and also I don't do any checking if the player selects an invalid option)


def GameOver():
print "Game over!"
return None

def Morning():
next_room=[Breakfast,Harbor,GameOver];
print """ You are drinking your coffe and reading the paper
when you see Xio Lynn walking past you
1.Finish Breakfast
2.Follow Him
3.Ask for time"""
command = input("What do you do:")
return next_room[command-1]

def Harbor():
next_room=[Dock,Gun]
print """You follow him to the harbor. Xio stands there and waits.
You see a guy walking towards Xio they start talking
1.Wait
2.Cap them"""
command = input("Your next move:")
return next_room[command-1]

def Dock():
print """You wait for Xio. The other guy jumps off the dock onto his boat.
he speeds away on the boat. Xio walk towards you
1. Cap him
2. Posion him
3. Follow him"""
command = input("What next:")
return GameOver;

def Gun():
print """you wait untill no one is looking. Nows your chance which
gun do you want to use?
1.Sniper
2.Silenced Pistol"""
command = input("Which gun:")
return GameOver;

def Breakfast():
next_room=[Hideout,Airport];
print """You finish your breakfast
1.Go to Xio's hideout
2.Go to Airport"""
command = input("What next:")
return next_room[command-1]

def Hideout():
print """You go to Xio Lynn's hideout in the warehouse district.
The only visible entrance is through a semi-dark ally.
There is some lighting along the way but the rest is all dark.
1. Climb roof
2. Hide in ally
3. Sneak into hideout"""
command = input("Your Action:")
return GameOver;

def Airport():
print """You go to the airport and find which flight Xio is
expected to go on.
1.Wait
2.Go to hideout"""
command = input("What's your next move:")
return GameOver;

#GAME LOOP
room=Morning;#Start in room "Morning"
while room!=None:#While we are in a room(game is not over)
print ""
room=room();#Execute the current room and get the next room



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!