Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import math
- pygame.init()
- screen = pygame.display.set_mode((800, 600))
- clock = pygame.time.Clock()
- agent_pos = (400, 300)
- directions = [(math.cos(math.radians(angle)), math.sin(math.radians(angle))) for angle in range(0, 360, 45)]
- line_length = 100
- class Agent:
- def __init__(self, position, speed, threshold, num_directions):
- self.position = position
- self.speed = speed
- self.threshold = threshold
- self.directions = [(math.cos(math.radians(angle)), math.sin(math.radians(angle))) for angle in
- range(0, 360, 360 // num_directions)]
- self.context_map = {direction: 0 for direction in self.directions}
- self.active_direction = None
- def update_context_map(self, obstacles, targets):
- target_map = {direction: 0 for direction in self.directions}
- obstacle_map = {direction: 0 for direction in self.directions}
- for target in targets:
- target_vector = [(target.position[0] - self.position[0]), (target.position[1] - self.position[1])]
- magnitude = math.hypot(target_vector[0], target_vector[1])
- if magnitude > 0:
- normalized_target_vector = [target_vector[0] / magnitude, target_vector[1] / magnitude]
- for direction in self.directions:
- dot_product = direction[0] * normalized_target_vector[0] + direction[1] * normalized_target_vector[
- 1]
- target_map[direction] += dot_product / magnitude
- for obstacle in obstacles:
- obstacle_vector = [(self.position[0] - obstacle.position[0]), (self.position[1] - obstacle.position[1])]
- distance = math.hypot(obstacle_vector[0], obstacle_vector[1])
- if distance < self.threshold:
- normalized_obstacle_vector = [obstacle_vector[0] / distance, obstacle_vector[1] / distance]
- for direction in self.directions:
- dot_product = direction[0] * normalized_obstacle_vector[0] + direction[1] * \
- normalized_obstacle_vector[1]
- obstacle_map[direction] -= dot_product / distance
- max_target_value = max(target_map.values()) if target_map else 1
- max_obstacle_value = max(obstacle_map.values()) if obstacle_map else 1
- for direction in self.directions:
- target_map[direction] = target_map[direction] / max_target_value if max_target_value > 0 else 0
- obstacle_map[direction] = obstacle_map[direction] / max_obstacle_value if max_obstacle_value > 0 else 0
- self.context_map[direction] = target_map[direction] - obstacle_map[direction]
- def move(self):
- self.active_direction = max(self.directions, key=lambda d: self.context_map[d])
- if self.context_map[self.active_direction] > 0:
- self.position = (self.position[0] + self.active_direction[0] * self.speed,
- self.position[1] + self.active_direction[1] * self.speed)
- def draw(self, screen):
- pygame.draw.circle(screen, (255, 0, 0), self.position, 10)
- for direction in self.directions:
- weight = self.context_map[direction]
- end_pos = (self.position[0] + direction[0] * line_length * weight,
- self.position[1] + direction[1] * line_length * weight)
- color = (255, 0, 0) if direction == self.active_direction else (0, 255, 0)
- pygame.draw.line(screen, color, self.position, end_pos)
- class Obstacle:
- def __init__(self, position):
- self.position = position
- def draw(self, screen):
- pygame.draw.circle(screen, (0, 0, 255), self.position, 10)
- class Target:
- def __init__(self, position):
- self.position = position
- def draw(self, screen):
- pygame.draw.circle(screen, (0, 255, 0), self.position, 10)
- obstacles = []
- targets = []
- agent = Agent((400, 300), 2, 150, 8)
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- elif event.type == pygame.MOUSEBUTTONDOWN:
- mouse_pos = pygame.mouse.get_pos()
- if event.button == 1:
- obstacles.append(Obstacle(mouse_pos))
- elif event.button == 3:
- targets.append(Target(mouse_pos))
- screen.fill((0, 0, 0))
- agent.update_context_map(obstacles, targets)
- agent.move()
- agent.draw(screen)
- for obstacle in obstacles:
- obstacle.draw(screen)
- for target in targets:
- target.draw(screen)
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment