From 33fc0b1a5c89ec45d087e1e31873edff63648b86 Mon Sep 17 00:00:00 2001 From: En Yi Date: Sat, 15 Jun 2019 12:40:43 +0100 Subject: [PATCH] Allow legacy terminal play --- game.py | 55 +++++++++++++++++++++++++++++++++++++ main.py | 83 ++++++-------------------------------------------------- table.py | 50 ++++++++++++++++++++-------------- 3 files changed, 93 insertions(+), 95 deletions(-) create mode 100644 game.py diff --git a/game.py b/game.py new file mode 100644 index 0000000..92728e9 --- /dev/null +++ b/game.py @@ -0,0 +1,55 @@ +import view +import pygame +import table + + +class GameScreen(view.PygView): + + def __init__(self, *args, autoplay=False, view_all_cards=False, terminal=False, **kwargs): + super().__init__(*args, **kwargs) + self.table = table.Table(0, 0, self.width, self.height, (0, 32, 0), + autoplay=autoplay, view_all_cards=view_all_cards, terminal=terminal) + self.table.update_table.connect(self.draw_table) + self.draw_table() + self.running = False + + def draw_table(self, **kwargs): + # TODO: optimise this by only redrawing the parts that changes + self.screen.blit(self.background, (0, 0)) + self.screen.blit(self.table.background, self.table.get_pos()) + for player in self.table.players: + self.screen.blit(player.deck_surface, player.get_pos()) + for playerzone in self.table.players_playzone: + self.screen.blit(playerzone.deck_surface, playerzone.get_pos()) + for i, announcer_line in enumerate(self.table.announcer_line): + self.screen.blit(announcer_line, (self.table.announcer_x, + self.table.announcer_y+self.table.announcer_height*i/3)) + for i, player_stats in enumerate(self.table.player_stats): + for j, stats_line in enumerate(player_stats): + self.screen.blit(stats_line, (self.table.player_stats_x[i], + self.table.player_stats_y[i]+self.table.stats_height*j/3)) + + for element in self.table.UI_elements: + if element.visible: + self.screen.blit(element.background, element.get_pos()) + + pygame.display.flip() + + def run(self): + self.running = True + while self.running: + all_events = pygame.event.get() + for event in all_events: + if event.type == pygame.QUIT: + self.running = False + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: + self.running = False + if event.key == pygame.K_p: + if not self.table.ongoing: + self.table.ongoing = True + self.table.process_UI(event) + if self.table.ongoing: + self.table.continue_game(all_events) + + pygame.quit() diff --git a/main.py b/main.py index b26a545..8acda9e 100644 --- a/main.py +++ b/main.py @@ -1,83 +1,16 @@ -import view -import pygame -import table import random import pickle import sys -#import queue -#import threading - -class GameScreen(view.PygView): - - def __init__(self, *args, autoplay=False, view_all_cards=False, **kwargs): - super().__init__(*args, **kwargs) - self.table = table.Table(0, 0, self.width, self.height, (0, 32, 0), - autoplay=autoplay, view_all_cards=view_all_cards) - self.table.update_table.connect(self.draw_table) - self.draw_table() - #self.player_commands = queue.Queue(1) - #self.player_thread = threading.Thread(target=self.get_player_inputs) - self.running = False - - def draw_table(self, **kwargs): - # TODO: optimise this by only redrawing the parts that changes - self.screen.blit(self.background, (0, 0)) - self.screen.blit(self.table.background, self.table.get_pos()) - for player in self.table.players: - self.screen.blit(player.deck_surface, player.get_pos()) - for playerzone in self.table.players_playzone: - self.screen.blit(playerzone.deck_surface, playerzone.get_pos()) - for i, announcer_line in enumerate(self.table.announcer_line): - self.screen.blit(announcer_line, (self.table.announcer_x, - self.table.announcer_y+self.table.announcer_height*i/3)) - for i, player_stats in enumerate(self.table.player_stats): - for j, stats_line in enumerate(player_stats): - self.screen.blit(stats_line, (self.table.player_stats_x[i], - self.table.player_stats_y[i]+self.table.stats_height*j/3)) - - for element in self.table.UI_elements: - if element.visible: - self.screen.blit(element.background, element.get_pos()) - - pygame.display.flip() - - - #def get_player_inputs(self): - # while self.running: - # if not self.player_commands.full(): - # player_cmd = input("Enter something:") - # self.player_commands.put(player_cmd) - - def run(self): - self.running = True - #self.player_thread.start() - while self.running: - draw_update = False - all_events = pygame.event.get() - for event in all_events: - if event.type == pygame.QUIT: - self.running = False - elif event.type == pygame.KEYDOWN: - if event.key == pygame.K_ESCAPE: - self.running = False - if event.key == pygame.K_p: - if not self.table.ongoing: - self.table.ongoing = True - self.table.process_UI(event) - if self.table.ongoing: - self.table.continue_game(all_events) - - - #if not self.player_commands.empty(): - # player_cmd = self.player_commands.get() - # print("Player Command Received: " + player_cmd) - - pygame.quit() +import game +""" +This script is to run the game. It would process any input argument and pass into the game. +""" if __name__ == '__main__': AUTOPLAY = False VIEW_ALL_CARDS = False + TERMINAL = False if len(sys.argv) > 1: prev_command = "" @@ -95,6 +28,8 @@ if __name__ == '__main__': VIEW_ALL_CARDS = True if command == "--auto" or command == "-a": AUTOPLAY = True + if command == "--terminal" or command == "-t": + TERMINAL = True prev_command = command rng_state = random.getstate() @@ -105,7 +40,7 @@ if __name__ == '__main__': # rng_state = pickle.load(f) #random.setstate(rng_state) - main_view = GameScreen(800, 600, clear_colour=(255, 0, 0), - autoplay=AUTOPLAY, view_all_cards=VIEW_ALL_CARDS) + main_view = game.GameScreen(800, 600, clear_colour=(255, 0, 0), + autoplay=AUTOPLAY, view_all_cards=VIEW_ALL_CARDS, terminal=TERMINAL) main_view.run() \ No newline at end of file diff --git a/table.py b/table.py index 95ae8de..72ce8a9 100644 --- a/table.py +++ b/table.py @@ -50,7 +50,7 @@ class Table: """ - def __init__(self, x, y, width, height, clear_colour, autoplay=False, view_all_cards=False): + def __init__(self, x, y, width, height, clear_colour, autoplay=False, view_all_cards=False, terminal=False): # TODO: Reduce the amount of update_table call self.update_table = Signal() self.x = x @@ -146,7 +146,10 @@ class Table: reveal_mode = cards.DeckReveal.SHOW_ALL if i == 0: - self.players.append(players.MainPlayer(playerx[i], playery[i], + player_class = players.MainPlayer + if terminal: + player_class = players.Player + self.players.append(player_class(playerx[i], playery[i], l_deck, w_deck, spacing, vert_orientation=vert, deck_reveal=reveal_mode)) @@ -193,6 +196,7 @@ class Table: self.ongoing = False self.require_player_input = False + self.terminal_play = terminal self.calling_panel = UI.CallPanel(playdeckx[0]+w_deck+5,playdecky[0]+w_deck-100, 220, 100) self.calling_panel.parent = self @@ -213,7 +217,6 @@ class Table: self.UI_elements = [self.calling_panel, self.yes_button, self.no_button] - def emit_call(self, output, **kwargs): pygame.event.post(pygame.event.Event(CALL_EVENT, call=output)) @@ -462,10 +465,11 @@ class Table: 1 * (not self.first_player)) % NUM_OF_PLAYERS) self.write_message(msg, line=2, delay_time=1) - self.calling_panel.list1.replace_list([str(i+1) for i in range(7)]) - self.calling_panel.list2.replace_list(['Clubs', 'Diamonds', 'Hearts', 'Spades', 'No Trump']) - self.calling_panel.cancel_button.visible = True - self.calling_panel.redraw() + if not self.terminal_play: + self.calling_panel.list1.replace_list([str(i+1) for i in range(7)]) + self.calling_panel.list2.replace_list(['Clubs', 'Diamonds', 'Hearts', 'Spades', 'No Trump']) + self.calling_panel.cancel_button.visible = True + self.calling_panel.redraw() def start_bidding(self, game_events): """ @@ -477,8 +481,9 @@ class Table: if not self.require_player_input: if not self.players[self.current_player].AI: self.require_player_input = True - self.calling_panel.visible = True - self.update_table.emit() + if not self.terminal_play: + self.calling_panel.visible = True + self.update_table.emit() return else: player_bid = self.players[self.current_player].make_decision(self.game_state, 0) @@ -488,8 +493,9 @@ class Table: if player_bid < 0: return False self.require_player_input = False - self.calling_panel.visible = False - self.update_table.emit() + if not self.terminal_play: + self.calling_panel.visible = False + self.update_table.emit() if not player_bid: if not self.first_player: # Starting bidder pass do not count at the start @@ -517,10 +523,11 @@ class Table: time.sleep(0.5) if self.passes == NUM_OF_PLAYERS - 1 or self.table_status["bid"] == 75: - self.calling_panel.list1.replace_list(['2','3','4','5','6','7','8','9','10','J','Q','K','A']) - self.calling_panel.list2.replace_list(['Clubs', 'Diamonds', 'Hearts', 'Spades']) - self.calling_panel.cancel_button.visible = False - self.calling_panel.redraw() + if not self.terminal_play: + self.calling_panel.list1.replace_list(['2','3','4','5','6','7','8','9','10','J','Q','K','A']) + self.calling_panel.list2.replace_list(['Clubs', 'Diamonds', 'Hearts', 'Spades']) + self.calling_panel.cancel_button.visible = False + self.calling_panel.redraw() return False else: if not self.require_player_input: @@ -530,22 +537,23 @@ class Table: self.display_current_player(self.current_player) if not self.players[self.current_player].AI: self.require_player_input = True - self.calling_panel.visible = True - self.update_table.emit() + if not self.terminal_play: + self.calling_panel.visible = True + self.update_table.emit() return False else: # Ask for the partner card self.table_status["partner"] = self.players[self.current_player].make_decision(self.game_state, 1) else: - partner = self.players[self.current_player].make_decision(self.game_state, 1, - game_events) + partner = self.players[self.current_player].make_decision(self.game_state, 1, game_events) if not partner: return False self.table_status["partner"] = partner self.require_player_input = False - self.calling_panel.visible = False - self.update_table.emit() + if not self.terminal_play: + self.calling_panel.visible = False + self.update_table.emit() # Setup the table status before the play starts self.table_status['partner reveal'] = False