-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
109 lines (90 loc) · 4.14 KB
/
ui.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
from model import Board
import pygame
import math
from pygame import gfxdraw
"""
Display a smooth circle
"""
def draw_circle(surface, x, y, radius, color):
gfxdraw.aacircle(surface, x, y, radius, color)
gfxdraw.filled_circle(surface, x, y, radius, color)
color = {
"brown" : (139,69,19),
"middle_brown" : (210,105,30),
"dark_brown" : (89, 19, 0),
"gold" : (255,215,0),
"black": (0,0,0),
"white" : (255,255,255),
}
class UIBoard(Board):
def __init__(self, color_map = None):
super().__init__()
self.pawn_size = 32
self.pawn_radius = self.pawn_size // 2
self.res_size = self.pawn_size // self.row_size
self.res_radius = self.res_size // 2
self.color_map = color_map
if self.color_map == None:
self.load_default_color_map()
def load_default_color_map(self):
self.color_map = {
1 : (255, 0, 0), # Red
2 : (0, 255, 0), # Green
3 : (0, 0, 255), # Blue
4 : (255, 255, 0), # Yellow
5 : (0, 255, 255), # Cyan
6 : (255, 255, 255), # White
7 : (32, 32, 32), # Gray
8 : (210, 40, 130), # Pink
}
def draw(self, surf, moves = None):
board_width = (self.row_size + 1) * self.pawn_size + 2 # +1 for the result
board_height = self.pawn_size * (self.max_row + 1) # +1 for the solution
# Step 1 : Draw the board
pygame.draw.rect(surf, color["brown"], (0, 0, board_width - self.pawn_size, board_height))
pygame.draw.rect(surf, color["middle_brown"], (board_width - self.pawn_size, 0, self.pawn_size, board_height))
# Step 2 draw the line between rows
for i in range(0, self.max_row):
pygame.draw.rect(surf, color["gold"], (0, i * self.pawn_size, board_width, 2))
# Step 3 draw the pawns and result
for i in range(0, len(self.rows)):
# Pawns
for j in range(0, len(self.rows[i])):
draw_circle(surf,
j * self.pawn_size + self.pawn_radius,
i * self.pawn_size + self.pawn_radius,
self.pawn_radius,
self.color_map.get(self.rows[i][j])
)
# Result
for j in range(0, self.res[i] ["good"]):
x = (self.row_size * self.pawn_size) + j * self.res_size + self.res_radius
y = (i * self.pawn_size) + math.floor(self.pawn_size * 1 / 3)
draw_circle(surf, x, y, self.res_radius, color["black"])
for j in range(0, self.res[i] ["wrong"]):
x = (self.row_size * self.pawn_size) + j * self.res_size + self.res_radius
y = (i * self.pawn_size) + math.floor(self.pawn_size * 2 / 3)
draw_circle(surf, x, y, self.res_radius, color["white"])
# Step 4 display the solution
for i in range(0, len(self.solution)):
draw_circle(surf,
i * self.pawn_size + self.pawn_radius,
self.max_row * self.pawn_size + self.pawn_radius,
self.pawn_radius,
self.color_map.get(self.solution[i])
)
# Step 5 cache solution
pygame.draw.rect(surf, color["dark_brown"], (0, self.max_row * self.pawn_size, board_width, self.pawn_size *2/ 3))
# Step 6 (optionnal) : Display all possible move
if moves != None:
sol_pawn_size = 8
sol_pawn_radius = sol_pawn_size // 2
y_max = surf.get_rect().height - 2 * sol_pawn_size
for i in range(0, len(moves)):
for j in range(0, len(moves[i])):
y = (i * sol_pawn_size + sol_pawn_radius)
x = (board_width) + (j * sol_pawn_size + sol_pawn_radius)
# Add offset to column change
x = x + (math.floor(1.5 * len(moves[i]) * sol_pawn_size)) * math.floor(y / y_max)
y = y % y_max
draw_circle(surf,x,y,sol_pawn_radius,self.color_map.get(moves[i][j]))