Added pass, resign, and quit buttons using SGC

This commit is contained in:
Anna Rose 2012-04-14 18:28:34 -04:00
parent db8eb7aa50
commit 90ff7ceba8
2 changed files with 76 additions and 19 deletions

View File

@ -24,9 +24,11 @@ class Goban:
self.black_captures = 0 self.black_captures = 0
self.white_captures = 0 self.white_captures = 0
self.last_move = None self.last_move = None
self.passed_last = False
self.ko = None self.ko = None
self.hover = None self.hover = None
self.elapsed_time = 0 self.elapsed_time = 0
self.winner = Goban.EMPTY
def set_hover(self, pos): def set_hover(self, pos):
@ -34,7 +36,7 @@ class Goban:
if rpos == self.hover: if rpos == self.hover:
return return
if not self._valid_move(rpos): if not self._valid_move(rpos) or self.to_move == Goban.EMPTY:
self.clear_hover() self.clear_hover()
return return
@ -46,6 +48,9 @@ class Goban:
def play_move(self, pos): def play_move(self, pos):
if self.to_move == Goban.EMPTY:
return
rpos = self._real_pos(pos) rpos = self._real_pos(pos)
if not self._valid_move(rpos): if not self._valid_move(rpos):
@ -54,12 +59,33 @@ class Goban:
self.board[rpos] = self.to_move self.board[rpos] = self.to_move
self._capture(rpos) self._capture(rpos)
self.last_move = rpos self.last_move = rpos
self.passed_last = False
self.to_move = self._other_color(self.to_move) self.to_move = self._other_color(self.to_move)
self.clear_hover() 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): def _capture(self, pos):
"""Look for stones captured on the 4 sides of pos, remove them and increment """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.""" 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)) textbox.fill((250, 250, 250))
font = pygame.font.Font(None, 24) 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)) heading = font.render('Captures', 1, (10, 10, 10))
black_cap = font.render('Black: {}'.format(self.black_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)) 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)) turn = font.render('To move: Black', 1, (10, 10, 10))
elif self.to_move == Goban.WHITE: elif self.to_move == Goban.WHITE:
turn = font.render('To move: White', 1, (10, 10, 10)) 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(heading, (0, 0))
textbox.blit(black_cap, (0, 28)) textbox.blit(black_cap, (0, 28))
textbox.blit(white_cap, (0, 56)) textbox.blit(white_cap, (0, 56))
textbox.blit(turn, (0, 100)) textbox.blit(turn, (0, 100))
textbox.blit(time, (0, 150)) # textbox.blit(time, (0, 150))
return textbox return textbox

52
pygo.py
View File

@ -11,6 +11,8 @@ import math
import pygame import pygame
from pygame.locals import * from pygame.locals import *
import sgc
from sgc.locals import *
import goban import goban
@ -69,13 +71,17 @@ def magnitude(vector):
def main(): def main():
# Basic screen init # Basic screen init
pygame.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') pygame.display.set_caption('pyGo')
# SGC font color
sgc.Font.col = (10,10,10)
# Create the background object, make it blank # Create the background object, make it blank
background = pygame.Surface(screen.get_size()) # background = pygame.Surface(screen.get_size())
background = background.convert() # background = background.convert()
background.fill((250, 250, 250)) # background.fill((250, 250, 250))
# Build the dict of image objects # Build the dict of image objects
img_res = build_img_res() img_res = build_img_res()
@ -85,19 +91,31 @@ def main():
gb = goban.Goban() gb = goban.Goban()
screen.fill((250, 250, 250))
board = gb.draw_board(board_size, img_res) board = gb.draw_board(board_size, img_res)
background.blit(board, (0,0)) screen.blit(board, (0,0))
text = gb.draw_info() text = gb.draw_info()
background.blit(text, (815, 25)) screen.blit(text, (815, 25))
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()
screen.blit(background, (0, 0))
pygame.display.flip() pygame.display.flip()
pygame.time.set_timer(USEREVENT, 1000) # pygame.time.set_timer(USEREVENT, 1000)
while True: while True:
event = pygame.event.wait() event = pygame.event.wait()
sgc.widgets.event(event)
if event.type == QUIT: if event.type == QUIT:
return return
@ -127,16 +145,22 @@ def main():
if event.button == 1: if event.button == 1:
gb.play_move((row, col)) gb.play_move((row, col))
if event.type == USEREVENT: # if event.type == USEREVENT:
gb.elapsed_time += 1 # 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) board = gb.draw_board(board_size, img_res)
background.blit(board, (0,0)) screen.blit(board, (0,0))
text = gb.draw_info() 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() pygame.display.flip()