def fight(self, displayInstance): """Function to simulate a fight between the current character and enemy""" firstMove = True self._fightOn = True fightMoves = 5 # continue fight for 5 moves, or until one character goes below 0 health while fightMoves > 0 and self._character.characterHealth > 0: # get character move firstMove = self._lockOnFirstMove(firstMove) self._getFightMove(self._character.characterName) # if last move resulted in bad health for enemy, exit fight if self._enemyStats['healthRating'] <= 0: threadSemaphore.unlock() break threadSemaphore.unlock() time.sleep(.3) # get enemy move threadSemaphore.lock() self._getFightMove(self._enemy) fightMoves = fightMoves - 1 threadSemaphore.unlock() time.sleep(.3) # get the result of the fight threadSemaphore.lock() self._fightOn = False self._determineFightResult()
def playGame(self, displayInstance): """Run the game until the end has reached""" while self._state != GameState.END: if self._state == GameState.PLAYING: # send a message to display self._generateGameSituation(displayInstance) # sleep briefly to allow other thread to take control of semaphore time.sleep(.1) # receive user input from display self._handleGameSituation(displayInstance) elif self._state == GameState.RESULT: time.sleep(.1) threadSemaphore.lock() self._newGameStats() self._state = GameState.AGAIN threadSemaphore.unlock() elif self._state == GameState.AGAIN: # wait until again message received while self._againMessage == "": {} # see if should play again self._playAgain() time.sleep(.5) time.sleep(.1)
def _playAgain(self): """Function to determine if should play again""" threadSemaphore.lock() if self._againMessage == 'y': self._reinitializeBeginning() else: self._state = GameState.END threadSemaphore.unlock()
def _lockOnFirstMove(self, firstMove): """Kind of a weird function, but this tells if this is the first move by the user; main thread locks up semaphore and we do not want to lock up again if user first round of fight""" if firstMove: firstMove = False else: threadSemaphore.lock() return firstMove
def _endGame(self): """Function to end the game and send signal to display to do so""" threadSemaphore.lock() if self._continueMessage == 'n' or \ self._horcruxesLeft == 0 or \ self._enemiesLeft == 0 or \ self.character.characterHealth <= 0 or \ self._stepsLeft == 0: self._state = GameState.RESULT self._displayMessage = "" self._continueMessage = "" threadSemaphore.unlock()
def _handleGameSituation(self, displayInstance): """Function that determines how to handle the given game situation""" threadSemaphore.lock() if self._eventType == 'obstacles' and self._userInput == '1': self._fightEnemy(displayInstance, self._action, enemies[self._action], self.character) elif self._eventType == 'obstacles' and self._userInput == '2': self._noFightSelected() threadSemaphore.unlock() # wait for continue message to be set in display while not displayInstance.continueSet: if self._stepsLeft == 0: break displayInstance.continueSet = False # determine if game should be ended self._endGame()
def _generateGameSituation(self, displayInstance): """Function that generates a situation for the user to be in""" threadSemaphore.lock() situationChoice = random.choice( [obstacles, downgrades, horcruxes, enhancements]) possibleChoices = [message for message in situationChoice['data']] message = random.choice(possibleChoices) while self._displayMessage == message: message = random.choice(possibleChoices) # set event type and display message to access in display # decrease the number of steps the user has self._eventType = situationChoice['type'] self._displayMessage = message[0] self._action = message[1] self._stepsLeft = self._stepsLeft - 1 self._updateOnAction(message) threadSemaphore.unlock()