Advertisement
selib

Text Adventure Python

Nov 20th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.11 KB | None | 0 0
  1. #Text Adventure
  2.  
  3. import shlex
  4.  
  5.  
  6.  
  7. def map():
  8.     print ' ______________________________'
  9.     print '|         |         |          |'
  10.     print '|         |         |          |'
  11.     print '| Room B  = Room D  =  Room E  |'
  12.     print '|   Jack  |  Sword  |   Bear   |'
  13.     print '|         |         |          |'
  14.     print '|____||___|____||___|__________|'
  15.     print '|         |         |'
  16.     print '|         |         |'
  17.     print '|  Steve  |  Jesse  |'
  18.     print '| Room A  = Room C  |'
  19.     print '|         |         |'
  20.     print '|_________|_________|'
  21.  
  22.  
  23.  
  24.  
  25. class Room(object):
  26.     def __init__(self, location,itemsinRoom, description,descriptionpeople,descriptiondirections):
  27.  
  28.         self.location = location
  29.         self.itemsinRoom = itemsinRoom
  30.         self.description = description
  31.         self.descriptionpeople = descriptionpeople
  32.         self.descriptiondirections = descriptiondirections
  33.  
  34.  
  35.  
  36.     def display_Room(self):
  37.         stri = (' ')
  38.  
  39.         itemprint = []
  40.         for item in self.itemsinRoom:
  41.             itemprint.append('A ' + item.name + ' is here.')
  42.  
  43.         rtr = stri.join(itemprint)
  44.  
  45.         return self.description + self.descriptionpeople + rtr
  46.  
  47.  
  48.     def printobejcts(self):
  49.         pass
  50.  
  51.  
  52. class People(object):
  53.     def __init__(self,location,name,text,description,state):
  54.         self.location = location
  55.         self.name = name
  56.         self.text = text
  57.         self.description = description
  58.         self.state = state
  59.  
  60.  
  61.     def printText(self):
  62.         print self.text
  63.  
  64.  
  65.  
  66.     def go(self,location,Decision):
  67.  
  68.         newlocation = list(self.location)
  69.  
  70.  
  71.         if Decision == 'go north' or Decision == 'go n' or Decision == 'n':
  72.             newlocation[1] += 1
  73.             test = world.get(tuple(newlocation))
  74.             if test == None:
  75.                 print 'You cannot go there'
  76.             else:
  77.                 Ply.location = tuple(newlocation)
  78.         elif Decision == 'go south' or Decision == 'go s' or Decision == 's':
  79.             newlocation[1] -= 1
  80.             test = world.get(tuple(newlocation))
  81.             if test == None:
  82.                 print 'You cannot go there'
  83.             else:
  84.                 Ply.location = tuple(newlocation)
  85.  
  86.         elif Decision == 'go east' or Decision == 'go e' or Decision == 'e':
  87.             newlocation[0] += 1
  88.             test = world.get(tuple(newlocation))
  89.             if test == None:
  90.                 print 'You cannot go there'
  91.             else:
  92.                 Ply.location = tuple(newlocation)
  93.  
  94.         elif Decision == 'go west' or Decision == 'go w' or Decision == 'w':
  95.             newlocation[0] -= 1
  96.             test = world.get(tuple(newlocation))
  97.             if test == None:
  98.                 print 'You cannot go there'
  99.             else:
  100.                 Ply.location = tuple(newlocation)
  101.  
  102.  
  103. class Player(People):
  104.     def __init__(self,location,name,text,state):
  105.         self.location = location
  106.         self.name = name
  107.         self.text = text
  108.         self.state = state
  109.  
  110.  
  111.  
  112.     def printlocation(self,location):
  113.         return world.get(self.location).display_Room()
  114.  
  115.  
  116.     def take(self,location,Decision):
  117.         tokens = shlex.split(Decision)
  118.         if len(tokens) != 2 or tokens[0] != 'take' or tokens[1] not in objects:
  119.             print "This doesn't exist"
  120.  
  121.         else:
  122.             if Ply.location == objects[tokens[1]].location:
  123.                 if objects[tokens[1]].ininv == 'no':
  124.                     Inventory.append(objects[tokens[1]].name)
  125.                     print 'Taken'
  126.                     objects[tokens[1]].ininv = 'yes'
  127.                     world.get(self.location).itemsinRoom.remove(objects[tokens[1]])
  128.  
  129.                 elif objects[tokens[1]].ininv == 'no':
  130.                     print "You already have that"
  131.  
  132.             else:
  133.                 print "You don't see that"
  134.  
  135.  
  136.  
  137.  
  138.     def drop(self,location,Decision):
  139.         tokens = shlex.split(Decision)
  140.         if len(tokens) != 2 or tokens[1] not in Inventory:
  141.             print "You don't have that"
  142.         else:
  143.             objects[tokens[1]].ininv = 'no'
  144.             Inventory.remove(objects[tokens[1]].name)
  145.             objects[tokens[1]].location == self.location
  146.             world.get(self.location).itemsinRoom.append(objects[tokens[1]])
  147.  
  148.  
  149.  
  150.  
  151.  
  152.     def talk(self,location,Decision):
  153.         if Decision == 'talk steve' or Decision == 'talk to steve' or Decision == 'talk with steve':
  154.             if self.location == Steve.location:
  155.                 if Jack.state == 'dead':
  156.                     if gold.ininv == 'no':
  157.                         print 'You killed Jack! Take this is a reward.'
  158.                         print 'Gold added to Inventory'
  159.                         Inventory.append(gold.name)
  160.                     elif gold.ininv == 'yes':
  161.                         print 'Thanks again, man.'
  162.                 elif Steve.state == 'dead':
  163.                     print 'This person is dead.'\
  164.  
  165.                 else:
  166.                     Steve.printText()
  167.             else:
  168.                 print "This person is not here."
  169.  
  170.         elif Decision == 'talk jack' or Decision == 'talk to jack' or Decision == 'talk with jack':
  171.             if self.location == Jack.location:
  172.                 if Jack.state == 'dead':
  173.                     print 'This person is dead.'
  174.                 else:
  175.                     Jack.printText()
  176.             else:
  177.                 print 'This person is not here'
  178.  
  179.         elif Decision == 'talk jesse' or Decision == 'talk to jesse' or Decision == 'talk with jesse':
  180.             if self.location == Jesse.location:
  181.                 if Jesse.state == 'dead':
  182.                     print 'this person is dead'
  183.                 elif 'gold' in Inventory:
  184.                     Drug = raw_input('You wanna buy some drugs?')
  185.                     if Drug == 'no':
  186.                         print 'THEN FUCK OFF'
  187.                     elif Drug == 'yes':
  188.                         Inventory.remove('gold')
  189.                         Inventory.append(meth.name)
  190.                         print 'Gold removed from Inventory'
  191.                         print 'Meth added to Inventory'
  192.                 else:
  193.                     Jesse.printText()
  194.             else:
  195.                 print 'This person is not here'
  196.  
  197.  
  198.         elif Decision == 'talk bear' or Decision == 'talk to bear' or Decision == 'talk with bear':
  199.             if self.location == Bear.location:
  200.                 if Bear.state == 'dead':
  201.                     print 'this monster is dead'
  202.                 else:
  203.                     Bear.printText()
  204.                     print 'The bear killed you'
  205.                     print 'Game over'
  206.                     Ply.state = 'dead'
  207.             else:
  208.                 print 'This thing is not here'
  209.  
  210.  
  211. class Item(object):
  212.     def __init__(self,name,text,location,cankill,ininv):
  213.         self.name = name
  214.         self.text = text
  215.         self.location = location
  216.         self.cankill = cankill
  217.         self.ininv = ininv
  218.  
  219.  
  220.  
  221. def kill(Decision):
  222.     tokens = shlex.split(Decision)
  223.     if len(tokens) == 2:
  224.         if tokens[1] in people:
  225.             if Ply.location == people[tokens[1]].location:
  226.                 killhelp(tokens[1])
  227.             else:
  228.                 print 'This person is not here.'
  229.         else:
  230.             print 'This person does not exist'
  231.     elif len(tokens) == 4:
  232.         if tokens[2] == 'with':
  233.             if tokens[1] in people:
  234.                 if Ply.location == people[tokens[1]].location:
  235.                     killhelpwithitem(tokens[3],tokens[1])
  236.                 else:
  237.                     print 'This person is not here.'
  238.             else:
  239.                 print 'This person does not exist'
  240.         else:
  241.             print "This won't work"
  242.     else:
  243.         print "This doesn't work"
  244.  
  245.  
  246. def killhelp(victim):
  247.     Dec = raw_input('With what?')
  248.     Dec2 = Dec.lower()
  249.     if Dec2 in objects:
  250.         if Dec2 in Inventory:
  251.             if objects[Dec2].cankill == 'yes':
  252.                 killcons(victim)
  253.             else:
  254.                 print "You can't kill with that!"
  255.         else:
  256.             print "You don't have that."
  257.     else:
  258.         print "This doesn't exist."
  259.  
  260.  
  261. def killhelpwithitem(Item,victim):
  262.     if Item in objects:
  263.         if Item in Inventory:
  264.             if objects[Item].cankill == 'yes':
  265.                 killcons(victim)
  266.             else:
  267.                 print "You can't kill with that!"
  268.         else:
  269.             print "You don't have that."
  270.     else:
  271.         print "This doesn't exist."
  272.  
  273.  
  274. def killcons(victim):
  275.     if victim == 'steve':
  276.         Steve.state = 'dead'
  277.         print 'Steve died'
  278.         RoomA.descriptionpeople = "You see Steve's dead body. "
  279.     if victim == 'jack':
  280.         Jack.state = 'dead'
  281.         print 'Jack died'
  282.         RoomB.descriptionpeople = "You see Jack's dead body. "
  283.     if victim == 'jesse':
  284.         Jesse.state = 'dead'
  285.         print 'Jesse died'
  286.         RoomD.descriptionpeople = "You see Jesse's dead body. "
  287.     if victim == 'myself':
  288.         print 'You killed yourself. Good job, Really. Congratulations.'
  289.         print 'Game over'
  290.         Ply.state = 'dead'
  291.     if victim == 'bear':
  292.         print 'YOU CANNOT KILL A BEAR GODDAMNIT'
  293.  
  294.  
  295. def inv():
  296.     stri = ', '
  297.     print 'You have ' + stri.join(Inventory)
  298.  
  299.  
  300. def exam(Decision):
  301.     tokens = shlex.split(Decision)
  302.     if len(tokens) != 2 or tokens[0] != 'exam' or tokens[1] not in objects:
  303.         print "This doesn't exist"
  304.     else:
  305.         if Ply.location == objects[tokens[1]].location or tokens[1] in Inventory:
  306.             print objects[tokens[1]].text
  307.         else:
  308.             print "You don't see this"
  309.  
  310.  
  311. def dodrugs(Decision):
  312.     if Decision == 'do meth' or 'do drugs':
  313.             if 'meth' in Inventory:
  314.                 print 'YOU ARE HIGH'
  315.                 Inventory.remove('meth')
  316.                 meth.ininv = 'no'
  317.             else:
  318.                 print "You don't have that"
  319.  
  320.     else:
  321.         print "things"
  322.  
  323.  
  324. def helpp():
  325.     print "You can use the following commands:"
  326.     print "go (north,south,west,east)"
  327.     print "talk (person)"
  328.     print "kill (person) with (item)"
  329.     print "take (item)"
  330.     print "inventory"
  331.     print "examine (item)"
  332.     print "quit"
  333.  
  334.  
  335. Inventory = []
  336.  
  337.  
  338. sword = Item('sword','This is a sword',(2,2),'yes','no')
  339. gold = Item('gold','This is gold',(1,1),'no','no')
  340. meth = Item('meth','THIS IS SOME REALLY NICE BLUE METH',(1,2),'no','no')
  341.  
  342.  
  343. Ply = Player((1,1),'Player','','alive')
  344. Steve = People((1,1),'Steve',"Hi, I'm Steve. Dude, can you do me favor? Kill Jack.",'This is Steve','alive')
  345. Jack = People((1,2),'Jack',"Hi, I'm Jack.",'This is Jack','alive')
  346. Jesse = People((2,1),'Steve','Piss off, bitch.','This is Jesse','alive')
  347. Bear = People((3,2),'Bear','RAWWWRRR','THIS IS A FUCKING BEAR','alive')
  348.  
  349. itemsinroomA = []
  350. itemsinroomB = []
  351. itemsinroomC = [sword]
  352. itemsinroomD = []
  353. itemsinroomE = []
  354.  
  355.  
  356. RoomA = Room((1,1),itemsinroomA,'You are in Room A.',' You see Steve. ',\
  357. ' You see a door to the north and to the east.')
  358. RoomB = Room((1,2),itemsinroomB,'You are in Room B.',' You see Jack. ',\
  359. ' You see a door to the south and to the east.')
  360. RoomC = Room((2,2),itemsinroomC,'You are in Room C.','',\
  361. ' You see a door to the west, to the east and to the south.')
  362. RoomD = Room((2,1),itemsinroomD,'You are in Room D.',' You see Jesse. ',\
  363. ' You see a door to the north and to the west.')
  364. RoomE = Room((3,2),itemsinroomE,'You are in Room E.',' You see a bear. ',\
  365. ' You see a door to the west.')
  366.  
  367.  
  368. world = {
  369. (1,1):RoomA,
  370. (1,2):RoomB,
  371. (2,2):RoomC,
  372. (2,1):RoomD,
  373. (3,2):RoomE
  374. }
  375.  
  376.  
  377. objects = {
  378. 'sword': sword,
  379. 'gold': gold,
  380. 'meth': meth
  381.  
  382. }
  383.  
  384.  
  385. people = {
  386. 'steve': Steve,
  387. 'jack': Jack,
  388. 'jesse': Jesse,
  389. 'bear' : Bear,
  390. 'myself': Ply
  391.  
  392. }
  393.  
  394.  
  395.  
  396. emptyline = '\n'
  397.  
  398.  
  399. def main():
  400.     print "Welcome to my Text Adventure!"
  401.     print "Use the 'help' command for help!"
  402.     while Ply.state == 'alive':
  403.  
  404.         print Ply.printlocation(Ply.location)
  405.         Decisionst = raw_input('>')
  406.         Decisionstr = Decisionst.lower()
  407.         lst = shlex.split(Decisionstr)
  408.         if lst[0] == 'go' or lst[0] in 'nwse':
  409.             Ply.go(Ply.location,Decisionstr)
  410.         elif lst[0] == 'take':
  411.             Ply.take(Ply.location, Decisionstr)
  412.         elif lst[0] == 'drop':
  413.             Ply.drop(Ply.location,Decisionstr)
  414.         elif lst[0] == 'quit':
  415.             break
  416.         elif lst[0] == 'talk':
  417.             Ply.talk(Ply.location, Decisionstr)
  418.         elif lst[0] == 'kill':
  419.             kill(Decisionstr)
  420.         elif lst[0] == 'die':
  421.             killcons('myself')
  422.         elif lst[0] == 'inventory' or lst[0] == 'inv' or lst[0] == 'i':
  423.             inv()
  424.         elif lst[0] == 'help':
  425.             helpp()
  426.         elif lst[0] == 'win':
  427.             print 'nice try'
  428.         elif lst[0] == 'shout':
  429.             print 'AAARGGGGGGHHHHH'
  430.         elif lst[0] == 'look':
  431.             Ply.printlocation(Ply.location)
  432.         elif lst[0] == 'examine' or 'exam':
  433.             exam(Decisionstr)
  434.         elif lst[0] == 'do drugs' or 'do meth':
  435.             dodrugs()
  436.         else:
  437.             print 'This doesnt work'
  438.  
  439.  
  440. map()
  441. main()
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448. #TEST CODE
  449. """
  450. """
  451.  
  452.  
  453. #IDEAS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement