IT 140 Textbased GAME PDF

Title IT 140 Textbased GAME
Author Faiza Salman
Course Perspectives in the Humanities
Institution Southern New Hampshire University
Pages 2
File Size 38.7 KB
File Type PDF
Total Downloads 44
Total Views 149

Summary

FULL FUNCTIONING CODE OF THE TEXTBASED GAME OF THE COURSE IT-140...


Description

def show_instructions(): # print intro and instructions print("The Fate of Sarah Adventure Game") print("Collect all 6 items to win the game, or be killed by the Predator.") print("Move commands: go South, go North, go East, go West") print("Add to Inventory: get 'item name'") def showStatus(currentRoom, inventory, rooms): # move to corresponding room and collect items from inventory print(' ---------------------------') print('You are in the ' + currentRoom) print('Inventory : ' + str(inventory)) if "item" in rooms[currentRoom]: # give user command to grab the item present in the room print('You see a ' + rooms[currentRoom]['item']) print('Get your item') inventory = []

# initialize an empty inventory list

# declare the dictionary for rooms and items rooms = { 'FamilyRoom': {'South': 'Study', 'North': 'MasterBedroom', 'East': 'Kitchen', 'West': 'Foyer', 'item': 'Charger'}, 'MasterBedroom': {'East': 'Closet', 'South': 'FamilyRoom', 'item': 'Cellphone'}, 'Closet': {'West': 'MasterBedroom', 'item': 'Coat'}, 'Foyer': {'East': 'FamilyRoom', 'item': 'Shoes'}, 'Study': {'North': 'FamilyRoom', 'East': 'Basement', 'item': 'Umbrella'}, 'Basement': {'West': 'Study', 'item': 'Predator'}, # villain 'Kitchen': {'North': 'DiningRoom', 'West': 'FamilyRoom', 'item': 'Knife'}, 'DiningRoom': {'South': 'Kitchen'} } currentRoom = 'FamilyRoom' # starting room show_instructions() # calling function def main(): global inventory, currentRoom while True: # gameplay loop showStatus(currentRoom, inventory, rooms) if "item" in rooms[currentRoom]: if rooms[currentRoom]['item'] == 'Predator': # set 'losing case' scenario using if condition print( 'HAHAHA Sarah, you did not collect all the items before coming my way. Your life is in danger now!') print('You have lost the game!')

break if len(inventory) == 6: # set 'winning case' scenario using if condition print('"Yay Sarah! You have collected all the items!"') print('Congratulations Sarah! You won and you are safe now.') print('Thank you for playing!') break print(" ---------------------------") print("Enter your move: ") move = '' while move == '': # set while loop for moving to different rooms, and get user input move = input() move = move.split() if len(move) != 2:

# handle an invalid command using if/else

condition print("Invalid Input!") continue elif move[0].title() == 'Go': # tell the user of their current room if move[1].title() in rooms[currentRoom]: currentRoom = rooms[currentRoom][move[1].title()] else: print("You can't go that way!") # if user moves in wrong direction elif move[0].title() == 'Get': # tell the user to get an item in the room from inventory if "item" in rooms[currentRoom] and move[1].title() in rooms[currentRoom]['item']: inventory += [move[1].title()] print('You got', move[1].title() + '!') del rooms[currentRoom]['item'] else: print("Can't get " + move[1].title() + "!") # handle an invalid command using if/else condition else: print("Invalid move!") print("Hope you enjoyed playing.") main()

# print the final message...


Similar Free PDFs