Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''This is the main class of the game.'''
- import datetime
- import os
- import random
- import sys
- from blessings import Terminal
- from bureaucrat import Bureaucrat
- from character import Character
- t = Terminal()
- class Game:
- '''The main logic of the game'''
- def __init__(self):
- self.clear_screen()
- print(t.underline('LAM-40 0.4'))
- self.character = Character()
- self.bureaucrat = Bureaucrat()
- self.timer = datetime.datetime(2023, 9, 17, 8, 0)
- while True:
- self.act()
- def act(self):
- '''An action taken by the player.'''
- # the following loop numerically lists all available actions
- for number, action in enumerate(self.character.actions):
- number = str(number + 1)
- if int(number) < 10:
- print('0' + number + '. ' + action)
- else:
- print(number + '. ' + action)
- self.action = input('\nIt is ' +
- t.bold('{}'.format(self.timer.strftime('%H:%M'))) +
- '.\nWhat will you do? ').lower()
- if self.action == 'q': # quits the game
- self.quit()
- elif self.action == 'w': # creates a new bureaucrat
- self.timer_add(random.choice(range(800, 1001)))
- self.evaluate_time() # checks if the player ran out of time
- self.bureaucrat = Bureaucrat()
- elif self.action in 'abcdefhijklmpstyw':
- self.react() # finds out what type of action was chosen
- self.evaluate_time()
- self.calculate_odds() # alters the odds of success
- self.evaluate_success() # determines success or failure
- else: # incorrect input is given
- print('\nIncorrect action. Please try again.\n')
- def react(self):
- '''A reaction to the choice of the player.'''
- if self.action == 'bt': # character.py for reference
- self.character.probability_is_low()
- self.character.asc_rank_asc_mood()
- self.timer_add(random.choice(range(200, 401)))
- elif self.action in 'cp':
- self.character.probability_is_high()
- self.character.desc_rank_desc_mood()
- self.timer_add(random.choice(range(500, 701)))
- elif self.action in 'dm':
- self.character.probability_is_low()
- self.character.desc_rank_desc_mood()
- self.timer_add(random.choice(range(350, 551)))
- elif self.action in 'efj':
- self.character.probability_is_high()
- self.character.asc_rank_asc_mood()
- self.timer_add(random.choice(range(650, 851)))
- elif self.action in 'hkly':
- self.character.probability_is_low()
- self.character.desc_rank_asc_mood()
- self.timer_add(random.choice(range(350, 551)))
- elif self.action in 'is':
- self.character.probability_is_high()
- self.character.asc_rank_desc_mood()
- self.timer_add(random.choice(range(500, 701)))
- else: # 'a' (ask)
- self.character.probability_is_high()
- self.character.desc_rank_asc_mood()
- self.timer_add(random.choice(range(650, 851)))
- def evaluate_time(self):
- '''Checks whether you still have time left.'''
- if self.timer - datetime.datetime(2023, 9, 17, 8, 0) >= \
- datetime.timedelta(seconds=43200):
- self.game_over()
- else:
- pass
- def calculate_odds(self):
- '''Calculates the odds of a successful action.'''
- # odds of success are randomly assigned to be 25-75% at first
- self.odds = float(random.choice(range(25, 76)))
- if self.bureaucrat.rank == 'low':
- if self.character.asc_rank is True: # character.py for reference
- self.odds /= self.character.divisor
- else:
- self.odds *= self.character.multiplier
- elif self.bureaucrat.rank == 'high':
- if self.character.asc_rank is True:
- self.odds *= self.character.multiplier
- else:
- self.odds /= self.character.divisor
- else:
- pass # medium rank doesn't affect the odds
- if self.bureaucrat.mood == 'bad':
- if self.character.asc_mood is True:
- self.odds /= self.character.divisor
- else:
- self.odds *= self.character.multiplier
- elif self.bureaucrat.mood == 'good':
- if self.character.asc_mood is True:
- self.odds *= self.character.multiplier
- else:
- self.odds /= self.character.divisor
- else:
- pass # average mood doesn't affect the odds
- def evaluate_success(self):
- '''Evaluates whether the action is successful or not.'''
- # the higher self.odds value
- # the higher the chance the action will succeed
- self.luck = random.choice(range(0, 101))
- if self.luck > self.odds:
- self.bureaucrat.react_negatively()
- if self.action in 'bkty': # your risky action was unsuccessful
- self.game_over()
- else:
- self.bureaucrat.react_positively()
- def timer_add(self, seconds_passed):
- '''Adds the time the action took to the timer.'''
- self.timer += datetime.timedelta(seconds=seconds_passed)
- def clear_screen(self):
- '''Clears the screen of the terminal.'''
- if os.name == 'nt': # 'nt' equals Windows
- os.system('cls')
- else:
- os.system('clear') # Mac OS and Linux
- def game_over(self):
- '''Called when the game is over.'''
- self.clear_screen()
- if self.action in 'bkty':
- print('Game over! You were handed over to the police ' +
- 'and arrested.\nYou will have to try again next day.')
- else: # the time is up
- print('Game over! The Instituion is now closed for today.' +
- '\nYou will have to try again next day.')
- self.choice = input("\nType 'r' if you want to restart: ").lower()
- if self.choice == 'r': # restarts the game from the beginning
- os.execl(sys.executable, sys.executable, * sys.argv)
- else:
- self.quit()
- def quit(self):
- '''Quits the game.'''
- self.clear_screen()
- sys.exit('Thank you for playing LAM-40.\n')
- Game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement