7-3+Project+2+-+TextBasedGame.py
docx
keyboard_arrow_up
School
Southern New Hampshire University *
*We aren’t endorsed by this school
Course
140
Subject
Industrial Engineering
Date
Dec 6, 2023
Type
docx
Pages
4
Uploaded by UltraBearPerson902
Robert Barr
Nancy Hill
IT-140
October 15, 2023
TextBasedGame.py
# Robert Barr
import random
# Define the rooms and their connection
rooms = {
'bar'
: [
'vault room'
, 'bedroom'
],
'vault room'
: [
'bar'
, 'keg room'
],
'keg room'
: [
'vault room'
, 'attic'
],
'bedroom'
: [
'bar'
, 'basement'
],
'attic'
: [
'keg room'
, 'secret room'
],
'basement'
: [
'bedroom'
],
'secret room'
: [
'attic'
],
}
# Define the items
items = [
'key'
, 'sword'
, 'book'
, 'potion'
, 'torch'
, 'shield'
]
# Randomly place items in rooms
room_items = {room: random.sample(items, random.randint(
1
, 2
)) for room in rooms.keys()}
# Initialize the player's location
current_room = 'bar'
inventory = []
# Place the banshee in a random room
banshee_room = random.choice(
list
(rooms.keys()))
# Function to show game instructions
def show_instructions
():
print
(
"Red Dragon Inn Text Adventure Game"
)
print
(
"Collect 6 items to win the game, or be consumed by the banshee."
)
print
(
"Move commands: go 'room name'"
)
print
(
"Add to Inventory: get 'item name'"
)
# Function to display the player's status
def display_status
():
print
(
f'You are in the {
current_room
}
.'
)
if room_items[current_room]:
print
(
f'You see the following items: {
", "
.join(room_items[current_room])
}
'
)
if current_room == banshee_room:
print
(
"A banshee is in this room! You need to defeat it."
)
if 'sword' in inventory:
print
(
'You have a sword in your inventory.'
)
# Main game loop
while True:
show_instructions()
display_status()
action = input
(
'What do you want to do? '
).lower()
if action == 'quit'
:
print
(
'Thanks for playing!'
)
break
if action.startswith(
'go '
):
direction = action.split()[
1
]
if direction in rooms[current_room]:
current_room = direction
else
:
print
(
"You can't go there from here."
)
elif action.startswith(
'get '
):
item = action.split()[
1
]
if item in room_items[current_room]:
inventory.append(item)
room_items[current_room].remove(item)
print
(
f'You have taken the {
item
}
.'
)
else
:
print
(
"That item is not in this room."
)
if len
(inventory) == 6
:
print
(
"Congratulations! You've collected all the items and won the game."
)
print
(
"Thank you for playing!"
)
break
elif action == 'use' and banshee_room == current_room:
if 'sword' in inventory:
print
(
'You have defeated the banshee with the sword. Congratulations, you win!'
)
print
(
"Thank you for playing!"
)
break
else
:
print
(
'You need a sword to defeat the banshee.'
)
else
:
print
(
'Invalid action. Please enter go, get, use, or quit.'
)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help