Skip to content

Commit

Permalink
feat: add init_pymunk stuff in basicground
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasfremming committed Nov 5, 2024
1 parent a76e280 commit cd26916
Showing 1 changed file with 0 additions and 125 deletions.
125 changes: 0 additions & 125 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,109 +74,26 @@ def create_population(population_size, creature: Creature):

def main():


#Initializes Genetic Algorithm

genetic_algorithm = GeneticAlgorithm()

def create_creature(in_nodes: int,out_nodes: int):
population = genetic_algorithm.initialize_population(200, in_nodes, out_nodes)

return population

fitness = []

# Initialize Pygame and Pymunk

pygame.init()
screen_width, screen_height = SCREEN_WIDTH, SCREEN_HEIGHT
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pymunk Rectangle Physics")
interface = Interface()


# Track whether physics is on or off
physics_on = False
physics_value = 0

def handle_physics():
nonlocal physics_on
nonlocal physics_value
physics_value = 1/60.0 if physics_value == 0 else 0
physics_on = True if physics_value != 0 else False
print("Physics Enabled" if physics_value != 0 else "Physics Disabled")

font = pygame.font.Font(None, 20)

pause_button = Button(
text="Pause",
pos=(10, 10),
width=100,
height=30,
font=font,
color=(70, 130, 180),
hover_color=(100, 149, 237),
text_color=(255, 255, 255),
active_color=(200, 100, 100),
callback=handle_physics
)

make_limb_mode = False

def make_limb():
nonlocal make_limb_mode
make_limb_mode = not make_limb_mode

limb_button = Button(
text="Add limb",
pos=(10, 50),
width=100,
height=30,
font=font,
color=(70, 130, 180),
hover_color=(100, 149, 237),
text_color=(255, 255, 255),
active_color=(200, 100, 100),
callback=make_limb
)

interface.add_button(pause_button)



# 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
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)

creature.add_motor_on_limbs(limb1, limb2, (325, 300))
creature.add_motor_on_limbs(limb2, limb3, (375, 300))

#dragging creature properties
dragging = False
dragged_limb = None
drag_offset = []

# creating rectangles properties
start_pos = None
end_pos = None
limbs = []

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

population_size = 10
creatures = create_creatures(population_size, space)
Expand All @@ -202,48 +119,8 @@ def make_limb():
print("Right arrow pressed")
if event.key == pygame.K_SPACE:
handle_physics()
elif event.type == pygame.MOUSEBUTTONDOWN:
if not physics_on:
mouse_x, mouse_y = event.pos
mouse_pos = (mouse_x, mouse_y)
# For dragging creature: Check if the mouse is over any limb
if not make_limb_mode:
for limb in creature.limbs:
if limb.contains_point(mouse_pos):
dragging = True
dragged_limb = limb
creature.start_dragging(dragged_limb)
drag_offset = (limb.body.position.x - mouse_x, limb.body.position.y - mouse_y)
break
# For creating rectangles
elif make_limb_mode:
start_pos = mouse_pos

elif event.type == MOUSEMOTION and make_limb_mode:
mouse_x, mouse_y = event.pos
mouse_pos = (mouse_x, mouse_y)
end_pos = mouse_pos

elif event.type == pygame.MOUSEBUTTONUP:
dragging = False
if make_limb_mode and start_pos and end_pos:
width = abs(start_pos[0] - end_pos[0])
height = abs(start_pos[1] - end_pos[1])
position = ((start_pos[0] + end_pos[0]) / 2, (start_pos[1] + end_pos[1]) / 2)
limb = creature.add_limb(width, height, position)

# Reset start and end positions
start_pos = None
end_pos = None


space.step(physics_value)

if dragging and dragged_limb and not make_limb_mode:
mouse_x, mouse_y = pygame.mouse.get_pos()
new_position = (mouse_x + drag_offset[0], mouse_y + drag_offset[1])
creature.update_creature_position(dragged_limb, new_position)

screen.fill((135, 206, 235))
environment.update()
environment.render()
Expand All @@ -258,8 +135,6 @@ def make_limb():
interface.remove_button(limb_button)
interface.render(screen)



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

0 comments on commit cd26916

Please sign in to comment.