forked from eagleflo/goban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goban.py
executable file
·117 lines (94 loc) · 3.8 KB
/
goban.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
# coding: utf-8
"""Goban made with Python, pygame and go.py.
This is a front-end for my go library 'go.py', handling drawing and
pygame-related activities. Together they form a fully working goban.
"""
__author__ = "Aku Kotkavuo <[email protected]>"
__version__ = "0.1"
import pygame
import go
from sys import exit
BACKGROUND = 'images/ramin.jpg'
BOARD_SIZE = (820, 820)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
class Stone(go.Stone):
def __init__(self, board, point, color):
"""Create, initialize and draw a stone."""
super(Stone, self).__init__(board, point, color)
self.coords = (5 + self.point[0] * 40, 5 + self.point[1] * 40)
self.draw()
def draw(self):
"""Draw the stone as a circle."""
pygame.draw.circle(screen, self.color, self.coords, 20, 0)
pygame.display.update()
def remove(self):
"""Remove the stone from board."""
blit_coords = (self.coords[0] - 20, self.coords[1] - 20)
area_rect = pygame.Rect(blit_coords, (40, 40))
screen.blit(background, blit_coords, area_rect)
pygame.display.update()
super(Stone, self).remove()
class Board(go.Board):
def __init__(self):
"""Create, initialize and draw an empty board."""
super(Board, self).__init__()
self.outline = pygame.Rect(45, 45, 720, 720)
self.draw()
def draw(self):
"""Draw the board to the background and blit it to the screen.
The board is drawn by first drawing the outline, then the 19x19
grid and finally by adding hoshi to the board. All these
operations are done with pygame's draw functions.
This method should only be called once, when initializing the
board.
"""
pygame.draw.rect(background, BLACK, self.outline, 3)
# Outline is inflated here for future use as a collidebox for the mouse
self.outline.inflate_ip(20, 20)
for i in range(18):
for j in range(18):
rect = pygame.Rect(45 + (40 * i), 45 + (40 * j), 40, 40)
pygame.draw.rect(background, BLACK, rect, 1)
for i in range(3):
for j in range(3):
coords = (165 + (240 * i), 165 + (240 * j))
pygame.draw.circle(background, BLACK, coords, 5, 0)
screen.blit(background, (0, 0))
pygame.display.update()
def update_liberties(self, added_stone=None):
"""Updates the liberties of the entire board, group by group.
Usually a stone is added each turn. To allow killing by 'suicide',
all the 'old' groups should be updated before the newly added one.
"""
for group in self.groups:
if added_stone:
if group == added_stone.group:
continue
group.update_liberties()
if added_stone:
added_stone.group.update_liberties()
def main():
while True:
pygame.time.wait(250)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1 and board.outline.collidepoint(event.pos):
x = int(round(((event.pos[0] - 5) / 40.0), 0))
y = int(round(((event.pos[1] - 5) / 40.0), 0))
stone = board.search(point=(x, y))
if stone:
stone.remove()
else:
added_stone = Stone(board, (x, y), board.turn())
board.update_liberties(added_stone)
if __name__ == '__main__':
pygame.init()
pygame.display.set_caption('Goban')
screen = pygame.display.set_mode(BOARD_SIZE, 0, 32)
background = pygame.image.load(BACKGROUND).convert()
board = Board()
main()