Implemented hovering stones under mouse cursor, and implemented a simple game clock
This commit is contained in:
parent
cf1b892d21
commit
55dbed09f5
63
pygo.py
63
pygo.py
|
@ -8,7 +8,7 @@ import pygame
|
|||
from pygame.locals import *
|
||||
|
||||
|
||||
def load_png(name):
|
||||
def load_png(name, alpha=None):
|
||||
""" Load image and return image object"""
|
||||
fullname = os.path.join('res', name)
|
||||
|
||||
|
@ -19,6 +19,9 @@ def load_png(name):
|
|||
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
|
||||
|
@ -34,10 +37,12 @@ def build_img_res():
|
|||
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')
|
||||
|
||||
|
@ -122,6 +127,21 @@ class Goban:
|
|||
self.captures = []
|
||||
self.captures.append(0)
|
||||
self.captures.append(0)
|
||||
self.hover = None
|
||||
self.elapsed_time = 0
|
||||
|
||||
|
||||
def set_hover(self, pos):
|
||||
if not self._valid_move(pos):
|
||||
self.clear_hover()
|
||||
return
|
||||
|
||||
x,y = pos
|
||||
self.hover = self.board[x][y]
|
||||
|
||||
|
||||
def clear_hover(self):
|
||||
self.hover = None
|
||||
|
||||
|
||||
def place_stone(self, pos):
|
||||
|
@ -176,7 +196,11 @@ class Goban:
|
|||
|
||||
kills_group = False
|
||||
for d in [(x, y+1), (x, y-1), (x+1, y), (x-1, y)]:
|
||||
if not self._has_liberties(d, opponent):
|
||||
new_x, new_y = d
|
||||
if new_x < 0 or new_x > 18 or new_y < 0 or new_y > 18:
|
||||
continue
|
||||
|
||||
if self._has_liberties(d, opponent) == 0:
|
||||
kills_group = True
|
||||
break
|
||||
self._clear_checks()
|
||||
|
@ -274,12 +298,21 @@ class Goban:
|
|||
for row in self.board:
|
||||
j = 0
|
||||
for square in row:
|
||||
s = pygame.transform.scale(img_res[square.get_draw_code()], (inc, inc))
|
||||
s = img_res[square.get_draw_code()]
|
||||
s = pygame.transform.scale(s, (inc, inc))
|
||||
ret.blit(s, (j*inc,i*inc))
|
||||
|
||||
if self.hover == square:
|
||||
c = img_res['bH']
|
||||
if self.turn == 1:
|
||||
c = img_res['wH']
|
||||
c = pygame.transform.scale(c, (inc, inc))
|
||||
ret.blit(c, (j*inc,i*inc))
|
||||
|
||||
j += 1
|
||||
i += 1
|
||||
|
||||
return ret.convert()
|
||||
return ret.convert_alpha()
|
||||
|
||||
|
||||
def draw_info(self):
|
||||
|
@ -288,6 +321,7 @@ class Goban:
|
|||
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.captures[0]), 1, (10, 10, 10))
|
||||
white_cap = font.render('White: {}'.format(self.captures[1]), 1, (10, 10, 10))
|
||||
|
@ -297,6 +331,7 @@ class Goban:
|
|||
textbox.blit(black_cap, (0, 28))
|
||||
textbox.blit(white_cap, (0, 56))
|
||||
textbox.blit(turn, (0, 100))
|
||||
textbox.blit(time, (0, 150))
|
||||
|
||||
return textbox
|
||||
|
||||
|
@ -330,11 +365,27 @@ def main():
|
|||
screen.blit(background, (0, 0))
|
||||
pygame.display.flip()
|
||||
|
||||
pygame.time.set_timer(USEREVENT, 1000)
|
||||
|
||||
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((row,col))
|
||||
else:
|
||||
goban.clear_hover()
|
||||
|
||||
# Place a stone on left-click
|
||||
if event.type == MOUSEBUTTONDOWN:
|
||||
x, y = event.pos
|
||||
row = y / board_inc
|
||||
|
@ -343,9 +394,13 @@ def main():
|
|||
if x <= board_size:
|
||||
if event.button == 1:
|
||||
goban.place_stone((row, col))
|
||||
|
||||
if event.button == 3:
|
||||
goban.toggle_marked((row, col))
|
||||
|
||||
if event.type == USEREVENT:
|
||||
goban.elapsed_time += 1
|
||||
|
||||
board = goban.draw_board(board_size, img_res)
|
||||
background.blit(board, (0,0))
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user