-
Notifications
You must be signed in to change notification settings - Fork 11
/
welcome_board_frontend.py
108 lines (91 loc) · 3.5 KB
/
welcome_board_frontend.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
import pyglet
from util_frontend import get_sprite, get_label, window_zoom
from util_frontend import loaded_robots_images, player_sprite
WINDOW_WIDTH = 1030
WINDOW_HEIGHT = 578
# Background
background_sprite = get_sprite('img/interface/png/board.png', x=0, y=0)
# Border of available robot's picture
border_sprite = get_sprite('img/interface/png/border.png')
robot_background_sprite = get_sprite('img/interface/png/robot_bcg.png')
picture_coordinates = []
for i in range(8):
x = 50 + i * 120
y = 120
picture_coordinates.append((x, y))
not_available_label = get_label(
"No more available robots.",
x=500, y=50,
font_size=20, anchor_x="center",
color=(255, 0, 0, 255),
)
def create_window(on_draw, on_mouse_press, on_text, on_text_motion):
"""
Return a pyglet window for graphic output.
"""
window = pyglet.window.Window(WINDOW_WIDTH, WINDOW_HEIGHT, resizable=True)
window.push_handlers(
on_draw=on_draw,
on_mouse_press=on_mouse_press,
on_text=on_text,
on_text_motion=on_text_motion,
)
return window
def draw_board(state, available_robots, window, own_robot_name):
"""
Draw the welcome board for choosing robot,
react to user's resizing of window by scaling the board.
"""
with window_zoom(window, WINDOW_WIDTH, WINDOW_HEIGHT):
background_sprite.draw()
if state is not None and available_robots is not None:
for coordinate, robot in zip(picture_coordinates, state.robots):
x, y = coordinate
if robot.name in loaded_robots_images.keys():
robot_background_sprite.x = x
robot_background_sprite.y = y
robot_background_sprite.draw()
player_sprite.image = loaded_robots_images[robot.name]
player_sprite.x = x
player_sprite.y = y
player_sprite.draw()
robot_name_label = get_label(
str(robot.displayed_name), player_sprite.x + 30, player_sprite.y - 26,
16, "center", (0, 0, 0, 255),
)
robot_name_label.draw()
for available_robot in available_robots:
if available_robot.name == robot.name:
border_sprite.x = player_sprite.x
border_sprite.y = player_sprite.y
border_sprite.draw()
if not available_robots:
not_available_label.draw()
own_name_label = get_label(
own_robot_name,
x=445,
y=327,
font_size=20,
anchor_x="left",
color=(0, 0, 0, 255),
)
own_name_label.draw()
def handle_click(state, x, y, window, available_robots):
"""
Board react on mouse press. Return robot_name when is clicked on robot.
"""
zoom = min(
window.height / WINDOW_HEIGHT,
window.width / WINDOW_WIDTH
)
x, y = (x / zoom, y / zoom)
for i, coordinate in enumerate(picture_coordinates):
coord_x, coord_y = coordinate
if (
coord_x < x < coord_x + player_sprite.width and
coord_y < y < coord_y + player_sprite.height
):
robot_name = sorted(loaded_robots_images.keys())[i]
for available_robot in available_robots:
if robot_name == available_robot.name:
return robot_name