# Frank L. # IT-140 Module 6 Milestone - Moving between rooms rooms = {0: {'name': 'Hallway', 'items': [], 'keys': [], 'enemies': []}, 1: {'name': 'Grand Entrance', 'items': ['sword', 'sand'], 'keys': [], 'enemies': []}, 2: {'name': 'Study', 'items': ['candle'], 'keys': ['sword'], 'enemies': ['baby dragon', 'salamander']}, 3: {'name': 'Damp Cellar', 'items': ['rope'], 'keys': ['candle'], 'enemies': ['darkness']}, 4: {'name': 'Pit Entrance', 'items': ['silver pieces'], 'keys': ['rope'], 'enemies': []}, 5: {'name': 'Dragon Cave', 'items': ['furnace', 'mirror'], 'keys': ['sand', 'silver pieces'],'enemies': ['warlock']}, 6: {'name': 'Empty Nest', 'items': [], 'keys': ['sword'], 'enemies': []}, 7: {'name': 'Control Room', 'items': ['Dragonbinder'], 'keys': ['mirror'], 'enemies': ['witch']}, 8: {'name': 'Dragon\'s Lair', 'items': ['Holy Grail'], 'keys': ['dragonbinder'], 'enemies': ['dragon']}, } directions = {'n': ['y', -1], 'e': ['x', 1], 's': ['y', 1], 'w': ['x', -1],'north': ['y', -1], 'east': ['x', 1], 'south': ['y', 1], 'west': ['x', -1]} game_map = "..........\ .....0008.\ .000070.9.\ .6.5...0..\ .0.0.300..\ ....00.0..\ ..040020..\ ..0.0.00..\ ....1.0.0.\ .........." game_map = game_map.replace(" ", "") width = 10 height = 10 coords = {'x': 4, 'y': 8} flags = {'game_over':0} def move_player(card): if card in directions: new_coords = coords.copy() new_coords[directions[card][0]] += directions[card][1] new_pos = new_coords['x'] + new_coords['y'] * width if game_map[new_pos] == '.': print("Woa, You tried to go through a wall! OUCH!") else: coords.update(new_coords) else: print("hmmm i am not sure which way {} is...".format(card)) def quit_game(): flags['game_over'] = True #all typed commands must be declared above this line commands = {'go': move_player, 'take': 1, 'use': 2, 'walk': move_player, 'climb': 3, 'quit': quit_game,'exit': quit_game} def print_room_info(pos): if game_map[pos] != '.': print("You are in the {}".format(rooms[int(game_map[pos])]['name'])) else: print("Not sure how to say this.. but you somehow fell out of the world.") def process_command(command): if command[0] in commands: if len(command) < 2: commands[command[0]]() else: commands[command[0]](command[1]) else: print("that command is not valid!") def begin_game(): coords.update({'x': 4, 'y': 8}) end_game = False print("Crystal Dragon Adventures") print("Move commands: go N(orth), go S(outh), go E(ast), go W(est), quit") print("-----------------------------------------------------------------") while not flags['game_over']: pos = coords['x'] + coords['y'] * width print_room_info(pos) process_command(input("Enter your command:").lower().split()) return 0 if __name__ == '__main__': begin_game()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question