Skip to content

Commit

Permalink
refactor: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniavm committed Nov 5, 2024
2 parents c5e4f73 + 1426092 commit 2bc4923
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 29 deletions.
22 changes: 6 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from src.render_object import RenderObject
from src.interface import Button, Interface
from src.agent_parts.limb import Limb
from src.agent_parts.rectangle import Point, Rectangle, rectangle_factory
from src.agent_parts.rectangle import Point


from src.agent_parts.rectangle import Point
Expand Down Expand Up @@ -143,35 +143,25 @@ def make_limb():
interface.add_button(pause_button)


environment = Environment(screen)
environment.ground_type = GroundType.BASIC_GROUND

# Set up the Pymunk space
space = pymunk.Space()
space.gravity = (0, 981) # Gravity pointing downward

environment = Environment(screen, space)
environment.ground_type = GroundType.BASIC_GROUND
for environment_segment in environment.ground.terrain_segments:
environment_segment.init_pymunk_polygon(space)

creature = Creature(space)

# Add limbs to the creature, placing them above the ground
#limb1 = creature.add_limb(100, 20, (300, 100), mass=1) # Positioned above the ground
#limb2 = creature.add_limb(100, 20, (350, 100), mass=1) # Positioned above the ground
#limb3 = creature.add_limb(110, 20, (400, 100), mass=5)

# Add a motor between limbs
#creature.add_motor(limb1, limb2, (50, 0), (-25, 0), rate=2, tolerance=30)
#creature.add_motor(limb2, limb3, (37, 0), (-23, 0), rate=-2, tolerance=50)


# Add limbs to the creature
limb1 = creature.add_limb(100, 20, (300, 300), mass=1)
limb2 = creature.add_limb(100, 20, (350, 300), mass=3)
limb3 = creature.add_limb(80, 40, (400, 300), mass=5)

# Add motors between limbs
#creature.add_motor(limb1, limb2, (25, 0), (-25, 0), rate=2)
#creature.add_motor(limb2, limb3, (37, 0), (-23, 0), rate=-2)
creature.add_motor_on_limbs(limb1, limb2, (325, 300))
creature.add_motor_on_limbs(limb2, limb3, (375, 300))

Expand All @@ -186,7 +176,7 @@ def make_limb():
limbs = []

environment = Environment(screen, space)
environment.ground_type = GroundType.BASIC_GROUND
environment.ground_type = GroundType.PERLIN

population_size = 10
creatures = create_creatures(population_size, space)
Expand Down Expand Up @@ -271,11 +261,11 @@ def make_limb():


# TODO: vision should be part of a creature, and not environment
print(environment.vision.get_near_periphery())
inputs = np.array([environment.vision.get_near_periphery().x,
environment.vision.get_near_periphery().y,
environment.vision.get_far_periphery().x,
environment.vision.get_far_periphery().y])

for index, creature in enumerate(creatures):
network = population[index]
for joint_rate in creature.get_joint_rates():
Expand Down
1 change: 0 additions & 1 deletion src/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from src.agent_parts_old.limb import Limb, LimbType, limb_factory
from src.agent_parts_old.creature import Creature, creature_factory
from CrawlAI.src.agent_parts.rectangle import Rectangle, rectangle_factory


class Agent(ABC):
Expand Down
2 changes: 1 addition & 1 deletion src/agent_parts/limb.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ def render(self, screen):
# Convert pymunk Vec2d vertices to pygame coordinates
vertices = [(float(v.x), float(v.y)) for v in vertices]
# Draw the polygon onto the screen
pygame.draw.polygon(surface=screen, color=(0, 255, 0), vertices=vertices, width=0)
pygame.draw.polygon(surface=screen, color=(0, 255, 0), points=vertices, width=0)
11 changes: 5 additions & 6 deletions src/agent_parts/motorjoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ def render(self, screen, body_a, body_b):
pos_a_world = body_a.local_to_world(self.pivot.anchor_a)
# Draw a line connecting the two bodies
pygame.draw.circle(
screen,
(255, 0, 0),
(int(pos_a_world.x),
surface=screen,
color=(255, 0, 0),
point=(int(pos_a_world.x),
int(pos_a_world.y)),
3)
radius=3)

def get_angle(self):
"""Get the angle of the motor joint."""
return self.pivot.angle
# Draw a circle connecting the two bodies
pygame.draw.circle(surface=screen, color=(255, 0, 0), center=(int(pos_a_world.x), int(pos_a_world.y)), radius=3)

9 changes: 4 additions & 5 deletions src/ground.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,10 @@ def get_y(self, x: int) -> int:
x-coordinate
"""

norma = SCREEN_WIDTH / PERLIN_SEGMENTS
real_x = (x + self.offset) / norma
y = self.generate_y(real_x)
pix_y = SCREEN_HEIGHT / 2 + y
return pix_y
for i, point in enumerate(self.render_points):
if point[0] <= x <= self.render_points[i+1][0]:
return int((point[1]+self.render_points[i+1][1])/2)
return None

def generate_y(self, x: int) -> int:
"""_summary_ Calculate the y value of the Perlin noise at the given x
Expand Down

0 comments on commit 2bc4923

Please sign in to comment.