From 90ff7ceba87b90f64c880fd050eded01583d40a7 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Sat, 14 Apr 2012 18:28:34 -0400 Subject: [PATCH] Added pass, resign, and quit buttons using SGC --- lib/goban.py | 39 +++++++++++++++++++++++++++++++++--- pygo.py | 56 +++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 76 insertions(+), 19 deletions(-) diff --git a/lib/goban.py b/lib/goban.py index 94cca51..9b2761d 100644 --- a/lib/goban.py +++ b/lib/goban.py @@ -24,9 +24,11 @@ class Goban: self.black_captures = 0 self.white_captures = 0 self.last_move = None + self.passed_last = False self.ko = None self.hover = None self.elapsed_time = 0 + self.winner = Goban.EMPTY def set_hover(self, pos): @@ -34,7 +36,7 @@ class Goban: if rpos == self.hover: return - if not self._valid_move(rpos): + if not self._valid_move(rpos) or self.to_move == Goban.EMPTY: self.clear_hover() return @@ -46,6 +48,9 @@ class Goban: def play_move(self, pos): + if self.to_move == Goban.EMPTY: + return + rpos = self._real_pos(pos) if not self._valid_move(rpos): @@ -54,12 +59,33 @@ class Goban: self.board[rpos] = self.to_move self._capture(rpos) self.last_move = rpos + self.passed_last = False self.to_move = self._other_color(self.to_move) self.clear_hover() + # fixme: need to handle post-game stuff here... scoring code + def pass_move(self): + if self.passed_last: + self.to_move = Goban.EMPTY + else: + self.to_move = self._other_color(self.to_move) + self.passed_last = True + + self.last_move = None + self.ko = None + + + def resign(self): + self.passed_last = False + self.last_move = None + self.ko = None + self.winner = self._other_color(self.to_move) + self.to_move = Goban.EMPTY + + def _capture(self, pos): """Look for stones captured on the 4 sides of pos, remove them and increment capture counter. This pos must be a *real* position value, not an x,y tuple.""" @@ -240,7 +266,7 @@ class Goban: textbox.fill((250, 250, 250)) font = pygame.font.Font(None, 24) - time = font.render('Time: {:02d}:{:02d}'.format(self.elapsed_time / 60, self.elapsed_time % 60), 1, (10, 10, 10)) + # time = font.render('Time: {:02d}:{:02d}'.format(self.elapsed_time / 60, self.elapsed_time % 60), 1, (10, 10, 10)) heading = font.render('Captures', 1, (10, 10, 10)) black_cap = font.render('Black: {}'.format(self.black_captures), 1, (10, 10, 10)) white_cap = font.render('White: {}'.format(self.white_captures), 1, (10, 10, 10)) @@ -248,12 +274,19 @@ class Goban: turn = font.render('To move: Black', 1, (10, 10, 10)) elif self.to_move == Goban.WHITE: turn = font.render('To move: White', 1, (10, 10, 10)) + else: + if self.winner == Goban.WHITE: + turn = font.render('Winner: White', 1, (10, 10, 10)) + elif self.winner == Goban.BLACK: + turn = font.render('Winner: Black', 1, (10, 10, 10)) + else: + turn = font.render('Scoring', 1, (10, 10, 10)) textbox.blit(heading, (0, 0)) textbox.blit(black_cap, (0, 28)) textbox.blit(white_cap, (0, 56)) textbox.blit(turn, (0, 100)) - textbox.blit(time, (0, 150)) + # textbox.blit(time, (0, 150)) return textbox diff --git a/pygo.py b/pygo.py index 354fad1..6ae8850 100755 --- a/pygo.py +++ b/pygo.py @@ -11,6 +11,8 @@ import math import pygame from pygame.locals import * +import sgc +from sgc.locals import * import goban @@ -69,36 +71,52 @@ def magnitude(vector): def main(): # Basic screen init pygame.init() - screen = pygame.display.set_mode((1000, 800)) +# screen = pygame.display.set_mode((1000, 800)) + screen = sgc.surface.Screen((1000,800)) pygame.display.set_caption('pyGo') + + # SGC font color + sgc.Font.col = (10,10,10) # Create the background object, make it blank - background = pygame.Surface(screen.get_size()) - background = background.convert() - background.fill((250, 250, 250)) + # background = pygame.Surface(screen.get_size()) + # background = background.convert() + # background.fill((250, 250, 250)) # Build the dict of image objects img_res = build_img_res() - + board_size = 800 board_inc = board_size / 19 gb = goban.Goban() + screen.fill((250, 250, 250)) board = gb.draw_board(board_size, img_res) - background.blit(board, (0,0)) - + screen.blit(board, (0,0)) text = gb.draw_info() - background.blit(text, (815, 25)) + screen.blit(text, (815, 25)) - screen.blit(background, (0, 0)) + pass_btn = sgc.widgets.Button(label="Pass", pos=(850,500)) + pass_btn.activate = gb.pass_move + pass_btn.add() + + resign_btn = sgc.widgets.Button(label="Resign", pos=(850,600)) + resign_btn.activate = gb.resign + resign_btn.add() + + quit_btn = sgc.widgets.Button(label="Quit", pos=(850,700)) + quit_btn.activate = sys.exit + quit_btn.add() + pygame.display.flip() - pygame.time.set_timer(USEREVENT, 1000) + # pygame.time.set_timer(USEREVENT, 1000) while True: event = pygame.event.wait() - + sgc.widgets.event(event) + if event.type == QUIT: return @@ -127,16 +145,22 @@ def main(): if event.button == 1: gb.play_move((row, col)) - if event.type == USEREVENT: - gb.elapsed_time += 1 + # if event.type == USEREVENT: + # gb.elapsed_time += 1 + + + # Cleanup removed windows + # for widget in dialogs: + # if not widget.active(): + # dialogs.remove(widget) board = gb.draw_board(board_size, img_res) - background.blit(board, (0,0)) + screen.blit(board, (0,0)) text = gb.draw_info() - background.blit(text, (815, 25)) + screen.blit(text, (815, 25)) - screen.blit(background, (0, 0)) + sgc.widgets.update(0) pygame.display.flip()