#!/usr/bin/python # -*- coding: utf-8 -*- # # A GTK Python GO client import os import pygame from pygame.locals import * def load_png(name): """ 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() 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['b'] = load_png('go_b.png') ret['bT'] = load_png('go_b.png') ret['bT'].blit(triangle, (0,0)) 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 class GobanSquare: """A single square on the go board""" def __init__(self, pos): self.x, self.y = pos self.state = 'empty' if (self.x, self.y) == (1,1): self.default_draw_code = 'ul' elif (self.x, self.y) == (1,19): self.default_draw_code = 'ur' elif (self.x, self.y) == (19,1): self.default_draw_code = 'dl' elif (self.x, self.y) == (19,19): self.default_draw_code = 'dr' elif (self.x, self.y) in [(4,4), (4,16), (16,4), (16,16)]: self.default_draw_code = 'h' elif self.x == 1: self.default_draw_code = 'u' elif self.y == 1: self.default_draw_code = 'l' elif self.x == 19: self.default_draw_code = 'd' elif self.y == 19: self.default_draw_code = 'r' else: self.default_draw_code = 'm' def get_draw_code(self): if self.state == 'empty': return self.default_draw_code if self.state == 'marked': return self.default_draw_code + 'c' if self.state == 'white': return 'w' if self.state == 'black': return 'b' return None def draw_board(goban, size, img_res): board = pygame.Surface((size,size)) inc = size / 19; i = 0 for row in goban: j = 0 for square in row: s = pygame.transform.scale(img_res[square.get_draw_code()], (inc, inc)) board.blit(s, (j*inc,i*inc)) j += 1 i += 1 return board.convert() def main(): # Basic screen init pygame.init() screen = pygame.display.set_mode((800, 800)) pygame.display.set_caption('pyGo') # 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() # Build the 361 board sprites goban = [] for i in range(19): goban.append([]) for j in range(19): goban[i].append(GobanSquare((i+1, j+1))) board_size = 800 board_inc = board_size / 19 board = draw_board(goban, board_size, img_res) background.blit(board, (0,0)) screen.blit(background, (0, 0)) pygame.display.flip() while True: event = pygame.event.wait() if event.type == QUIT: return if event.type == MOUSEBUTTONDOWN: if event.button == 1: state = 'black' if event.button == 2: state = 'marked' if event.button == 3: state = 'white' x, y = event.pos goban[y / board_inc][x / board_inc].state = state board = draw_board(goban, board_size, img_res) background.blit(board, (0,0)) screen.blit(background, (0, 0)) pygame.display.flip() if __name__ == '__main__': main()