def move_board(self, direction, speed=11): self.pan[0] = utils.min_max(self.pan[0] - speed * direction[0], -self.size(0) // 2, self.size(0) // 2) self.pan[1] = utils.min_max(self.pan[1] - speed * direction[1], -self.size(1) // 2, self.size(1) // 2)
def move_board(self, direction=(0, 0), step=11): step = (-step, -step) self.pan = (self.pan[0] + step[0] * direction[0], self.pan[1] + step[1] * direction[1]) self.pan = (utils.min_max(self.pan[0], -self.total_size[0] // 2, self.total_size[0] // 2), utils.min_max(self.pan[1], -self.total_size[1] // 2, self.total_size[1] // 2))
def focus_board(self, grid): grid_pos = utils.add( self.get(grid).get_rect()[0], (self.grid_size // 2, grid[1] // 2)) self.pan = list( utils.add(self.args.get_pos(1, 1), utils.negative(self.pos), utils.negative(grid_pos))) self.pan[0] = utils.min_max(self.pan[0], -self.size(0) // 2, self.size(0) // 2) self.pan[1] = utils.min_max(self.pan[1], -self.size(1) // 2, self.size(1) // 2)
def process_right_click(self, mouse_pos): for name in self.buttons: if self.buttons[name].in_range(mouse_pos): target_turn = utils.min_max( eval(f'{self.map.turn} {name[4:]} 10'), self.min_turn, self.max_turn) return ['turn', target_turn] return [None]
def generate_apples(self, players, num=None): # recalculate num if num is None: num = self.max_apples - len(self.apples) empty = self.get_empty(players) num = utils.min_max(num, 0, len(empty)) if num == 0: return # generate apples new_apple_grids = choices(empty, k=num) for grid in new_apple_grids: self.apples.append(grid)
def process_events(self, events): commands = {'move-board': [0, 0], 'turn': self.map.turn, 'speed': 0, 'pause': False} # key events for key in events['key-pressed']: if key == 'w': commands['move-board'][1] -= 1 elif key == 'a': commands['move-board'][0] -= 1 elif key == 's': commands['move-board'][1] += 1 elif key == 'd': commands['move-board'][0] += 1 for key in events['key-down']: if key == 'up': commands['turn'] -= 10 elif key == 'left': commands['turn'] -= 1 elif key == 'down': commands['turn'] += 10 elif key == 'right': commands['turn'] += 1 elif key == '-': commands['speed'] -= 1 elif key == '=': commands['speed'] += 1 elif key == 'space': commands['pause'] = True commands['turn'] = utils.min_max(commands['turn'], self.min_turn, self.max_turn) commands['speed'] = {0: None, -1: '-', 1: '+'}[commands['speed']] # mouse events if events['mouse-left'] == 'down': self.map.cursor = self.map.pos_to_cord(events['mouse-pos']) return commands
def multiply(color, num): return rgb(*[utils.min_max(c * num, 0, 255) for c in color])
def add(color, num): return rgb(*[utils.min_max(c + num, 0, 255) for c in color])
def multiply(color, num): return tuple((utils.min_max(color[i] * num, 0, 255)) for i in (0, 1, 2))
def add(color, num): return tuple((utils.min_max(color[i] + num, 0, 255)) for i in (0, 1, 2))