|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +# Replace RPG starter project with this code when new instructions are live |
| 4 | + |
| 5 | +def showInstructions(): |
| 6 | + #print a main menu and the commands |
| 7 | + print(''' |
| 8 | +RPG Game |
| 9 | +======== |
| 10 | +Commands: |
| 11 | + go [direction] |
| 12 | + get [item] |
| 13 | +''') |
| 14 | + |
| 15 | +def showStatus(): |
| 16 | + #print the player's current status |
| 17 | + print('---------------------------') |
| 18 | + print('You are in the ' + currentRoom) |
| 19 | + #print the current inventory |
| 20 | + print('Inventory : ' + str(inventory)) |
| 21 | + #print an item if there is one |
| 22 | + if "item" in rooms[currentRoom]: |
| 23 | + print('You see a ' + rooms[currentRoom]['item']) |
| 24 | + print("---------------------------") |
| 25 | + |
| 26 | +#an inventory, which is initially empty |
| 27 | +inventory = [] |
| 28 | + |
| 29 | +#a dictionary linking a room to other rooms |
| 30 | +## A dictionary linking a room to other rooms |
| 31 | +rooms = { |
| 32 | + |
| 33 | + 'Hall' : { |
| 34 | + 'south' : 'Kitchen', |
| 35 | + 'east' : 'Dining Room', |
| 36 | + 'item' : 'key' |
| 37 | + }, |
| 38 | + |
| 39 | + 'Kitchen' : { |
| 40 | + 'north' : 'Hall', |
| 41 | + 'item' : 'monster', |
| 42 | + }, |
| 43 | + 'Dining Room' : { |
| 44 | + 'west' : 'Hall', |
| 45 | + 'south': 'Garden', |
| 46 | + 'item' : 'potion', |
| 47 | + 'north' : 'Pantry', |
| 48 | + }, |
| 49 | + 'Garden' : { |
| 50 | + 'north' : 'Dining Room' |
| 51 | + }, |
| 52 | + 'Pantry' : { |
| 53 | + 'south' : 'Dining Room', |
| 54 | + 'item' : 'cookie', |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | +#start the player in the Hall |
| 59 | +currentRoom = 'Hall' |
| 60 | + |
| 61 | +showInstructions() |
| 62 | + |
| 63 | +#loop forever |
| 64 | +while True: |
| 65 | + |
| 66 | + showStatus() |
| 67 | + |
| 68 | + #get the player's next 'move' |
| 69 | + #.split() breaks it up into an list array |
| 70 | + #eg typing 'go east' would give the list: |
| 71 | + #['go','east'] |
| 72 | + move = '' |
| 73 | + while move == '': |
| 74 | + move = input('>') |
| 75 | + |
| 76 | + # split allows an items to have a space on them |
| 77 | + # get golden key is returned ["get", "golden key"] |
| 78 | + move = move.lower().split(" ", 1) |
| 79 | + |
| 80 | + #if they type 'go' first |
| 81 | + if move[0] == 'go': |
| 82 | + #check that they are allowed wherever they want to go |
| 83 | + if move[1] in rooms[currentRoom]: |
| 84 | + #set the current room to the new room |
| 85 | + currentRoom = rooms[currentRoom][move[1]] |
| 86 | + #there is no door (link) to the new room |
| 87 | + else: |
| 88 | + print('You can\'t go that way!') |
| 89 | + |
| 90 | + #if they type 'get' first |
| 91 | + if move[0] == 'get' : |
| 92 | + #if the room contains an item, and the item is the one they want to get |
| 93 | + if "item" in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']: |
| 94 | + #add the item to their inventory |
| 95 | + inventory += [move[1]] |
| 96 | + #display a helpful message |
| 97 | + print(move[1] + ' got!') |
| 98 | + #delete the item from the room |
| 99 | + del rooms[currentRoom]['item'] |
| 100 | + #otherwise, if the item isn't there to get |
| 101 | + else: |
| 102 | + #tell them they can't get it |
| 103 | + print('Can\'t get ' + move[1] + '!') |
| 104 | + |
| 105 | + ## Define how a player can win |
| 106 | + if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory: |
| 107 | + print('You escaped the house with the ultra rare key and magic potion... YOU WIN!') |
| 108 | + break |
| 109 | + |
| 110 | + ## If a player enters a room with a monster |
| 111 | + elif 'item' in rooms[currentRoom] and 'monster' in rooms[currentRoom]['item']: |
| 112 | + print('A monster has got you... GAME OVER!') |
| 113 | + break |
0 commit comments