This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reversi_board.py
51 lines (46 loc) · 1.67 KB
/
reversi_board.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
import pygame
from board import Board
class ReversiBoard(Board):
"""
Status
------
-1: dark side
0: no piece
1: light side
2: translucent(for available block)
"""
DARK = (0, 0, 0)
LIGHT = (255, 255, 255)
TRANSLUCENT = (153, 184, 147)
def __init__(self, side_length=600, top_left=(0, 0), dark=DARK, light=LIGHT, translucent=TRANSLUCENT):
rows = ['1', '2', '3', '4', '5', '6', '7', '8']
cols = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
super().__init__(side_length, top_left, rows, cols)
self.status2color = {
-1: dark,
1: light,
2: translucent
}
def draw_pieces(self, screen):
for key in self.status:
if self.status[key] == 0:
continue
else:
row_index = key // len(self.cols)
col_index = key % len(self.cols)
pos = (self.top_left[0] + 0.1 * self.side_length + 0.8 * (col_index + 0.5) / len(self.cols) * self.side_length,
self.top_left[1] + 0.1 * self.side_length + 0.8 * (row_index + 0.5) / len(self.rows) * self.side_length)
pygame.draw.circle(screen, self.status2color[self.status[key]], pos, 0.35 * self.block_size)
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((800, 600), 0, 32)
board = ReversiBoard(600, (100, 0))
board.draw_board(screen)
board.update('1I', -1)
board.draw_pieces(screen)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()