See previous commit...

This commit is contained in:
Anna Rose 2012-04-16 13:00:56 -04:00
parent 17a49169eb
commit 704e7209a6
3 changed files with 116 additions and 76 deletions

11
lib/config.py Normal file
View File

@ -0,0 +1,11 @@
import ConfigParser
def init():
global config
config = read_config_file()
def read_config_file():
ret = ConfigParser.ConfigParser()
ret.read('pygo.cfg')
return ret

View File

@ -1,7 +1,3 @@
import pygame
from pygame.locals import *
class Goban:
"""Represents the go board. Handles stone placement, captures, etc"""
@ -263,71 +259,71 @@ class Goban:
def draw_board(self, size, img_res):
ret = pygame.Surface((size,size))
# def draw_board(self, size, img_res):
# ret = pygame.Surface((size,size))
inc = size / self.board_size;
# inc = size / self.board_size;
for pos in range(len(self.board)):
point = self.board[pos]
# for pos in range(len(self.board)):
# point = self.board[pos]
if point == Goban.EMPTY:
code = self.def_draw_codes[pos]
elif point == Goban.BLACK:
code = 'b'
elif point == Goban.WHITE:
code = 'w'
# if point == Goban.EMPTY:
# code = self.def_draw_codes[pos]
# elif point == Goban.BLACK:
# code = 'b'
# elif point == Goban.WHITE:
# code = 'w'
if pos == self.last_move:
code = code + 'T'
# if pos == self.last_move:
# code = code + 'T'
if pos == self.ko:
code = code + 'C'
# if pos == self.ko:
# code = code + 'C'
s = img_res[code]
s = pygame.transform.scale(s, (inc, inc))
ret.blit(s, ((pos % self.board_size) *inc, (pos / self.board_size) *inc))
# s = img_res[code]
# s = pygame.transform.scale(s, (inc, inc))
# ret.blit(s, ((pos % self.board_size) *inc, (pos / self.board_size) *inc))
if self.hover == pos:
c = img_res['bH']
if self.to_move == Goban.WHITE:
c = img_res['wH']
c = pygame.transform.scale(c, (inc, inc))
ret.blit(c, ((pos % self.board_size) *inc, (pos / self.board_size) *inc))
# if self.hover == pos:
# c = img_res['bH']
# if self.to_move == Goban.WHITE:
# c = img_res['wH']
# c = pygame.transform.scale(c, (inc, inc))
# ret.blit(c, ((pos % self.board_size) *inc, (pos / self.board_size) *inc))
return ret.convert_alpha()
# return ret.convert_alpha()
def draw_info(self):
textbox = pygame.Surface((150, 300))
textbox = textbox.convert()
textbox.fill((250, 250, 250))
# def draw_info(self):
# textbox = pygame.Surface((150, 300))
# textbox = textbox.convert()
# 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))
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))
if self.to_move == Goban.BLACK:
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))
# 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))
# 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))
# if self.to_move == Goban.BLACK:
# 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(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))
return textbox
# return textbox

75
pygo.py
View File

@ -6,34 +6,67 @@
import sys
sys.path.append('lib/')
import ConfigParser
import goban
import pygogui
import config
import gtk, gtk.glade
class Pygo():
"""This class handles the main interface, defines basic callbacks"""
def __init__(self):
self.resize = True
self.goban = None
self.network_mode = False
self.our_color = None
self.init_user_interface('./ui/default.glade')
self.init_widgets()
def init_user_interface(self, path_to_skin):
self.tree=gtk.glade.XML(path_to_skin, "window")
self.tree.signal_autoconnect(self)
self.window = self.tree.get_widget('window')
def init_widgets(self):
self.window.resize(1000,800)
# gobject.timeout_add(1000, self.update)
def on_local_new(self, widget):
if self.goban:
del self.goban
self.goban = goban.Goban()
self.network_mode = False
self.our_color = None
def on_net_direct(self, widget):
print 'stub: Pygo.on_menu_net_direct()'
def on_quit(self, widget):
sys.exit(0)
def main():
# Read config file
settings = read_config_file()
config.init()
# Data
gb = goban.Goban()
gui = pygogui.GUI(gb, settings)
# base_icon = gtk.gdk.pixbuf_new_from_file('ui/icon.svg')
# icon = base_icon.scale_simple(128, 128, gtk.gdk.INTERP_BILINEAR)
# gtk.window_set_default_icon(icon)
go_obj = Pygo()
gui.update()
while True:
# All of the real work happens in pygogui
# It keeps a copy of all the relevant data
gui.do_event()
gui.update()
def read_config_file():
ret = ConfigParser.ConfigParser()
ret.read('pygo.cfg')
return ret
# Let's see if we can avoid using threads in this implementation
# gtk.gdk.threads_init()
# gtk.gdk.threads_enter()
gtk.main()
# gtk.gdk.threads_leave()
if __name__ == '__main__': main()