Advertisement
Guest User

Python Simple Text Adventure

a guest
Jun 30th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. # Simple Text Adventure Skeleton Code for Python
  2.  
  3. class Player:
  4.     def __init__(self, name, inventory):
  5.         self.name = name
  6.         self.inventory = inventory
  7.  
  8. class Room:
  9.     def __init__(self, name, description, items):
  10.         self.name = name
  11.         self.description = description
  12.         self.items = items
  13.     def remove_item(self, item):
  14.         self.items.remove(item)
  15.  
  16. # Print title screen
  17. print "+------------------------------------------------+"
  18. print "| Simple Text Adventure Skeleton Code for Python |"
  19. print "+------------------------------------------------+"
  20. print
  21.  
  22. # Build player
  23. name = raw_input("Please enter your name: ")
  24. inventory = []
  25. player = Player(name, inventory)
  26. print "Hello, {}.".format(player.name)
  27.  
  28. # Build rooms and load into map
  29. map_width, map_height = 2, 2
  30. world = [[Room("", "", "") for x in range(map_width)] for y in range(map_height)]
  31. world[0][0] = Room("Bedroom", "You are in your bedroom.", ["wallet", "keys"])
  32. world[0][1] = Room("Bathroom", "You are in the bathroom.", ["magazine", "toilet paper"])
  33. world[1][0] = Room("Kitchen", "You are in the kitchen.", ["apple", "chainsaw"])
  34. world[1][1] = Room("Garage", "You are in the garage", ["car", "gasoline"])
  35.  
  36. def move(userinput, rooms, x, y):
  37.     if userinput == "n":
  38.         if y > 0:
  39.             y -= 1
  40.         else:
  41.             print "You can't go that way."
  42.     elif userinput == "s":
  43.         if y < map_height - 1:
  44.             y += 1
  45.         else:
  46.             print "You can't go that way."
  47.     elif userinput == "e":
  48.         if x > 0:
  49.             x -= 1
  50.         else:
  51.             print "You can't go that way."
  52.     elif userinput == "w":
  53.         if x < map_width - 1:
  54.             x += 1
  55.         else:
  56.             print "You can't go that way."
  57.     return x, y
  58.  
  59. def get(inventory, item, room):
  60.     if item in room.items:
  61.         inventory.append(item)
  62.         print "You pick up the {}.".format(item)
  63.         room.remove_item(item)
  64.     else:
  65.         print "You don't see that here."
  66.  
  67. # Game loop
  68. x, y = 0, 0
  69. print world[x][y].name + ": " + world[x][y].description
  70. print "You see: {}".format(world[x][y].items)
  71. playing = True
  72. while playing:
  73.     userinput = raw_input("> ")
  74.     if userinput in ["n", "e", "s", "w"]:
  75.         x, y = move(userinput, world, x, y)
  76.         print world[x][y].name + ": " + world[x][y].description
  77.         print "You see: " + str(world[x][y].items)
  78.     elif userinput == "look":
  79.         print world[x][y].name + ": " + world[x][y].description
  80.         print "You see: " + str(world[x][y].items)
  81.     elif userinput[:4] == "get ":
  82.         item = userinput[4:]
  83.         get(player.inventory, item, world[x][y])
  84.     elif userinput == "i":
  85.         print "Inventory: {}".format(player.inventory)
  86.     elif userinput == "help":
  87.         print "Type n/e/s/w to move your player"
  88.         print "Type i to view your inventory"
  89.         print "Type get + the item to pick up an item"
  90.         print "Type quit to quit the game"
  91.     elif userinput == "quit":
  92.         break
  93.     else: print "I don't understand."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement