I have now finished my Python Adventure Game by adding some puzzles and a win algorithm. Its a very simple game but was great fun to write.
Source code is below.
# # Barry's simple adventure game # # Room data indexs SHORT_DESC = 0 LONG_DESC = 1 N_EXIT = 2 S_EXIT = 3 E_EXIT = 4 W_EXIT = 5 U_EXIT = 6 D_EXIT = 7 OBJECT = 8 # Room Data rooms = [ ['dummy'], ['the entrance', 'the entrance to a rather large looking cave', 2, 0, 0, 0, 0, 0, ''], # 1 ['a coridor', 'a coridor that stretches off to the North', 3, 1, 0, 0, 0, 0, 'Lamp'], #2 ['a round room', 'a round room with many exits', 7, 2, 4, 0, 20, 6, ''], #3 ['a crawlway', 'a narrow east / west crawlway', 0, 0, 5, 3, 0, 0, ''], #4 ['a low cave', 'a low dead end cave', 0, 0, 0, 4, 0, 0, 'Knife'], #5 ['a well', 'a deep well with no way out', 0, 0, 0, 0, 0, 0, ''], #6 ['a crystal cave', 'a beautifull cave with crystal walls', 8, 3, 9, 0, 0, 0, ''], #7 ['an alcove', 'a small alcove in the cave wall', 0, 7, 0, 0, 0, 0, ''], #8 ['a passage', 'a long east / west passage', 0, 0, 10, 7, 0, 0, ''], #9 ['an anteroom', 'a richly decorated anteroom', 13, 11, 12, 9, 0, 0, ''], #10 ['a wardrobe', 'a large wardrobe with space for lots of clothes', 10, 0, 0, 0, 0, 0, 'Key'], #11 ['the throne room', 'a very large room with a golden throne on a dais', 0, 0, 0, 10, 0, 0, 'Crown'], #12 ['the view room', 'a cave with a small hole giving a view along a green valley', 0, 10, 14, 0, 0, 0, ''], #13 ['the guard room', 'a room with chairs and a table that looks like it was used as a guard room', 0, 0, 15, 13, 0, 0, ''], #14 ['the ballroom', ' a magnificent ballroom with purple and gold walls', 0, 17, 18, 14, 16, 0, ''], #15 ['the organ loft', 'a small room containing the controls for a large pipe organ', 0, 0, 0, 0, 0, 15, ''], #16 ['the vault', 'a small airtight vault with shelves around the sides', 15, 0, 0, 0, 0, 0, 'Diamond'], #17 ['the dining room', 'a long room with a huge dining table and chairs', 0, 0, 19, 15, 0, 0, ''], #18 ['the kitchen', 'a large dusty kitchen', 0, 0, 0, 18, 0, 0, ''], #19 ['a gallery', 'a high gallery leading off in a number of directions', 24, 22, 23, 21, 0, 3, ''], #20 ['the waverfall cave', 'a large cave with a dark lake and a waterfall at the far end', 0, 0, 20, 0, 0, 0, ''], #21 ['the echo cave', 'a small cave where sound echoes all around', 20, 0, 0, 0, 0, 0, ''], #22 ['the assasin room', 'a small room bare room', 0, 0, 25, 20, 0, 0, 'a dead Masked Man'], #23 ['a dark cave', 'a dark cave', 24, 24, 20, 24, 24, 24, ''], #24 ['the exit', 'the exit of the cave', 0, 0, 0, 23, 0, 0, ''] #25 ] objects = ['dummy'] visits = [0] for room in range(1, rooms.__len__()): objects.append(rooms[room][OBJECT]) visits.append(0) current_room = 1 visits[current_room] = 1 inventory = [] battery = 100 # Code def describe_room(room): print if visits[room] > 1: print 'You are in ' + rooms[room][SHORT_DESC] else: print 'You are in ' + rooms[room][LONG_DESC] print if rooms[room][N_EXIT] != 0: print 'There is an exit to the North' if rooms[room][S_EXIT] != 0: print 'There is an exit to the South' if rooms[room][E_EXIT] != 0: print 'There is an exit to the East' if rooms[room][W_EXIT] != 0: print 'There is an exit to the West' if rooms[room][U_EXIT] != 0: print 'There is an exit leading upwards' if rooms[room][D_EXIT] != 0: print 'There is an exit leading downwards' if objects[room] != '': print print 'There is a ' + objects[room] + ' here' print def move(exit): global current_room if rooms[current_room][exit] != 0: # Can't enter the vault without the key if ((current_room == 15) and (rooms[current_room][exit] == 17)): if 'Key' in inventory: print print print "You use the key to open the door to the vault" current_room = rooms[current_room][exit] visits[current_room] += 1 else: print print print "You can't open the heavy door but it does have a key hole" else: current_room = rooms[current_room][exit] visits[current_room] += 1 else: print print print '>>> There is no exit in that direction' def north(): move(N_EXIT) def south(): move(S_EXIT) def east(): move(E_EXIT) def west(): move(W_EXIT) def up(): move(U_EXIT) def down(): move(D_EXIT) def get(): if objects[current_room] == command.split()[1]: objects[current_room] = '' inventory.append(command.split()[1]) else: print print "There is not a " + command.split()[1] + " here" def drop(): if command.split()[1] in inventory: inventory.remove(command.split()[1]) objects[current_room] = command.split()[1] else: print print "You are not carrying a " + command.split()[1] def inv(): print("Inventory: ") for item in inventory: print item commands = { 'north' : north, 'n' : north, 'south' : south, 's' : south, 'east' : east, 'e' : east, 'west' : west, 'w' : west, 'up' : up, 'u' : up, 'down' : down, 'd' : down, 'get' : get, 'inventory' : inv, 'inv' : inv, 'drop' : drop } # Main game loop exit = False while (not exit): print print describe_room(current_room) command = raw_input( "What do you want to do: " ) if command != "": command_name = command.split()[0] else: command_name = "none" if command_name in commands.keys(): commands[command_name]() else: print print print '>>> I don\'t understand that' # Check lamp if 'Lamp' in inventory: battery = battery - 1 if battery == 50: print print print "The battery in your lamp is half used" else: if battery == 25: print print print "The battery in your lamp is three quarters used" else: if battery == 10: print print print "The battery in your lamp is almost exhausted" else: if battery == 0: print print "Your lamp has failed" print "You fell in a pit and died!!" print exit = True else: if current_room == 3: print print print "It's dark here, you should find a light source or you might get hurt" else: if current_room > 3: print print "You fell in a pit and died!!" print exit = True # Assasin puzzle if current_room == 23: if 'Knife' in inventory: print print print "You killed a masked man with the knife you were carrying, well done" else: print print "A masked man killed you, you should have looked for a weapon!" print exit = True # Win check if current_room == 25: if (('Crown' in inventory) and ('Diamond' in inventory)): print print "Well done, you found all of the treasure" print exit = True else: print print "There is more treasure to find, go back and keep looking" print |