def getAction(self): while 1: key = kb.getKey() # If the key is the quit key, quit. if key == kp.QUIT: sys.exit(0) # If the key is the save key, ask if the player wants to save, and do so. elif key == kp.SAVE: if self.currentLevel.elements[self.coords] == level.UPSTAIRS_GLYPH: decision = kb.boolean_question(self.currentLevel.messages, "Do you really want to save and quit the game?") if decision: raise exc.SavingLevelChange() else: self.currentLevel.messages.say("Never mind, then.") return action.DoNothing() else: self.currentLevel.messages.say("You can only save when on the stairs (<).") # If the key is the wait key, wait. elif key == kp.WAIT: return action.Wait(self) # If the key is a movement key, move or attack, as is appropriate. elif key in config.DIRECTION_SWITCH: target = coordinates.add(self.coords, config.DIRECTION_SWITCH[key]) if target in self.currentLevel.dudeLayer: return action.Attack(self, self.currentLevel.dudeLayer[target]) elif self.canMove(target): # If the player is stuck, he cannot move! if self.hasCondition("stuck"): self.currentLevel.messages.append("You are stuck and cannot move!") return action.DoNothing() else: return action.Move(self, config.DIRECTION_SWITCH[key]) else: # A move is illegal! return action.DoNothing() elif key in config.RUN_DIRECTION_SWITCH: direction = config.RUN_DIRECTION_SWITCH[key] target = coordinates.add(self.coords, direction) if len(self.fov.dudes) != 0: self.currentLevel.messages.append("Not with enemies in view!") return action.DoNothing() elif self.canMove(target): # If the player is stuck, he cannot move. if self.hasCondition("stuck"): self.currentLevel.messages.append("You are stuck and cannot move!") return action.DoNothing() else: self.giveCondition(cond.Running(direction)) return action.Move(self, direction) # If the key requests that a card be used, prompt for a card, then use it. elif key == kp.FIRE: if len(self.deck.hand) == 0: self.currentLevel.messages.append( "You have no cards to use!") return action.DoNothing() else: card_id = kb.card_question(self.currentLevel.messages, "Which card do you want to evoke?", self.deck) if card_id == -1: return action.DoNothing() else: return self.useCard(card_id) elif key == kp.HEAL: # Have the player use a card to heal wounds. card_id = kb.card_question(self.currentLevel.messages, "Which card will you sacrifice for your health?", self.deck) if card_id == -1: return action.DoNothing() else: del self.deck.hand[card_id] return action.Heal(self, self, rng.XdY(2, 7 + self.char_level), False) # If the key is the "go upstairs" key, try to go up a level. elif key == kp.UP: if self.currentLevel.elements[self.coords] == level.UPSTAIRS_GLYPH: return action.Up()
def getAction(self): while 1: key = kb.getKey() # If the key is the quit key, quit. if key == kp.QUIT: sys.exit(0) # If the key is the save key, ask if the player wants to save, and do so. elif key == kp.SAVE: if self.currentLevel.elements[self.coords] == level.UPSTAIRS_GLYPH: decision = kb.boolean_question(self.currentLevel.messages, "Do you really want to save and quit the game?") if decision: raise exc.SavingLevelChange() else: self.currentLevel.messages.say("Never mind, then.") return action.DoNothing() else: self.currentLevel.messages.say("You can only save when on the stairs (<).") # If the key is the wait key, wait. elif key == kp.WAIT: return action.Wait(self) # If the key is a movement key, move or attack, as is appropriate. elif key in config.DIRECTION_SWITCH: target = coordinates.add(self.coords, config.DIRECTION_SWITCH[key]) if target in self.currentLevel.dudeLayer: return action.Attack(self, self.currentLevel.dudeLayer[target]) elif self.canMove(target): # If the player is stuck, he cannot move! if self.hasCondition("stuck"): self.currentLevel.messages.append("You are stuck and cannot move!") return action.DoNothing() else: return action.Move(self, config.DIRECTION_SWITCH[key]) else: # A move is illegal! return action.DoNothing() elif key in config.RUN_DIRECTION_SWITCH: direction = config.RUN_DIRECTION_SWITCH[key] target = coordinates.add(self.coords, direction) if len(self.fov.dudes) != 0: self.currentLevel.messages.append("Not with enemies in view!") return action.DoNothing() elif self.canMove(target): # If the player is stuck, he cannot move. if self.hasCondition("stuck"): self.currentLevel.messages.append("You are stuck and cannot move!") return action.DoNothing() else: self.giveCondition(cond.Running(direction)) return action.Move(self, direction) # If the key is a card key, use the card. elif key in kb.card_values: card_id = kb.card_values[key] if card_id >= len(self.deck.hand): return action.DoNothing() card_to_use = self.deck.hand[card_id] # If the card is directional, get the direction to use it in. if card_to_use.is_directional: direction_of_target_square = kb.direction_question( self.currentLevel.messages, "In which direction would you like to use the %s card?" % card_to_use.ability_name) if card_to_use.is_melee: target_square = coordinates.add(self.coords, direction_of_target_square) if target_square in self.currentLevel.dudeLayer: del self.deck.hand[card_id] return action.SpecialMelee(self, self.currentLevel.dudeLayer[target_square], card_to_use.action_code) else: self.currentLevel.messages.say("You whiff completely!") del self.deck.hand[card_id] return action.Wait(self) else: if card_to_use.action_code == "GRENTHROW": target_square = coordinates.add(self.coords, coordinates.multiply(direction_of_target_square, 2)) if self.currentLevel.isEmpty(target_square) and (not events.is_grenade_at_coords(target_square, self.currentLevel)): del self.deck.hand[card_id] return action.ThrowGrenade(self, target_square) else: self.currentLevel.messages.say("There's something in the way!") return action.DoNothing() elif card_to_use.action_code == "ARROW": del self.deck.hand[card_id] return action.FireArrow(self, direction_of_target_square, 12) elif card_to_use.action_code == "HASTE": del self.deck.hand[card_id] return action.HasteMonster(self, self, 12) elif card_to_use.action_code == "HASTEALL": del self.deck.hand[card_id] return action.HasteAll(self, 8) assert False assert False elif key == kp.REST: # Give the player the "rest" status, so she waits until healed fully. self.giveCondition(cond.Resting()) return action.Wait(self) # If the key is the "go upstairs" key, try to go up a level. elif key == kp.UP: if self.currentLevel.elements[self.coords] == level.UPSTAIRS_GLYPH: return action.Up()