130 lines
3.0 KiB
Python
Executable File
130 lines
3.0 KiB
Python
Executable File
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
#
|
|
# A pygame-based Python GO client
|
|
|
|
import sys
|
|
sys.path.append('lib/')
|
|
|
|
import os
|
|
import pygame
|
|
from pygame.locals import *
|
|
from goban import *
|
|
|
|
|
|
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 main():
|
|
# Basic screen init
|
|
pygame.init()
|
|
screen = pygame.display.set_mode((1000, 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()
|
|
|
|
board_size = 800
|
|
board_inc = board_size / 19
|
|
|
|
goban = Goban()
|
|
|
|
board = goban.draw_board(board_size, img_res)
|
|
background.blit(board, (0,0))
|
|
|
|
text = goban.draw_info()
|
|
background.blit(text, (815, 25))
|
|
|
|
screen.blit(background, (0, 0))
|
|
pygame.display.flip()
|
|
|
|
while True:
|
|
event = pygame.event.wait()
|
|
|
|
if event.type == QUIT:
|
|
return
|
|
|
|
# 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 x <= board_size:
|
|
goban.set_hover((col,row))
|
|
else:
|
|
goban.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:
|
|
goban.play_move((col, row))
|
|
|
|
|
|
board = goban.draw_board(board_size, img_res)
|
|
background.blit(board, (0,0))
|
|
|
|
text = goban.draw_info()
|
|
background.blit(text, (815, 25))
|
|
|
|
screen.blit(background, (0, 0))
|
|
pygame.display.flip()
|
|
|
|
|
|
|
|
if __name__ == '__main__': main()
|