On 7/7/2012 5:57 AM myles broomes said...

I am currently coding a 'text-based adventure game', and im having a bit
of trouble with while loops. Here is my code so far:

Please paste in the traceback you're getting, and please set your mail client program for plain text when posting. What I see here has the indentation stripped out of your script so that before I can even test your code I'll have to guess at how you have it indented, and I'm more likely than you to get it right and may inadvertently fix your bug and never know it even before getting a chance to see the bug.

Emile



#Text-based Adventure RPG
#The player travels through different towns and dungeons
#The overall goal of the game is simple; the player must make it to the
final town,
#Battling various monsters and overcoming several challenges along the way
import time
player_name = None
player_invent = [30]
emb_town_visit=None
YES='yes'
NO='no'
user_choice=None
plyr_location='Embark'
shopkeep_mny=100
shopkeep_invent=['Bronze Sword (10)','Steel Sword (100)','Rounded Shield
(10)','Kite Shield (50)','Medicine (5)','Mana Potion (5)']

class Main_char(object):
"""A class for the main character object."""
def __init__(self,name,invent,level=1,health=30,mana=15):
self.name=name
self.level=level
self.invent=invent
self.hp=health
self.mana=mana
print("\nYour journey begins here in Embark town,"+self.name)
def __str__(self):
print("\nName: ",self.name, "\nLevel: ",self.level, "\nYour inventory:
",self.invent)
def town_chcklist(self):
"""A checklist showing a list of the towns the player has already
visited."""
self.checklist=[]
if town1==YES:
self.checklist.append(town1)

def use_item(item):
"""Allows the player to use an item in their inventory."""
if item in ('Medicine','Mana Potion'):
Hero.invent.delete(item)
if item=='Medicine':
Hero.hp+20
elif item=='Mana Potion':
Hero.mana+10
class Shopkeep(object):
"""A blueprint for the shopkeeper objects."""
def __init__(self,inv,money):
self.inv=inv
self.money=money
def __str__(self):
print("Shopkeeper: Here are my wares: \n\n",self.inv+". \nI currently
have",self.money,"gold.")

#Create the towns/ dungeons/ areas in different functions
def Embark():
"""The town in which the player begins there journey."""
if emb_town_visit==None:
print("Welcome to Embark Town",player_name+"!")
while True:
print("\n",hero)
print("""
\n\t\tPress '1' to exit the town...
\n\t\tPress '2' to enter the local shop...""")
user_choice=input('What would you like to do?: ')
if user_choice=='1':
shop()
continue
elif user_choice=='2':
if emb_town_visit==None:
print("\n\t\t\tAs you leave your home of Embark Town, you look back with
a smile, then proceed North.")
break
else:
print("\n\t\t\tThat is not a valid choice...")
continue

#The player has visited Embark Town
if emb_town_visit==None:
emb_town_visit=YES
#Exit the function and change the players location variable
return 'Left Embark Town',emb_town_visit

def shop():
"""A generic shop that is placed in each town."""
while True:
print("""
\nWhat would you like to do?
\nPress '1' to buy an item...
\n...Press '2' to exit the shop...
\n...Or press '3' to ask about the town...""")
user_choice=input(">")
if user_choice=='1':
print("Shopkeeper: Goodbye.")
break
elif user_choice=='2':
print(emb_shopkeep)
user_choice=None
print("Enter the name of the item you would like to purchase.")
user_choice=title(input(">"))
for item in emb_shopkeep.inv:
if user_choice in emb_shopkeep.inv:
message=handle_pur(user_choice)
print(message)
emb_shopkeep.inv.delete(user_choice)
else:
print("\n\t\t\tThat is not a valid choice!")
def handle_pur(item):
"""Handles purhchases made by the player in the shop."""
if item=='Bronze Sword':
if Hero.invent[0] >= 10:
Hero.invent[0]-10
Hero.invent.append(item)
msg='You now own a',item
elif Hero.invent[0] < 10:
msg='You cannot afford that item.'
elif item=='Steel Sword':
if Hero.invent[0] >= 100:
Hero.invent[0] - 100
Hero.invent.append(item)
msg='You now own a',item
elif Hero.invent[0] < 100:
msg='You cannot afford that item.'
elif item =='Rounded Shield':
if Hero.invent[0] >= 10:
Hero.invent[0] - 10
Hero.invent.append(item)
msg='You now own a',item
elif Hero.invent < 10:
msg='You cannot afford that item.'
elif item=='Kite Shield':
if Hero.invent[0] >= 50:
Hero.invent[0] - 50
Hero.invent.append(item)
msg='You now own a',item
elif Hero.invent < 50:
msg='You cannot afford that item.'
elif item=='Medicine':
if Hero.invent[0] >= 5:
Hero.invent[0] - 5
Hero.invent.append(item)
msg='You now own a',item
elif Hero.invent[0] < 5:
msg='You cannot afford that item.'
elif item=='Mana Potion':
if Hero.invent[0] >= 5:
Hero.invent[0] - 5
Hero.invent.append(item)
msg='You now own a',item
elif Hero.invent[0] < 5:
msg='You cannot afford that item.'
#Informs the program of which message to tell the player
return msg

emb_shopkeep=Shopkeep(shopkeep_invent,shopkeep_mny)

#Player creation loop
while True:
print("""
\nValiant hero, your quest begins here in Embark Town.""",
time.sleep(200),
"""\nWhere you will go is up to you...""",
time.sleep(200),
"""\nHow your adventure will pan out is up to you...""",
time.sleep(200),
"""\nBut first...""",
time.sleep(200),
"""\n...tell me your name...""")
player_name=title(input(">"))
print("\nAh! So your name is",player_name,"- a truly heroic name!")
Hero=Main_char(player_name,player_invent)
break

def main():
"""Main game loop."""
while True:
if plyr_location=='Embark':
plyr_location,emb_town_visit=Embark()
elif plyr_location=='Left Embark Town':
return

#Launch the game
main()

For some reason that I cant figure out, the 'Player creation loop' wont
run when I try to run the program. When I try to run it in IDLE, nothing
happens, Any help is much appreciated.

Myles Broomes


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to