Refactored gui code into a class to make it more managable

This commit is contained in:
Anna Rose 2012-04-15 00:20:39 -04:00
parent 6e2f1649c8
commit 16061abf0a
2 changed files with 161 additions and 153 deletions

156
lib/pygogui.py Normal file
View File

@ -0,0 +1,156 @@
import os
import sys
import math
import pygame
from pygame.locals import *
import sgc
from sgc.locals import *
class GUI:
def __init__(self, goban):
# Basic screen init
pygame.init()
# screen = pygame.display.set_mode((1000, 800))
self.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))
# Build the dict of image objects
self.img_res = _build_img_res()
self.board_size = 800
self.board_inc = self.board_size / 19
self.screen.fill((250, 250, 250))
self.pass_btn = sgc.widgets.Button(label="Pass", pos=(850,500))
self.pass_btn.activate = goban.pass_move
self.pass_btn.add()
self.resign_btn = sgc.widgets.Button(label="Resign", pos=(850,600))
self.resign_btn.activate = goban.resign
self.resign_btn.add()
self.quit_btn = sgc.widgets.Button(label="Quit", pos=(850,700))
self.quit_btn.activate = sys.exit
self.quit_btn.add()
# pygame.time.set_timer(USEREVENT, 1000)
def do_event(self, goban, network_mode, our_color):
event = pygame.event.wait()
sgc.widgets.event(event)
if event.type == QUIT:
return
# This set of events should only be called if we can currently play
if network_mode == False or goban.to_move == our_color:
# Hover a transparent stone over our
# cursor position, assuming play is legal
if event.type == MOUSEMOTION:
x, y = event.pos
row = y / self.board_inc
col = x / self.board_inc
if _magnitude(event.rel) < 3:
if x <= self.board_size:
goban.set_hover((row,col))
else:
goban.clear_hover()
elif goban.hover != goban._real_pos((row,col)):
goban.clear_hover()
# Place a stone on left-click
if event.type == MOUSEBUTTONDOWN:
x, y = event.pos
row = y / self.board_inc
col = x / self.board_inc
if x <= self.board_size:
if event.button == 1:
goban.play_move((row, col))
# if event.type == USEREVENT:
# goban.elapsed_time += 1
# Cleanup removed windows
# for widget in dialogs:
# if not widget.active():
# dialogs.remove(widget)
def update(self, goban):
board = goban.draw_board(self.board_size, self.img_res)
self.screen.blit(board, (0,0))
text = goban.draw_info()
self.screen.blit(text, (815, 25))
sgc.widgets.update(0)
pygame.display.flip()
def _magnitude(vector):
x,y = vector
return math.sqrt(x*x + y*y)
def _load_png(name, alpha=None):
""" Load image and return image object"""
fullname = os.path.join('res', name)
try:
image = pygame.image.load(fullname)
if image.get_alpha() is None:
image = image.convert()
else:
image = image.convert_alpha()
if alpha is not None:
image.set_alpha(alpha)
except pygame.error, message:
print 'Cannot load image:', fullname
raise SystemExit, message
return image
def _build_img_res():
ret = {}
triangle = _load_png('go_t.png')
ret['w'] = _load_png('go_w.png')
ret['wT'] = _load_png('go_w.png')
ret['wT'].blit(triangle, (0,0))
ret['wH'] = _load_png('go_w.png', 128)
ret['b'] = _load_png('go_b.png')
ret['bT'] = _load_png('go_b.png')
ret['bT'].blit(triangle, (0,0))
ret['bH'] = _load_png('go_b.png', 128)
circle = _load_png('go_c.png')
for d in ('u', 'd', 'l', 'r', 'm', 'dl', 'dr', 'ul', 'ur', 'h'):
ret[d] = _load_png('go_' + d + '.png')
ret[d + 'C'] = _load_png('go_' + d + '.png')
ret[d + 'C'].blit(circle, (0,0))
return ret

158
pygo.py
View File

@ -6,170 +6,22 @@
import sys import sys
sys.path.append('lib/') sys.path.append('lib/')
import os
import math
import pygame
from pygame.locals import *
import sgc
from sgc.locals import *
import goban import goban
import pygogui
def load_png(name, alpha=None):
""" Load image and return image object"""
fullname = os.path.join('res', name)
try:
image = pygame.image.load(fullname)
if image.get_alpha() is None:
image = image.convert()
else:
image = image.convert_alpha()
if alpha is not None:
image.set_alpha(alpha)
except pygame.error, message:
print 'Cannot load image:', fullname
raise SystemExit, message
return image
def build_img_res():
ret = {}
triangle = load_png('go_t.png')
ret['w'] = load_png('go_w.png')
ret['wT'] = load_png('go_w.png')
ret['wT'].blit(triangle, (0,0))
ret['wH'] = load_png('go_w.png', 128)
ret['b'] = load_png('go_b.png')
ret['bT'] = load_png('go_b.png')
ret['bT'].blit(triangle, (0,0))
ret['bH'] = load_png('go_b.png', 128)
circle = load_png('go_c.png')
for d in ('u', 'd', 'l', 'r', 'm', 'dl', 'dr', 'ul', 'ur', 'h'):
ret[d] = load_png('go_' + d + '.png')
ret[d + 'C'] = load_png('go_' + d + '.png')
ret[d + 'C'].blit(circle, (0,0))
return ret
def magnitude(vector):
x,y = vector
return math.sqrt(x*x + y*y)
def main(): def main():
# Basic screen init
pygame.init()
# 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))
# Build the dict of image objects
img_res = build_img_res()
# Data # Data
gb = goban.Goban() gb = goban.Goban()
network_mode = False network_mode = False
our_color = None our_color = None
gui = pygogui.GUI(gb)
board_size = 800 gui.update(gb)
board_inc = board_size / 19
screen.fill((250, 250, 250))
board = gb.draw_board(board_size, img_res)
screen.blit(board, (0,0))
text = gb.draw_info()
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()
pygame.display.flip()
# pygame.time.set_timer(USEREVENT, 1000)
while True: while True:
event = pygame.event.wait() event = gui.do_event(gb, network_mode, our_color)
sgc.widgets.event(event) gui.update(gb)
if event.type == QUIT:
return
# This set of events should only be called if we can currently play
if network_mode == False or gb.to_move == our_color:
# Hover a transparent stone over our
# cursor position, assuming play is legal
if event.type == MOUSEMOTION:
x, y = event.pos
row = y / board_inc
col = x / board_inc
if magnitude(event.rel) < 3:
if x <= board_size:
gb.set_hover((row,col))
else:
gb.clear_hover()
elif gb.hover != gb._real_pos((row,col)):
gb.clear_hover()
# Place a stone on left-click
if event.type == MOUSEBUTTONDOWN:
x, y = event.pos
row = y / board_inc
col = x / board_inc
if x <= board_size:
if event.button == 1:
gb.play_move((row, col))
# 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)
screen.blit(board, (0,0))
text = gb.draw_info()
screen.blit(text, (815, 25))
sgc.widgets.update(0)
pygame.display.flip()
if __name__ == '__main__': main() if __name__ == '__main__': main()