scoundrel/main.py
2025-05-13 13:55:07 -04:00

163 lines
5.3 KiB
Python

import os
import pickle
import sys
import termios
from game import Game
from assets.ascii_art import START_SCREEN, GAME_OVER
from assets.help import PAGE_ONE, PAGE_TWO, PAGE_THREE, PAGE_FOUR, PAGE_FIVE, PAGE_SIX, PAGE_SEVEN
from pynput.keyboard import Key, Listener, KeyCode
class Main:
def __init__(self):
self.screen_mode = True
self.game = None
self.username = 'Wayne Skyler'
self.save_file = 'save.pickle'
self.save_data = None
self.help_screens = [PAGE_ONE, PAGE_TWO, PAGE_THREE, PAGE_FOUR, PAGE_FIVE, PAGE_SIX, PAGE_SEVEN]
self.current_help_screen = 0
self.on_help_screen = False
self.from_screen = None
if not os.path.isfile(self.save_file):
with open(self.save_file, 'wb') as f:
save_data = {
'username': self.username,
'high_scores': []
}
pickle.dump(save_data, f)
else:
with open(self.save_file, 'rb') as f:
self.save_data = pickle.load(f)
self.username = self.save_data['username']
def main(self):
self.screens()
def screens(self, mode=None):
while self.screen_mode:
if self.game and mode != 'win':
if self.game.health <= 0:
os.system('clear')
self.game.store_high_score()
print(GAME_OVER.format(score=self.game.score))
self.input(mode='game_over')
else:
os.system('clear')
if mode == 'win':
self.game.health = 0
self.game.flash_message(f'\n\n\n\twin. Congratulashon. {self.game.score}')
self.game = None
self.screens(mode=None)
else:
print(START_SCREEN.format(username=self.username))
self.input(mode='start')
def gameplay(self):
self.game = Game(username=self.username)
self.game.initialize()
while self.game.health > 0:
if not self.game.paused and not self.game.on_help_screen:
self.game.cleanup()
if self.game.check_win():
self.screen_mode = True
self.screens(mode='win')
if None in self.game.current_room_cards:
breakpoint()
self.game = None
self.game.display()
self.game.input()
else:
if self.game.paused:
self.pause_game()
self.game.input()
elif self.game.on_help_screen:
self.game.help_screen()
self.game.input()
self.screen_mode = True
self.screens()
def pause_game(self):
os.system('clear')
print('\n\n\t PAUSED')
print('\t Press p to unpause...')
def high_scores(self):
with open(self.save_file, 'rb') as f:
save_data = pickle.load(f)
os.system('clear')
print('\t\tHIGH SCORES\n')
for score in save_data['high_scores']:
print(f"\t{score[0]}\t{score[1]}")
print('\n\tPress Enter to Return to Start Screen \n')
self.input('game_over')
def help_screen(self):
os.system('clear')
print(self.help_screens[self.current_help_screen])
def input(self, mode='start'):
def on_press(key):
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
if key == Key.enter:
if mode == 'start':
self.screen_mode = False
self.gameplay()
if mode == 'game_over':
self.game = None
self.screen_mode = True
self.screens()
if key == Key.esc:
sys.exit()
if key == KeyCode.from_char('h'):
self.high_scores()
if key == KeyCode.from_char('u'):
self.username = input('Input Your Username: ')
with open(self.save_file, 'rb') as f:
self.save_data = pickle.load(f)
self.save_data['username'] = self.username
with open(self.save_file, 'wb') as f:
pickle.dump(self.save_data, f)
self.screens()
if key == KeyCode.from_char('?'):
if mode != 'game_help':
self.on_help_screen = True if not self.on_help_screen else False
if self.on_help_screen:
self.help_screen()
else:
self.screens()
elif mode == 'game_help':
self.game.on_help_screen = True if not self.game.on_help_screen else False
if key == Key.up:
if self.current_help_screen != 0:
self.current_help_screen -= 1
self.help_screen()
if key == Key.down:
if self.current_help_screen < len(self.help_screens) - 1:
self.current_help_screen += 1
self.help_screen()
with Listener(on_press=on_press) as listener:
listener.join()
if __name__ == '__main__':
main = Main()
main.main()