-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrgsimulatorUI.py
214 lines (169 loc) · 7.54 KB
/
rgsimulatorUI.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import Tkinter
import tkFont
def mid(l1, l2):
return (int((l1[0]+l2[0]) / 2), int((l1[1]+l2[1]) / 2))
class SimulatorUI:
def __init__(self, settings):
self.settings = settings
self.square_size = 40
self.border_width = 1
self.padding = 0
self.arrow_width = 3
self.selection_border_width = 5
self.fill_color = "#FFF"
self.obstacle_fill_color = "#555"
self.bot_fill_color = ["#57C", "#C75"]
self.border_color = "#333"
self.selection_border_color = "#FF0"
self.move_arrow_color = "#00F"
self.attack_arrow_color = "#000"
self.map_width = self.settings.board_size
self.map_height = self.settings.board_size
self.width = self.square_size*self.map_width
self.height = self.square_size*self.map_height
self.root = Tkinter.Tk()
self.root.resizable(0, 0)
self.setTitle("Robot Game")
self.turn_label = Tkinter.Label(self.root, text = "")
self.turn_label.pack()
self.setTurn(1)
self.canvas = Tkinter.Canvas(self.root, width = self.width, height = self.height)
self.canvas.pack()
self.squares = {}
self.labels = {}
self.actions = {}
for x in xrange(0, self.map_width):
for y in xrange(0, self.map_height):
coordinates = self.getSquareCoordinates((x, y))
x1, y1 = coordinates[0]
x2, y2 = coordinates[1]
self.squares[(x, y)] = self.canvas.create_rectangle(
x1, y1, x2, y2,
fill = self.obstacle_fill_color if (x, y) in self.settings['obstacles'] else self.fill_color,
outline = self.border_color,
width = self.border_width
)
self.labels[(x, y)] = self.canvas.create_text(
x1 + self.square_size/2, y1 + self.square_size/2,
text = (x+1)*(y+1)-1 if x*y == 0 else "",
font = "TkFixedFont",
fill = "#000"
)
self.actions[(x, y)] = []
self.center = (int(self.map_width/2), int(self.map_height/2))
self.selection = self.center
selection_coordinates = self.getSquareCoordinates(self.selection)
selection_x1, selection_y1 = selection_coordinates[0]
selection_x2, selection_y2 = selection_coordinates[1]
self.selection_square = self.canvas.create_rectangle(
selection_x1, selection_y1, selection_x2, selection_y2,
fill = "",
outline = self.selection_border_color,
width = self.selection_border_width
)
# I am a dirty hack, fix me
text_font = tkFont.nametofont("TkTextFont")
text_font.configure(weight = "bold")
def run(self):
self.root.mainloop()
def bind(self, event, hook):
self.root.bind(event, hook)
def onMouseClick(self, event):
self.setSelection(self.getSquareByCoordinates(event.x, event.y))
def getSquareCoordinates(self, loc):
x, y = loc
return (
(self.square_size*x + self.padding/2, self.square_size*y + self.padding/2),
(self.square_size*(x + 1) - self.padding/2, self.square_size*(y + 1) - self.padding/2)
)
def getSquareByCoordinates(self, x, y):
return (x/self.square_size, y/self.square_size)
def hideSelection(self):
self.canvas.itemconfigure(self.selection_square, width = 0)
def showSelection(self):
self.canvas.itemconfigure(self.selection_square, width = self.selection_border_width)
def setSelection(self, loc):
if loc not in self.settings['obstacles']:
selection_coordinates = self.getSquareCoordinates(loc)
selection_x1, selection_y1 = selection_coordinates[0]
selection_x2, selection_y2 = selection_coordinates[1]
self.canvas.coords(self.selection_square, selection_x1, selection_y1, selection_x2, selection_y2)
self.selection = loc
def moveSelection(self, dloc):
self.setSelection((self.selection[0] + dloc[0], self.selection[1] + dloc[1]))
def setTitle(self, title):
self.root.title(title)
def setTurn(self, turn):
self.turn_label.config(text = "Turn %s" % turn)
def setFill(self, loc, color):
self.canvas.itemconfigure(self.squares[loc], fill = color)
def setText(self, loc, text):
self.canvas.itemconfigure(self.labels[loc], text = text)
def renderEmpty(self, loc):
self.setText(loc, "")
self.setFill(loc, self.obstacle_fill_color if loc in self.settings['obstacles'] else self.fill_color)
def clearBots(self):
for x in xrange(1, self.map_width):
for y in xrange(1, self.map_height):
self.renderEmpty((x, y))
def renderBot(self, loc, hp, player_id):
self.setText(loc, hp)
self.setFill(loc, self.bot_fill_color[player_id])
def clearAction(self, loc):
for action in self.actions[loc]:
self.canvas.delete(action)
self.actions[loc] = []
def clearActions(self):
for loc in self.actions:
self.clearAction(loc)
def fadeAction(self, loc):
for action in self.actions[loc]:
if self.canvas.type(action) == "text":
old_text = self.canvas.itemcget(action, "text")
new_text = old_text.strip("()")
new_text = "("+new_text+")"
self.canvas.itemconfig(action, fill="#CCC", text=new_text)
else:
self.canvas.itemconfig(action, fill="#CCC")
def fadeActions(self):
for loc in self.actions:
self.fadeAction(loc)
def renderActionChar(self, loc, char):
coordinates = self.getSquareCoordinates(loc)
center_coordinates = mid(coordinates[0], coordinates[1])
char_coordinates = mid(center_coordinates, coordinates[1])
x, y = char_coordinates
action_char = self.canvas.create_text(
x, y,
text = char,
font = "TkTextFont",
fill = "#000"
)
self.actions[loc].append(action_char)
def renderActionArrow(self, loc, loc2, color):
coordinates1 = self.getSquareCoordinates(loc)
center_coordinates1 = mid(coordinates1[0], coordinates1[1])
coordinates2 = self.getSquareCoordinates(loc2)
center_coordinates2 = mid(coordinates2[0], coordinates2[1])
mid_coordinates = mid(center_coordinates1, center_coordinates2)
x1, y1 = mid(center_coordinates1, mid_coordinates)
x2, y2 = mid(center_coordinates2, mid_coordinates)
arrow = self.canvas.create_line(x1, y1, x2, y2, fill = color, width = self.arrow_width, arrow = Tkinter.LAST)
self.actions[loc].append(arrow)
def renderAction(self, loc, action):
if action[0] == "guard":
self.renderActionChar(loc, "G")
elif action[0] == "suicide":
self.renderActionChar(loc, "S")
elif action[0] == "move":
self.renderActionChar(loc, "M")
self.renderActionArrow(loc, action[1], self.move_arrow_color)
else:
self.renderActionChar(loc, "A")
self.renderActionArrow(loc, action[1], self.attack_arrow_color)
def renderText(self, loc, text):
coordinates = self.getSquareCoordinates(loc)
center = mid(coordinates[0], coordinates[1])
x, y = mid(center, coordinates[0])
textobj = self.canvas.create_text(x, y, text=text)
self.actions[loc].append(textobj)