def printRoomStateOrDescription(player): # get and print room description or state roomState = db.getRoomState(player.roomID) if roomState is not None: print("--\n{}".format(roomState)) else: roomDescription = db.getRoomDescription(player.roomID) print("--\n{}".format(roomDescription))
def go(command, directions, player): if command[0] == "go" and len(command) == 1: if directions != {}: print("You can go: ") for direction in directions.keys(): print("\t* {}".format(direction)) else: print("You can't go anywhere, try to do something else.") elif command[0] == "go" and len(command) == 2: # cheat for testing game roomID = [] for i in range(1, 35): roomID.append(str(i)) if command[1] in directions: player.roomID = directions[command[1]] db.setPlayerRoomID(player.roomID) npcs = db.getNPCsInRoom(player.roomID) db.modifypoints(db.getPointsFromRoom(player.roomID)) if player.roomID == 2 and 'torch' in player.inventory: for i in (2, 3, 5): db.updateRoomState(i, 1) printRoomStateOrDescription(player) elif player.roomID in (3, 5) and 'torch' not in player.inventory: print(db.getRoomState(player.roomID)) raise SystemExit elif player.roomID == 6 and npcs != {}: printRoomStateOrDescription(player) fight(player,npcs) elif player.roomID == 7: if db.getRoomStateID(player.roomID) != 3: success = action.throwIntelligence(player) if success == 2: db.updateRoomState(player.roomID, 0) printRoomStateOrDescription(player) db.updateRoomState(player.roomID, 3) elif success == 1: db.updateRoomState(player.roomID, 1) printRoomStateOrDescription(player) db.modifyhp(-2) db.updateRoomState(player.roomID, 3) else: db.updateRoomState(player.roomID, 2) printRoomStateOrDescription(player) db.modifyhp(-10) db.updateRoomState(player.roomID, 3) else: printRoomStateOrDescription(player) elif player.roomID == 8: success = action.throwIntelligence(player) if success in (1, 2): db.updateRoomState(player.roomID, 0) printRoomStateOrDescription(player) db.updateMovements(player, 15, 7, 'NULL', 14, 'NULL', 'NULL') else: db.updateRoomState(player.roomID, 1) printRoomStateOrDescription(player) elif player.roomID == 11: printRoomStateOrDescription(player) items = db.getItemsInRoom(player.roomID) if "sword" in items: take(items, player, "sword") elif player.roomID == 12: printRoomStateOrDescription(player) fight(player,npcs) elif player.roomID == 13: printRoomStateOrDescription(player) if npcs != {}: for key, character in npcs.items(): if character.ID == 7: npc = npcs[key] quest = conversation.talk(npc) db.dropNPCItem(npc,"carddeck") db.cleanNPCFromRoom(npc) npcs = db.getNPCsInRoom(player.roomID) if quest == 1: fight(player, npcs) db.updateRoomState(player.roomID, 3) printRoomStateOrDescription(player) items = db.getItemsInRoom(player.roomID) if "carddeck" in items: take(items, player, "carddeck") db.updateRoomState(player.roomID, 1) printRoomStateOrDescription(player) if quest == 0: db.updateRoomState(player.roomID, 2) printRoomStateOrDescription(player) elif player.roomID == 15: if db.getRoomStateID(player.roomID) != 3: success = action.throwIntelligence(player) if success == 2: db.updateRoomState(player.roomID, 0) printRoomStateOrDescription(player) db.updateRoomState(player.roomID, 3) elif success == 1: db.updateRoomState(player.roomID, 1) printRoomStateOrDescription(player) db.modifyhp(-8) db.updateRoomState(player.roomID, 3) else: db.updateRoomState(player.roomID, 2) print(db.getRoomState(player.roomID)) raise SystemExit else: printRoomStateOrDescription(player) elif player.roomID == 17: printRoomStateOrDescription(player) if npcs != {}: for key, character in npcs.items(): if character.ID == 8: npc = npcs[key] quest = conversation.talk(npc) if quest == 1: db.dropNPCItem(npc,"healthpotion") db.cleanNPCFromRoom(npc) npcs = db.getNPCsInRoom(player.roomID) fight(player, npcs) db.updateRoomState(player.roomID, 4) printRoomStateOrDescription(player) items = db.getItemsInRoom(player.roomID) if "healthpotion" in items: take(items, player, "healthpotion") db.updateRoomState(player.roomID, 3) printRoomStateOrDescription(player) elif quest == 0: keys = [] for key, npc in npcs.items(): keys.append(key) for key in keys: db.cleanNPCFromRoom(npcs[key]) db.updateRoomState(player.roomID, 3) printRoomStateOrDescription(player) elif player.roomID == 19 \ and command[1] == "south" \ and "torch" in player.inventory: roomstate = db.getRoomStateID(18) if roomstate == 0: player.roomID = 18 db.setPlayerRoomID(player.roomID) db.updateRoomState(player.roomID,2) for i in (23,24): db.bringNPCToRoom(player.roomID,i) npcs = db.getNPCsInRoom(player.roomID) printRoomStateOrDescription(player) fight(player,npcs) db.updateRoomState(player.roomID,3) printRoomStateOrDescription(player) else: printRoomStateOrDescription(player) elif player.roomID == 21 \ and command[1] == "west" \ and player.playerClass == "barbarian": player.roomID = 20 db.setPlayerRoomID(player.roomID) db.updateRoomState(player.roomID,1) db.updateMovements(player,19,'NULL','NULL','NULL','NULL','NULL') printRoomStateOrDescription(player) db.updateRoomState(player.roomID,0) printRoomStateOrDescription(player) elif player.roomID == 21 \ and command[1] == "east" \ and player.playerClass == "barbarian": player.roomID = 22 db.setPlayerRoomID(player.roomID) db.updateMovements(player,23,'NULL','NULL','NULL','NULL',25) printRoomStateOrDescription(player) elif player.roomID == 22 \ and npcs != {} \ and "armor" in player.inventory \ and "carddeck" in player.inventory: if db.getRoomStateID(player.roomID) != 7: db.updateRoomState(22, 2) printRoomStateOrDescription(player) db.updateRoomState(22, 3) printRoomStateOrDescription(player) db.updateRoomState(22, 7) items = db.getItemsInRoom(player.roomID) if "gold" in items: take(items, player, "gold") print("You now have a huge pile of gold in your inventory") else: printRoomStateOrDescription(player) elif player.roomID == 22 \ and npcs != {} \ and "armor" in player.inventory: if db.getRoomStateID(player.roomID) not in (6,7): db.updateRoomState(22, 2) printRoomStateOrDescription(player) success = action.throwIntelligence(player) if success == 2: db.updateRoomState(player.roomID, 3) printRoomStateOrDescription(player) db.updateRoomState(22, 7) items = db.getItemsInRoom(player.roomID) if "gold" in items: take(items, player, "gold") print("You now have a huge pile of gold in your inventory") elif success != 2: db.updateRoomState(player.roomID, 4) printRoomStateOrDescription(player) fight(player,npcs) db.updateRoomState(player.roomID, 5) printRoomStateOrDescription(player) db.updateRoomState(22, 7) items = db.getItemsInRoom(player.roomID) if "gold" in items: take(items, player, "gold") print("You now have a huge pile of gold in your inventory") else: printRoomStateOrDescription(player) elif player.roomID == 22 and npcs != {}: if db.getRoomStateID(player.roomID) not in (6,7): db.updateRoomState(22, 0) printRoomStateOrDescription(player) fight(player,npcs) db.updateRoomState(player.roomID, 5) printRoomStateOrDescription(player) db.updateRoomState(22, 7) items = db.getItemsInRoom(player.roomID) if "gold" in items: take(items, player, "gold") print("You now have a huge pile of gold in your inventory") else: printRoomStateOrDescription(player) elif player.roomID == 22 and npcs =={}: db.updateRoomState(22, 7) printRoomStateOrDescription(player) elif player.roomID == 23 and 'armor' in player.inventory: db.updateRoomState(player.roomID, 2) printRoomStateOrDescription(player) elif player.roomID == 24: success = action.throwIntelligence(player) if success == 2: db.updateRoomState(player.roomID, 0) printRoomStateOrDescription(player) elif success == 1: db.updateRoomState(player.roomID, 1) printRoomStateOrDescription(player) db.modifyhp(-5) else: db.updateRoomState(player.roomID, 2) printRoomStateOrDescription(player) db.modifyhp(-10) elif player.roomID == 28: printRoomStateOrDescription(player) npcs = db.getNPCsInRoom(player.roomID) if npcs != {}: fight(player,npcs) db.updateRoomState(player.roomID, 1) elif player.roomID == 32 \ and command[1] == "east" \ and player.playerClass == "barbarian": player.roomID = 31 db.setPlayerRoomID(player.roomID) db.updateRoomState(player.roomID,1) db.updateMovements(player,'NULL',34,'NULL',27,'NULL','NULL') printRoomStateOrDescription(player) db.updateRoomState(player.roomID,0) printRoomStateOrDescription(player) elif player.roomID == 33 \ and command[1] == "south" \ and player.playerClass == "thief": db.updateRoomState(player.roomID, 2) printRoomStateOrDescription(player) elif player.roomID == 33 and npcs != {}: printRoomStateOrDescription(player) npcs = db.getNPCsInRoom(player.roomID) fight(player,npcs) else: # print room state after room is actually changed printRoomStateOrDescription(player) # cheat for testing game elif command[1] in roomID: player.roomID = int(command[1]) db.setPlayerRoomID(player.roomID) printRoomStateOrDescription(player) else: print( "Invalid direction. You can use " "only directions in list below:" ) for direction in directions.keys(): print("\t* {}".format(direction))
def execute(command, directions, items, npcs, player): ''' :param command: only valid commands came in from isValid(command) :return: "main" if switch context to "main", "game" if keep playing. ''' if command[0] == "go": go(command, directions, player) elif command[0] == "help": if len(command) == 1: help() else: help(command[1]) elif command[0] == "menu": return "main" elif command[0] == "quit": raise SystemExit elif command[0] == "look": if len(command) == 1: look(items, npcs, player) else: look(items, npcs, player, command[1]) elif command[0] == "take": if len(command) == 1: take(items) else: take(items, player, command[1]) elif command[0] == "inventory": print(player.inventory) elif command[0] == "drop": if len(command) == 1: drop(player) else: drop(player, command[1]) elif command[0] == "use": if len(command) == 1: use(player) else: use(player, command[1]) elif command[0] == "jump": if player.roomID == 3: success = action.jump(player) if success == 2: db.updateRoomState(player.roomID, 2) if 'west' in directions: db.updateMovements(player, 'NULL', 'NULL', 4, 'NULL', 'NULL', 'NULL') printRoomStateOrDescription(player) db.updateRoomState(player.roomID, 1) else: db.updateMovements(player, 'NULL', 'NULL', 'NULL', 2, 'NULL', 'NULL') printRoomStateOrDescription(player) db.updateRoomState(player.roomID, 1) elif success == 1: db.updateRoomState(player.roomID, 3) db.modifyhp(-5) if 'west' in directions: db.updateMovements(player, 'NULL', 'NULL', 4, 'NULL', 'NULL', 'NULL') else: db.updateMovements(player, 'NULL', 'NULL', 'NULL', 2, 'NULL', 'NULL') else: db.updateRoomState(player.roomID, 4) printRoomStateOrDescription(player) raise SystemExit elif player.roomID == 27: success = action.jump(player) if success == 2: db.updateRoomState(player.roomID, 1) if 'north' in directions: db.updateMovements(player, 'NULL',28,31,'NULL','NULL','NULL') printRoomStateOrDescription(player) db.updateRoomState(player.roomID, 0) else: db.updateMovements(player, 26, 'NULL', 'NULL', 'NULL', 'NULL', 'NULL') printRoomStateOrDescription(player) db.updateRoomState(player.roomID, 0) elif success == 1: db.updateRoomState(player.roomID, 2) db.modifyhp(-5) if 'north' in directions: db.updateMovements(player, 'NULL',28,31,'NULL','NULL','NULL') print(db.getRoomState(player.roomID)) db.updateRoomState(player.roomID, 0) else: db.updateMovements(player, 26, 'NULL', 'NULL', 'NULL', 'NULL', 'NULL') printRoomStateOrDescription(player) else: db.updateRoomState(player.roomID, 3) printRoomStateOrDescription(player) raise SystemExit else: print("There is no reason to jump.") elif command[0] == "fight": if len(command) == 1: fight(player, npcs) else: fight(player, npcs, command[1]) elif command[0] == "talk": if len(command) == 1: talk(player, npcs) else: talk(player, npcs, command[1]) elif command[0] == "give": if len(command) == 1: give(player) else: give(player, command[1]) elif command[0] == "push": if len(command) == 1: push(player) else: push(player, command[1]) elif command[0] in commands: print('Command "{0:s}" is not implemented yet.'.format(command[0])) return "game"
# -*- coding: utf-8 -*- import functions.commands as command import functions.database as db if db.testConnection(): player = db.getPlayer() f = open('ASCII/StartScreen_UTF-8.asc', 'r') print(f.read()) f.close() roomState = db.getRoomState(player.roomID) if roomState is not None: print("--\n{}".format(roomState)) else: roomDescription = db.getRoomDescription(player.roomID) print("--\n{}".format(roomDescription)) while True: ''' You can end loop by selecting 5 in main context or write "quit" in game context. ''' context = command.doMenu() while context == "main": # validate to player that we are in correct context prompt = "(main) >>> " try: