Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Single User Dungeon
#1
Alright so i am total beginner here and I need help with what the code should look like to allow players to interact with other items in the room. I currently have a couple of items in each room and I would like for these items to help the player find the special item that they need in order to complete the game. I would love some suggestions as to what this should look like/where should I put this part of the code? I started to create a piece of this code at the bottom of my code, but it is not coming up when i run the code.


entry_way = ["Entry Way", ["car keys"], []]
living_room = ["Living Room", ["paintings", "DVDs", "nice couches"], []]
dining_room = ["Dining Room", ["chairs, table"], []]
kitchen = ["Kitchen", ["a group of people talking"], []]
bedroom = ["bedroom", ["tv, bed"],[]]
patio = ["patio", [special_item], []]

# Connect the rooms based on their relationship to
# North, South, West, East
entry_way[2] = [living_room, 0, 0, dining_room]
dining_room[2] = [0, 0, entry_way, kitchen]
living_room[2] = [0, entry_way, 0, bedroom]
kitchen[2] = [0, patio, dining_room, 0]
bedroom[2] = [0, dining_room, living_room, 0]
patio[2] = [kitchen,0,0,0]

# TODO #2 CREATE the body of print_message function
# print_message takes in a room and based on what's
# inside the room, will print out the appropriate
# message to describe the room
# For example, if room = ["Entry Way", ["car keys"], []]
# This function should print the following message:
# "You enter room Entry Way."
# "You see in this room: keys."
# You don't have to use those exact words, but make sure
# you print out the name of the room and the items in the room
def print_message(room):

   print("You are in room: " + room[0])


# Get the player's choice.
# Validate the choice by making sure it's a room they can go to
# Return a room based on the player's choice
def get_player_choice(rooms):

   # get the players choice of what direction they want to go
   choice = input("Where do you want to go? (N, S, W, E) ")
   print(" ")
   # we're going to translate the string input to a
   # number that means a particular direction
   # -1 is an invalid direction
   # 0 is north
   # 1 is south
   # 2 is west
   # 3 is east
   direction = -1

   while direction == -1:
       if choice == "N" or choice == "n":
           direction = 0
       elif choice == "S" or choice == "s":
           direction = 1
       elif choice == "W" or choice == "w":
           direction = 2
       elif choice == "E" or choice == "e":
           direction = 3

       if direction == -1:
           choice = input("Please enter N, S, W, E to move. ")
       elif rooms[direction] == 0:
           choice = input("You can't go in that direction. Try another direction (N, S, W, E): ")
           direction = -1

   return rooms[direction]


def main():

   # start the adventure
   # and let the player know what they're looking for
   #New Extension
   print("You are in a mad dash to get to the airport!")
   print("You are just about to pull out of the driveway when you realize that your " + special_item + " is no where insight.")
   print("You need to find it so you can catch your flight!")
   print("You look around the house to find " + special_item)
   print("Remember that each of your rooms can help you find it!")


   # initialize the current_room to be the starting room
   # initialize the item_found boolean variable to False
   current_room = entry_way
   item_found = False



   # TODO #3 CREATE the body of this while loop (below)
   # While the special item is being searched for,
   # print a description of the current_room and
   # check if the special item is in the current_room.
   # Otherwise ask the player to make a choice.
   # hint: you'll need to use the functions print_msg() and get_player_choice()
   #    and think about when you're in the room with the item or when you're not

   while not item_found:
       print_message(current_room)
       if special_item in current_room[1]:
           print("Congrats you found it!")
           print("Now go back to where you started.")
           item_found = True
       current_room = get_player_choice(current_room[2])




   # TODO #4 After finding the special item, the player must go back
   # to the room where the player started.
   # hint: a while loop will be helpful to do this

   while item_found:
       #once item found
       print_message(current_room)
       if current_room == entry_way:
           #win
           print("Thank you for finding my phone!")
           break
       current_room = get_player_choice(current_room[2])

#display the room item if there is one
def get_item(rooms):
 if "item" in rooms[current_room]:
   print("you see a " + rooms[current_room]["item"])

  


# call the main() function to start the program
main()
Reply
#2
Okay, you have the items in your room lists. First thing you want to do is show them to the user. In the print_message function, I would print out the items in the room. Something like:

print('You see the following items:', ', '.join(room[1]))
Then, in the get_player_choice function, allow them to get or drop items. Change the input text to "What do you want to do?". You're going to need a player inventory. This would work as another list, that you pass to the various functions. So, the player would enter something like 'get paintings'. You can parse that into the command (get) and the parameter (paintings). I find the partition method of strings to be good for that. Then check that the thing they want is in the room (with the in operator on the room's contents). If it is, remove it from room's contents and append it to the player's inventory. Dropping the item would work the same, but in reverse.

Check out the text adventure tutorial link in my signature. It has an example of a system just like that, only using dictionaries instead of lists.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Single User Dungeon kython 2 4,107 Mar-01-2017, 11:00 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020