def use(player, item=None): roomState = db.getRoomStateID(player.roomID) if item == None: if player.inventory != []: print("You can use items below:") for item in player.inventory: print("\t* {}".format(item)) else: print("There is no items in inventory to use.") elif item == "torch" and item in player.inventory and player.roomID == 21: db.updateRoomState(player.roomID, 2) printRoomStateOrDescription(player) npcs = db.getNPCsInRoom(player.roomID) keys = [] for key in npcs.keys(): keys.append(key) for key in keys: db.cleanNPCFromRoom(npcs[key]) db.updateMovements(player,'NULL','NULL',20,22,'NULL','NULL') db.updateRoomState(22,6) elif item == "sleepingpotion" and item in player.inventory and player.roomID == 33 and roomState==2: db.updateRoomState(player.roomID, 3) player.inventory.remove(item) printRoomStateOrDescription(player) npcs = db.getNPCsInRoom(player.roomID) keys = [] for key in npcs.keys(): keys.append(key) for key in keys: db.cleanNPCFromRoom(npcs[key]) raise SystemExit elif item == "healthpotion" and item in player.inventory: db.modifyhp(15) player = db.updatePlayer(player) player.inventory.remove(item) db.useItem(item) print("You drink the health potion. You feel reinvigorated as the " "healing potion surges through your body, mending the wounds " "and restoring your beaten physique." ) elif item in player.inventory: print('''Using item "{}" doesn't make sense.'''.format(item)) else: print('Cannot use item "{}".'.format(item))
def talk(player, npcs, npc=None): if npc == None: if npcs != {}: print("Characters you can talk with:") for key, character in npcs.items(): print("\t{} = {}".format(key, character.NPCName)) else: print("There are no characters to talk with in room.") if npc in npcs.keys(): if player.roomID == 10: quest = conversation.talk(npcs[npc]) if quest == 1: db.updateMovements(player, 12, 9, 'NULL', 11, 'NULL', 'NULL') db.updateRoomState(player.roomID, 4) db.modifypoints(db.getPointsFromNPC(npcs[npc].ID)) db.cleanNPCFromRoom(npcs[npc]) npcs = db.getNPCsInRoom(player.roomID) printRoomStateOrDescription(player) elif player.roomID == 34: quest = conversation.talk(npcs[npc], player) if quest == 1: db.updateMovements(player, 31, 33, 'NULL', 'NULL', 'NULL', 'NULL') db.updateRoomState(player.roomID, 1) else: print("Conversation with {} has not written yet.".format( npcs[npc].NPCName))
def drop(player=None, item=None): if item == None: if player.inventory != []: print("You can drop items below:") for item in player.inventory: print("\t* {}".format(item)) else: print("There is no items in inventory to drop.") elif item in player.inventory: db.dropItem(item, player) print('You dropped item "{}".'.format(item)) player.inventory.remove(item) if player.roomID == 18 and item == "torch": db.updateRoomState(player.roomID,1) printRoomStateOrDescription(player) elif player.roomID == 30 and item == "gold": db.updateRoomState(player.roomID,1) npcs = db.getNPCsInRoom(player.roomID) for key, character in npcs.items(): if character.ID == 17: npc = npcs[key] if player.playerClass == "barbarian": db.dropNPCItem(npc,"shield") items = db.getItemsInRoom(player.roomID) take(items,player,"shield") else: db.dropNPCItem(npc,"sleepingpotion") items = db.getItemsInRoom(player.roomID) take(items,player,"sleepingpotion") db.updateRoomState(player.roomID,3) printRoomStateOrDescription(player) db.updateRoomState(player.roomID,4) else: print('Cannot drop item "{}".'.format(item))
def fight(player=None, npcs={}, npc=None): if npc == None: while npcs != {}: for i in range(0,len(npcs)): npcs = fight(player,npcs,'0') elif npcs[npc].NPCName=='Dragon': db.updateRoomState(player.roomID, 2) printRoomStateOrDescription(player) raise SystemExit elif npcs[npc].NPCName=='Spider': db.updateRoomState(player.roomID, 1) printRoomStateOrDescription(player) raise SystemExit elif npcs[npc].NPCName == "Zhaltrauc": shieldIsActive = False useShield = input("The gem on Zhaltrauc's crown begins to glow!") if useShield.lower() == "use shield" and "shield" in player.inventory: shieldIsActive=True print("Zhaltrauc shoot's a beam of light from his gem. You lift " "your shield just in time and deflect the shot, blinding " "Zhaltrauc!") else: print("Blinding light flashes from Zhaltrauc's gem! You are blinded by the light!") # Attack turn if shieldIsActive: attack = action.attack(player, npcs[npc]) db.modifyNPCHP(attack, npcs[npc]) npcs[npc] = db.updateNPC(npcs[npc]) player = db.updatePlayer(player) if npcs[npc].HP <= 0: print("{} {} died.".format(npcs[npc].NPCName, npcs[npc].ID)) db.modifypoints(db.getPointsFromNPC(npcs[npc].ID)) db.cleanNPCFromRoom(npcs[npc]) npcs = db.getNPCsInRoom(player.roomID) db.updateRoomState(player.roomID,1) printRoomStateOrDescription(player) raise SystemExit # Dodge turn if not shieldIsActive: if npcs[npc].HP > 0: dodge = action.dodge(player, npcs[npc]) db.modifyhp(dodge) npcs[npc] = db.updateNPC(npcs[npc]) player = db.updatePlayer(player) if player.HP <= 0: print("You died.") raise SystemExit elif npc in npcs.keys(): enemyIsAlive = True while enemyIsAlive: # Attack turn attack = action.attack(player, npcs[npc]) db.modifyNPCHP(attack, npcs[npc]) npcs[npc] = db.updateNPC(npcs[npc]) player = db.updatePlayer(player) input('Press "Enter"') # Dodge turn for npc in npcs.keys(): npcs = db.getNPCsInRoom(player.roomID) if npc in npcs.keys(): if npcs[npc].HP > 0: dodge = action.dodge(player, npcs[npc]) db.modifyhp(dodge) npcs[npc] = db.updateNPC(npcs[npc]) player = db.updatePlayer(player) if player.HP <= 0: print("You died.") raise SystemExit if npc in npcs.keys(): if npcs[npc].HP <= 0: print("{} {} died.".format(npcs[npc].NPCName, npcs[npc].ID)) db.modifypoints(db.getPointsFromNPC(npcs[npc].ID)) db.cleanNPCFromRoom(npcs[npc]) npcs = db.getNPCsInRoom(player.roomID) enemyIsAlive = False if player.roomID == 1: db.updateMovements(player, 2, 'NULL', 'NULL', 'NULL', 'NULL', 'NULL') if player.roomID == 6 and npcs == {}: db.updateMovements(player, 'NULL', 'NULL', 5, 'NULL', 'NULL', 'NULL') db.updateRoomState(player.roomID, 1) printRoomStateOrDescription(player) db.updateRoomState(player.roomID, 2) if player.roomID == 10: db.updateRoomState(player.roomID, 2) printRoomStateOrDescription(player) if player.roomID == 12 and npcs == {}: db.updateMovements(player, 'NULL', 10, 13, 'NULL', 'NULL', 'NULL') db.updateRoomState(player.roomID, 1) printRoomStateOrDescription(player) db.updateRoomState(player.roomID, 2) input('Press "Enter"') else: print('Cannot fight with "{}".'.format(npc)) return npcs
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))
# validate to player that we are in correct context prompt = "(game) >>> " hp=db.gethp() points=db.getPointsFromPlayer() # update player object and if doesn't exist yet create it player = db.updatePlayer(player) print("\nYour HP: {}".format(hp) ) print("Your points: {}".format(points)) # get directions player can go directions = db.getDirections(player.roomID) # get items in room player can take items = db.getItemsInRoom(player.roomID) # get NPCs in room player can action (talk or fight) npcs = db.getNPCsInRoom(player.roomID) # ask a command from player c = input(prompt).lower().split() # pass a command, possible directions, items in room and the player # object to command parser if (command.isValid(c)): context = command.execute(c, directions, items, npcs, player) else: print('Invalid command. ' 'Write "help" to get list of available commands.' )