def add_resources(self, team, new_resources): self.resources[team] = clamp(self.resources[team] + new_resources, 0, MAX_RESOURCES) publish_game_event(EventType.E_RESOURCES_GAINED, { 'team': team, 'new_resources': new_resources })
def scroll_camera(self): map = self.get_manager(Manager.MAP) if self.is_active(): mousex, mousey = pygame.mouse.get_pos() # Convert the screen coordinates to the grid coordinates self.gx = int( clamp((mousex / SETTINGS.get(Setting.SCREEN_SCALE) + self.camera_x) // GRID_WIDTH, 0, map.width - 1)) self.gy = int( clamp((mousey / SETTINGS.get(Setting.SCREEN_SCALE) + self.camera_y) // GRID_HEIGHT, 0, map.height - 1)) camera_min_gx = self.camera_dest_x // GRID_WIDTH camera_min_gy = self.camera_dest_y // GRID_HEIGHT camera_max_gx = camera_min_gx + CAMERA_WIDTH // GRID_WIDTH camera_max_gy = camera_min_gy + CAMERA_HEIGHT // GRID_HEIGHT if self.gx >= camera_max_gx - camera_scroll_border: self.camera_dest_x += GRID_WIDTH if self.gx <= camera_min_gx + camera_scroll_border: self.camera_dest_x -= GRID_WIDTH if self.gy >= camera_max_gy - camera_scroll_border: self.camera_dest_y += GRID_HEIGHT if self.gy <= camera_min_gy + camera_scroll_border: self.camera_dest_y -= GRID_HEIGHT self.camera_dest_x = clamp(self.camera_dest_x, 0, map.width * GRID_WIDTH - CAMERA_WIDTH) self.camera_dest_y = clamp(self.camera_dest_y, 0, map.height * GRID_HEIGHT - CAMERA_HEIGHT) # Scroll the actual camera position to the destination coords if self.camera_x != self.camera_dest_x: self.camera_x += (self.camera_dest_x - self.camera_x) / camera_scroll_speed if abs(self.camera_x - self.camera_dest_x) <= camera_lock_distance: self.camera_x = self.camera_dest_x if self.camera_y != self.camera_dest_y: self.camera_y += (self.camera_dest_y - self.camera_y) / camera_scroll_speed if abs(self.camera_y - self.camera_dest_y) <= camera_lock_distance: self.camera_y = self.camera_dest_y
def draw_three_digit_number(spr_digit_icons, number, team): # Restrict the number to 3 digits formatted_number = "{}".format(clamp(number, 0, 999)) display = pygame.Surface((24, 8), pygame.SRCALPHA, 32) x = 16 digits = [int(digit) for digit in formatted_number] digits.reverse() for digit in digits: if digit != -1: display.blit(spr_digit_icons[team][digit], (x, 0)) x -= 8 return display
def update_frame(self): self.index = self.get_index() if self.use_global_animation_frame and not (self.own_frames_for_index and self.index != 0): # Use the global frame global_frame = AnimatedGameObject.global_animation_frame if self.indexed: num_frames = self.image.get_height() // self.size - 1 else: num_frames = self.image.get_width() // self.size - 1 self.current_frame = clamp(global_frame, 0, num_frames) else: # Tick the image and update the frame self.current_frame += self.framerate / TICK_RATE if self.current_frame >= self.image.get_width() // self.size: self.current_frame = 0 self.on_animation_reset()
def test_in_bounds(self): self.assertEqual(clamp(10, 5, 15), 10)
def test_above_bound(self): self.assertEqual(clamp(20, 5, 15), 15)
def test_below_bound(self): self.assertEqual(clamp(0, 5, 15), 5)
def get_next_team(self, current_team, direction=1): available_teams = self.get_manager( Manager.TEAM).all_teams + [Team.NONE] return available_teams[clamp( available_teams.index(current_team) + direction, 0, len(available_teams) - 1)]
def get_next_piece_type(self, current_piece_type, direction=1): return PieceType( clamp(current_piece_type.value + direction, 1, len(PieceType) - 1))
def get_next_tile_type(self, current_tile_type, direction=1): return TileType( clamp(current_tile_type.value + direction, 1, len(TileType)))
def set_setting(self, setting, new_value): self.unsaved_settings[setting] = clamp(new_value, setting_bounds[setting][0], setting_bounds[setting][1])
def scroll_menu_down(self): self.menu_min = clamp(self.menu_min + 1, 0, self.num_options - self.max_displayable_options) self.clamp_menu_bounds() self.set_cursor_pos_to_mouse_coords()
def set_cursor_pos_to_mouse_coords(self): if self.cursor_in_menu_bounds(): self.menu_pos = clamp( (int(get_pos()[1] / SETTINGS.get(Setting.SCREEN_SCALE)) - self.root_y) // 24 + self.menu_min, self.menu_min, self.menu_max)
def clamp_menu_bounds(self): self.menu_min = clamp(self.menu_min, 0, self.num_options - self.max_displayable_options) self.menu_max = clamp(self.menu_min + self.max_displayable_options, 0, self.num_options)