1 graphics_enabled = True
5 os.environ["SDL_VIDEO_ALLOW_SCREENSAVER"] = "1"
7 graphics_enabled = False
13 # A thread to make things pretty
14 class GraphicsThread(StoppableThread):
15 def __init__(self, board, title = "UCC::Progcomp 2013 - QChess", grid_sz = [80,80]):
16 StoppableThread.__init__(self)
20 self.window = pygame.display.set_mode((grid_sz[0] * w, grid_sz[1] * h))
21 pygame.display.set_caption(title)
23 #print "Initialised properly"
25 self.grid_sz = grid_sz[:]
26 self.state = {"select" : None, "dest" : None, "moves" : None, "overlay" : None, "coverage" : None}
28 self.lock = threading.RLock()
29 self.cond = threading.Condition()
30 self.sleep_timeout = None
31 self.last_event = time.time()
34 pygame.font.Font(os.path.join(os.path.curdir, "data", "DejaVuSans.ttf"), 32).render("Hello", True,(0,0,0))
37 create_images(grid_sz)
40 for c in images.keys():
41 for p in images[c].keys():
42 images[c][p] = images[c][p].convert(self.window)
43 small_images[c][p] = small_images[c][p].convert(self.window)
50 # On the run from the world
53 while not self.stopped():
55 if self.sleep_timeout == None or (time.time() - self.last_event) < self.sleep_timeout:
58 self.board.display_grid(window = self.window, grid_sz = self.grid_sz) # Draw the board
60 #print "Display overlay"
63 #print "Display pieces"
64 self.board.display_pieces(window = self.window, grid_sz = self.grid_sz) # Draw the board
67 self.window.fill((0,0,0))
71 for event in pygame.event.get():
72 self.last_event = time.time()
73 if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_q):
74 if isinstance(game, GameThread):
76 game.final_result = ""
77 if game.state["turn"] != None:
78 game.final_result = game.state["turn"].colour + " "
79 game.final_result += "terminated"
83 elif event.type == pygame.MOUSEBUTTONDOWN:
84 self.mouse_down(event)
86 elif event.type == pygame.MOUSEBUTTONUP:
96 self.message("Game ends, result \""+str(game.final_result) + "\"")
99 # Wake up anyone who is sleeping
104 pygame.quit() # Time to say goodbye
106 # Mouse release event handler
107 def mouse_up(self, event):
108 if event.button == 3:
110 self.state["overlay"] = None
111 elif event.button == 2:
113 self.state["coverage"] = None
115 # Mouse click event handler
116 def mouse_down(self, event):
117 if event.button == 1:
118 m = [event.pos[i] / self.grid_sz[i] for i in range(2)]
119 if isinstance(game, GameThread):
121 p = game.state["turn"]
126 if isinstance(p, HumanPlayer):
128 s = self.board.grid[m[0]][m[1]]
129 select = self.state["select"]
131 if s != None and s.colour != p.colour:
132 self.message("Wrong colour") # Look at all this user friendliness!
135 # Notify human player of move
138 self.state["select"] = s
139 self.state["dest"] = None
148 if self.state["moves"] == None:
151 if not m in self.state["moves"]:
152 self.message("Illegal Move") # I still think last year's mouse interface was adequate
157 if self.state["dest"] == None:
159 self.state["dest"] = m
160 self.state["select"] = None
161 self.state["moves"] = None
164 elif event.button == 3:
165 m = [event.pos[i] / self.grid_sz[i] for i in range(len(event.pos))]
166 if isinstance(game, GameThread):
168 p = game.state["turn"]
173 if isinstance(p, HumanPlayer):
175 self.state["overlay"] = self.board.probability_grid(self.board.grid[m[0]][m[1]])
177 elif event.button == 2:
178 m = [event.pos[i] / self.grid_sz[i] for i in range(len(event.pos))]
179 if isinstance(game, GameThread):
181 p = game.state["turn"]
186 if isinstance(p, HumanPlayer):
188 self.state["coverage"] = self.board.coverage(m[0], m[1], None, self.state["select"])
193 square_img = pygame.Surface((self.grid_sz[0], self.grid_sz[1]),pygame.SRCALPHA) # A square image
194 # Draw square over the selected piece
196 select = self.state["select"]
198 mp = [self.grid_sz[i] * [select.x, select.y][i] for i in range(len(self.grid_sz))]
199 square_img.fill(pygame.Color(0,255,0,64))
200 self.window.blit(square_img, mp)
201 # If a piece is selected, draw all reachable squares
202 # (This quality user interface has been patented)
204 m = self.state["moves"]
206 square_img.fill(pygame.Color(255,0,0,128)) # Draw them in blood red
208 mp = [self.grid_sz[i] * move[i] for i in range(2)]
209 self.window.blit(square_img, mp)
210 # If a piece is overlayed, show all squares that it has a probability to reach
212 m = self.state["overlay"]
217 mp = [self.grid_sz[i] * [x,y][i] for i in range(2)]
218 square_img.fill(pygame.Color(255,0,255,int(m[x][y] * 128))) # Draw in purple
219 self.window.blit(square_img, mp)
220 font = pygame.font.Font(os.path.join(os.path.curdir, "data", "DejaVuSans.ttf"), 14)
221 text = font.render("{0:.2f}".format(round(m[x][y],2)), 1, pygame.Color(0,0,0))
222 self.window.blit(text, mp)
224 # If a square is selected, highlight all pieces that have a probability to reach it
226 m = self.state["coverage"]
229 mp = [self.grid_sz[i] * [p.x,p.y][i] for i in range(2)]
230 square_img.fill(pygame.Color(0,255,255, int(m[p] * 196))) # Draw in pale blue
231 self.window.blit(square_img, mp)
232 font = pygame.font.Font(os.path.join(os.path.curdir, "data", "DejaVuSans.ttf"), 14)
233 text = font.render("{0:.2f}".format(round(m[p],2)), 1, pygame.Color(0,0,0))
234 self.window.blit(text, mp)
235 # Draw a square where the mouse is
236 # This also serves to indicate who's turn it is
238 if isinstance(game, GameThread):
240 turn = game.state["turn"]
244 if isinstance(turn, HumanPlayer):
245 mp = [self.grid_sz[i] * int(pygame.mouse.get_pos()[i] / self.grid_sz[i]) for i in range(2)]
246 square_img.fill(pygame.Color(0,0,255,128))
247 if turn.colour == "white":
248 c = pygame.Color(255,255,255)
250 c = pygame.Color(0,0,0)
251 pygame.draw.rect(square_img, c, (0,0,self.grid_sz[0], self.grid_sz[1]), self.grid_sz[0]/10)
252 self.window.blit(square_img, mp)
254 # Message in a bottle
255 def message(self, string, pos = None, colour = None, font_size = 20):
256 #print "Drawing message..."
257 font = pygame.font.Font(os.path.join(os.path.curdir, "data", "DejaVuSans.ttf"), font_size)
259 colour = pygame.Color(0,0,0)
261 text = font.render(string, 1, colour)
264 s = pygame.Surface((text.get_width(), text.get_height()), pygame.SRCALPHA)
265 s.fill(pygame.Color(128,128,128))
267 tmp = self.window.get_size()
270 pos = (tmp[0] / 2 - text.get_width() / 2, tmp[1] / 3 - text.get_height())
272 pos = (pos[0]*text.get_width() + tmp[0] / 2 - text.get_width() / 2, pos[1]*text.get_height() + tmp[1] / 3 - text.get_height())
275 rect = (pos[0], pos[1], text.get_width(), text.get_height())
277 pygame.draw.rect(self.window, pygame.Color(0,0,0), pygame.Rect(rect), 1)
278 self.window.blit(s, pos)
279 self.window.blit(text, pos)
281 pygame.display.flip()
283 def getstr(self, prompt = None):
284 s = pygame.Surface((self.window.get_width(), self.window.get_height()))
285 s.blit(self.window, (0,0))
292 self.message(result, pos = (0, 1))
295 for event in pygame.event.get():
296 if event.type == pygame.QUIT:
298 if event.type == pygame.KEYDOWN:
299 if event.key == pygame.K_BACKSPACE:
300 result = result[0:len(result)-1]
301 self.window.blit(s, (0,0)) # Revert the display
306 if event.unicode == '\r':
309 result += str(event.unicode)
314 # Function to pick a button
315 def SelectButton(self, choices, prompt = None, font_size=20):
317 #print "Select button called!"
318 self.board.display_grid(self.window, self.grid_sz)
321 font = pygame.font.Font(os.path.join(os.path.curdir, "data", "DejaVuSans.ttf"), font_size)
323 sz = self.window.get_size()
326 for i in range(len(choices)):
329 text = font.render(c, 1, pygame.Color(0,0,0))
330 p = (sz[0] / 2 - (1.5*text.get_width())/2, sz[1] / 2 +(i-1)*text.get_height()+(i*2))
331 targets.append((p[0], p[1], p[0] + 1.5*text.get_width(), p[1] + text.get_height()))
334 mp =pygame.mouse.get_pos()
335 for i in range(len(choices)):
337 if mp[0] > targets[i][0] and mp[0] < targets[i][2] and mp[1] > targets[i][1] and mp[1] < targets[i][3]:
338 font_colour = pygame.Color(255,0,0)
339 box_colour = pygame.Color(0,0,255,128)
341 font_colour = pygame.Color(0,0,0)
342 box_colour = pygame.Color(128,128,128)
344 text = font.render(c, 1, font_colour)
345 s = pygame.Surface((text.get_width()*1.5, text.get_height()), pygame.SRCALPHA)
347 pygame.draw.rect(s, (0,0,0), (0,0,1.5*text.get_width(), text.get_height()), self.grid_sz[0]/10)
348 s.blit(text, ((text.get_width()*1.5)/2 - text.get_width()/2 ,0))
349 self.window.blit(s, targets[i][0:2])
352 pygame.display.flip()
354 for event in pygame.event.get():
355 if event.type == pygame.QUIT:
357 elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
358 for i in range(len(targets)):
360 if event.pos[0] > t[0] and event.pos[0] < t[2]:
361 if event.pos[1] > t[1] and event.pos[1] < t[3]:
363 #print "Reject " + str(i) + str(event.pos) + " vs " + str(t)
366 # Function to pick players in a nice GUI way
367 def SelectPlayers(self, players = []):
370 #print "SelectPlayers called"
372 missing = ["white", "black"]
374 missing.remove(p.colour)
376 for colour in missing:
379 choice = self.SelectButton(["human", "agent", "network"],prompt = "Choose " + str(colour) + " player")
381 players.append(HumanPlayer("human", colour))
384 internal_agents = inspect.getmembers(sys.modules[__name__], inspect.isclass)
385 internal_agents = [x for x in internal_agents if issubclass(x[1], InternalAgent)]
386 internal_agents.remove(('InternalAgent', InternalAgent))
387 if len(internal_agents) > 0:
388 choice2 = self.SelectButton(["internal", "external"], prompt="Type of agent")
393 agent = internal_agents[self.SelectButton(map(lambda e : e[0], internal_agents), prompt="Choose internal agent")]
394 players.append(agent[1](agent[0], colour))
398 from tkFileDialog import askopenfilename
399 root = Tkinter.Tk() # Need a root to make Tkinter behave
400 root.withdraw() # Some sort of magic incantation
401 path = askopenfilename(parent=root, initialdir="../agents",title=
404 return self.SelectPlayers()
405 players.append(make_player(path, colour))
410 self.board.display_grid(self.window, self.grid_sz)
411 pygame.display.flip()
412 path = self.getstr(prompt = "Enter path:")
417 return self.SelectPlayers()
420 p = make_player(path, colour)
422 self.board.display_grid(self.window, self.grid_sz)
423 pygame.display.flip()
424 self.message("Invalid path!")
431 self.board.display_grid(self.window, self.grid_sz)
433 address = self.getstr(prompt = "Address? (leave blank for server)")
440 map(int, address.split("."))
442 self.board.display_grid(self.window, self.grid_sz)
443 self.message("Invalid IPv4 address!")
446 players.append(NetworkReceiver(colour, address))
449 #print str(self) + ".SelectPlayers returns " + str(players)