Allow legacy terminal play

master
En Yi 2019-06-15 12:40:43 +01:00
parent 1d17b2ccda
commit 33fc0b1a5c
3 changed files with 93 additions and 95 deletions

55
game.py 100644
View File

@ -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()

83
main.py
View File

@ -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()

View File

@ -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,6 +465,7 @@ class Table:
1 * (not self.first_player)) % NUM_OF_PLAYERS)
self.write_message(msg, line=2, delay_time=1)
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
@ -477,6 +481,7 @@ class Table:
if not self.require_player_input:
if not self.players[self.current_player].AI:
self.require_player_input = True
if not self.terminal_play:
self.calling_panel.visible = True
self.update_table.emit()
return
@ -488,6 +493,7 @@ class Table:
if player_bid < 0:
return False
self.require_player_input = False
if not self.terminal_play:
self.calling_panel.visible = False
self.update_table.emit()
@ -517,6 +523,7 @@ class Table:
time.sleep(0.5)
if self.passes == NUM_OF_PLAYERS - 1 or self.table_status["bid"] == 75:
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
@ -530,6 +537,7 @@ class Table:
self.display_current_player(self.current_player)
if not self.players[self.current_player].AI:
self.require_player_input = True
if not self.terminal_play:
self.calling_panel.visible = True
self.update_table.emit()
return False
@ -537,13 +545,13 @@ class Table:
# 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
if not self.terminal_play:
self.calling_panel.visible = False
self.update_table.emit()