def event(self, event): if event.type == pygame.MOUSEBUTTONDOWN: for widget in self.widgets: # left button if event.button == 1: if widget.inbound(Vector2D.tuple(event.pos)): clicked = getattr(widget, 'clicked', None) if callable(clicked): clicked()
def __init__(self, size: Vector2D, info_text: Text, position=Vector2D(0, 20), padding=Vector2D(0, 0)): self.size = size self.position = position self.padding = padding self.info_text = info_text self.info_text.text = 'Select start' self.tilepadding = Vector2D(0, 0) screen_size = Vector2D.tuple(pygame.display.get_surface().get_rect().size) space = Vector2D( screen_size.x - position.x - padding.x, screen_size.y - position.y - padding.y ) self.tilesize = Vector2D( int((space.x - (self.size.x * self.tilepadding.x)) / self.size.x), int((space.y - (self.size.y * self.tilepadding.y)) / self.size.y), ) self.grid = [] for x in range(size.x): l = [0] * size.y self.grid.append(l) self.tiles = [] for x in range(size.x): l = [None] * size.y self.tiles.append(l) self.remake_tiles(self.grid) self.drawable = Switch(True) self.mouse_left_down = Switch(False) self.mouse_left_down_type = None self.start = None self.end = None self.algorithm = None def onflip(val): if self.algorithm.solution_length == -1: self.info_text.text = 'No solution found' return self.info_text.text = 'Running' if val else f'Found solution of length {self.algorithm.solution_length}' self.running = Switch(False, onflip=onflip) self.misc = {}
def update(self): mouse_pos = Vector2D.tuple(pygame.mouse.get_pos()) for widget in self.widgets: mouse_is_over = widget.inbound(mouse_pos) if not widget.hover and mouse_is_over: widget.enter() if widget.hover and not mouse_is_over: widget.exit()
def update(self): if self.running.get(): try: affected = self.algorithm.next() self.update_tiles(affected) except StopIteration: self.running.set(False) # not further actions allowed # when algorithms is running return else: # get current mouse position mouse_pos = Vector2D.tuple(pygame.mouse.get_pos()) # get tile in mouse position position tile = self.tile(mouse_pos) if tile is None: return # update tile to indicate hover if not tile.hover: tile.enter() # revert previous hover previous hover tile try: inbound = self.misc['over'] if tile.position != inbound.position: inbound.exit() except KeyError: pass self.misc['over'] = tile keys = pygame.key.get_pressed() if keys[pygame.K_LALT]: for key in key_map.keys(): # save if keys[key]: value = key_map[key] self.update_grid() self.save(value) break else: for key in key_map.keys(): # load if keys[key]: value = key_map[key] self.load(value) break
def update(self): mouse_pos = Vector2D.tuple(pygame.mouse.get_pos()) for widget in self.widgets: try: mouse_is_over = widget.inbound(mouse_pos) except NotImplementedError: continue if not widget.hover and mouse_is_over: widget.enter() if widget.hover and not mouse_is_over: widget.exit()
def update(self): if self.running.get(): try: affected = self.algorithm.next() self.remake_tiles(affected) except StopIteration: self.running.set(False) else: mouse_pos = Vector2D.tuple(pygame.mouse.get_pos()) for tile in self.all_tiles(): inbound = tile.inbound(mouse_pos) if not tile.hover and inbound: tile.enter() if tile.hover and not inbound: tile.exit()
def event(self, event): """ event handler :param event: event in consideration :return: None """ if event.type == pygame.MOUSEBUTTONDOWN: for widget in self.widgets: # left button if event.button == 1: try: if widget.inbound(Vector2D.tuple(event.pos)): clicked = getattr(widget, 'clicked', None) if callable(clicked): clicked() except NotImplementedError: continue
def tile(self, coord) -> Union[Tile, None]: """ :returns: tile depending on value in case of tuple :return: tile in pixel positon (x, y) """ # finding tile position = Vector2D.tuple(coord) x = (position.y - self.position.y - self.padding.y) / (self.tilesize.y + self.tilepadding.y) y = (position.x - self.position.x - self.padding.x) / (self.tilesize.x + self.tilepadding.x) ix = math.floor(x) iy = math.floor(y) # out of bounds if ix < 0 or ix >= self.size.x or iy < 0 or iy >= self.size.y: return return self.tiles[ix][iy]
def event(self, event): if event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: if not self.drawable.get(): easygui.msgbox('Press either Left Ctrl or Left Shift to clear the current grid' '\nLeft Ctrl: Everything excluding walls' '\nLeft Shift: Everything including walls', 'Clear grid', ok_button='CLOSE') return self.update_grid() if self.start is None or self.end is None: easygui.msgbox('Starting point or End point not specified', 'Missing inputs', 'CLOSE') return # lock input and start the a* algorithm self.drawable.set(False) self.algorithm = AStarAlgorithm(self.grid) self.running.set(True) if event.key == pygame.K_LSHIFT: if self.running.get(): return self.update_grid() self.clean_grid([Tile.VISITED, Tile.START, Tile.END, Tile.PATH, Tile.NEIGHBOURS, Tile.WALL]) self.update_tiles(self.grid) self.drawable.set(True) if event.key == pygame.K_LCTRL: if self.running.get(): return self.update_grid() self.clean_grid([Tile.VISITED, Tile.START, Tile.END, Tile.PATH, Tile.NEIGHBOURS]) self.update_tiles(self.grid) self.drawable.set(True) if event.type == pygame.MOUSEBUTTONDOWN: # left if event.button == 1: self.mouse_left_down.set(True) if self.drawable.get(): # for all tiles check and initiate drawing for tile in self.all_tiles(): if tile.inbound(Vector2D.tuple(event.pos)): currentstate = tile.state if currentstate == Tile.WALL: self.mouse_left_down_type = Tile.UNVISITED elif currentstate == Tile.UNVISITED: self.mouse_left_down_type = Tile.WALL if self.mouse_left_down_type is not None: tile.state = self.mouse_left_down_type # right if event.button == 3: if self.drawable.get(): if self.start is None or self.end is None: for tile in self.all_tiles(): if tile.inbound(Vector2D.tuple(event.pos)): if self.start is None: self.start = tile tile.state = Tile.START self.info_text.text = 'Select end' else: self.end = tile tile.state = Tile.END self.info_text.text = 'Ready' else: print('Start and end has been selected') if event.type == pygame.MOUSEBUTTONUP: # left if event.button == 1: self.mouse_left_down.set(False) self.mouse_left_down_type = None if event.type == pygame.MOUSEMOTION: # left if self.mouse_left_down.get(): if self.drawable.get(): # for all tiles draw / change state for tile in self.all_tiles(): if tile.state == Tile.START or tile.state == Tile.END: continue if tile.inbound(Vector2D.tuple(event.pos)): if self.mouse_left_down_type is not None: tile.state = self.mouse_left_down_type