Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. '''This is the main class of the game.'''
  2.  
  3. import datetime
  4. import os
  5. import random
  6. import sys
  7. from blessings import Terminal
  8. import time
  9.  
  10. from bureaucrat import Bureaucrat
  11. from character import Character
  12. import text
  13.  
  14. t = Terminal()
  15.  
  16.  
  17. class Game:
  18.     '''The main logic of the game.'''
  19.     def __init__(self):
  20.         self.clear_screen()
  21.         input(t.underline('Welcome to LAM-40 Alpha!') + '\n\n' +
  22.               text.intro + text.disclaimer)  # the first screen of the game
  23.         self.clear_screen()
  24.         self.character = Character()  # creates an instance of the player class
  25.         # sets the date and the time of the game
  26.         self.timer = datetime.datetime(2023, 9, 17, 8, 0)
  27.         for number, floor in enumerate(range(39)):
  28.             self.bureaucrats_number = random.choice((range(1),
  29.                                                      range(2),
  30.                                                      range(3)))
  31.             for bureaucrat in self.bureaucrats_number:
  32.                 self.bureaucrat = Bureaucrat()
  33.                 self.act()
  34.             self.clear_screen()
  35.             print('One of the visitors slowly passes by and says:\n' +
  36.                   random.choice(text.visitor_remarks) + '\n\n' +
  37.                   'You reach ' + t.bold('the floor number {}.'
  38.                                         .format(number + 2)) + '\n')
  39.             time.sleep(2)
  40.         self.game_completed()
  41.  
  42.     def act(self):
  43.         '''An action taken by the player.'''
  44.         # the following loop numerically lists all available actions
  45.         for number, action in enumerate(self.character.actions):
  46.             number = str(number + 1)
  47.             if int(number) < 10:
  48.                 print('0' + number + '. ' + action)
  49.             else:
  50.                 print(number + '. ' + action)
  51.         self.action = input('\nIt is ' +
  52.                             t.bold('{}'.format(self.timer.strftime('%H:%M'))) +
  53.                             '. What will you do? ').lower()
  54.         if self.action == 'q':  # quits the game
  55.             self.quit()
  56.         elif self.action == 'w':  # creates a new bureaucrat
  57.             self.timer_add(random.choice(range(800, 1001)))
  58.             print('\nYou wait for the next bureaucrat to appear.\n')
  59.             time.sleep(1)
  60.             self.evaluate_time()  # checks if the player ran out of time
  61.             self.bureaucrat = Bureaucrat()
  62.             self.act()
  63.         elif self.action in 'abcdefhijklmpstyw':
  64.             self.react()  # finds out what type of action was chosen
  65.             self.calculate_odds()  # alters the odds of success
  66.             self.evaluate_success()  # determines success or failure
  67.         else:  # incorrect input is given
  68.             print('\nIncorrect action. Please try again.\n')
  69.             time.sleep(1)
  70.  
  71.     def react(self):
  72.         '''A reaction to the choice of the player.'''
  73.         if self.action == 'bt':  # character.py for reference
  74.             self.character.probability_is_low()
  75.             self.character.asc_rank_asc_mood()
  76.             self.timer_add(random.choice(range(200, 401)))
  77.         elif self.action in 'cp':
  78.             self.character.probability_is_high()
  79.             self.character.desc_rank_desc_mood()
  80.             self.timer_add(random.choice(range(500, 701)))
  81.         elif self.action in 'dm':
  82.             self.character.probability_is_low()
  83.             self.character.desc_rank_desc_mood()
  84.             self.timer_add(random.choice(range(350, 551)))
  85.         elif self.action in 'efj':
  86.             self.character.probability_is_high()
  87.             self.character.asc_rank_asc_mood()
  88.             self.timer_add(random.choice(range(650, 851)))
  89.         elif self.action in 'hkly':
  90.             self.character.probability_is_low()
  91.             self.character.desc_rank_asc_mood()
  92.             self.timer_add(random.choice(range(350, 551)))
  93.         elif self.action in 'is':
  94.             self.character.probability_is_high()
  95.             self.character.asc_rank_desc_mood()
  96.             self.timer_add(random.choice(range(500, 701)))
  97.         else:  # 'a' (ask)
  98.             self.character.probability_is_high()
  99.             self.character.desc_rank_asc_mood()
  100.             self.timer_add(random.choice(range(650, 851)))
  101.  
  102.     def calculate_odds(self):
  103.         '''Calculates the odds of a successful action.'''
  104.         # odds of success are randomly assigned to be 25-75% at first
  105.         self.odds = float(random.choice(range(25, 76)))
  106.         if self.bureaucrat.rank == 'low':
  107.             if self.character.asc_rank is True:  # character.py for reference
  108.                 self.odds /= self.character.divisor
  109.             else:
  110.                 self.odds *= self.character.multiplier
  111.         elif self.bureaucrat.rank == 'high':
  112.             if self.character.asc_rank is True:
  113.                 self.odds *= self.character.multiplier
  114.             else:
  115.                 self.odds /= self.character.divisor
  116.         else:
  117.             pass  # medium rank doesn't affect the odds
  118.         if self.bureaucrat.mood == 'bad':
  119.             if self.character.asc_mood is True:
  120.                 self.odds /= self.character.divisor
  121.             else:
  122.                 self.odds *= self.character.multiplier
  123.         elif self.bureaucrat.mood == 'good':
  124.             if self.character.asc_mood is True:
  125.                 self.odds *= self.character.multiplier
  126.             else:
  127.                 self.odds /= self.character.divisor
  128.         else:
  129.             pass  # average mood doesn't affect the odds
  130.  
  131.     def evaluate_success(self):
  132.         '''Evaluates whether the action is successful or not.'''
  133.         # the higher self.odds value
  134.         # the higher the chance the action will succeed
  135.         self.luck = random.choice(range(0, 101))
  136.         if self.luck > self.odds:
  137.             self.react_negatively()
  138.             if self.action in 'bkty':  # your risky action was unsuccessful
  139.                 self.game_over()
  140.         else:
  141.             self.react_positively()
  142.  
  143.     def react_negatively(self):
  144.         '''A negative reaction to the choice of the player.'''
  145.         self.clear_screen()
  146.         print(t.bold("It doesn't affect the bureaucrat!\n") +
  147.               random.choice(text.negative_reactions) + '\n')
  148.         time.sleep(1.5)
  149.         self.evaluate_time()
  150.         self.act()
  151.  
  152.     def react_positively(self):
  153.         '''A positive reaction to the choice of the player.'''
  154.         self.clear_screen()
  155.         print(t.bold("It's super effective!\n") +
  156.               random.choice(text.positive_reactions) + '\n')
  157.         time.sleep(1.5)  # a new bureaucrat is generated
  158.         self.evaluate_time()
  159.  
  160.     def timer_add(self, seconds_passed):
  161.         '''Adds the time the action took to the timer.'''
  162.         self.timer += datetime.timedelta(seconds=seconds_passed)
  163.  
  164.     def evaluate_time(self):
  165.         '''Checks whether you still have time left.'''
  166.         if self.timer - datetime.datetime(2023, 9, 17, 8, 0) >= \
  167.            datetime.timedelta(seconds=43200):
  168.             self.game_over()
  169.         else:
  170.             pass
  171.  
  172.     def quit(self):
  173.         '''Quits the game.'''
  174.         self.clear_screen()
  175.         sys.exit('Thank you for playing LAM-40.\n')
  176.  
  177.     def game_over(self):
  178.         '''Called when the game is over.'''
  179.         self.clear_screen()
  180.         if self.action in 'bkty':
  181.             print('Game over! You were handed over to the police ' +
  182.                   'and arrested.\nYou will have to try again next day.')
  183.         else:  # the time is up
  184.             print('Game over! The Instituion is now closed for today.' +
  185.                   '\nYou will have to try again next day.')
  186.         self.choice = input("\nType 'r' if you want to restart: ").lower()
  187.         if self.choice == 'r':  # restarts the game from the beginning
  188.             os.execl(sys.executable, sys.executable, * sys.argv)
  189.         else:
  190.             self.quit()
  191.  
  192.     def game_completed(self):
  193.         '''Ends the game after showing the ending.'''
  194.         self.clear_screen()
  195.         if self.timer - datetime.datetime(2023, 9, 17, 8, 0) >= \
  196.            datetime.timedelta(seconds=21600):
  197.             print(text.ending)
  198.         else:  # the player reaches the floor number 40 in 6 hours
  199.             print(text.secret_ending)
  200.         self.clear_screen()
  201.         sys.exit('Congratulations! You beat LAM-40. See you next time!\n')
  202.  
  203.     def clear_screen(self):
  204.         '''Clears the screen of the terminal.'''
  205.         if os.name == 'nt':  # 'nt' equals Windows
  206.             os.system('cls')
  207.         else:
  208.             os.system('clear')  # Mac OS and Linux
  209.  
  210. Game()