# Configuración de Pymunk
self.space = pymunk.Space()
self.space.gravity = (0, GRAVITY)
self.draw_options = pymunk.pygame_util.DrawOptions(self.screen)
# Crear suelo
ground = pymunk.Segment(self.space.static_body, (0, HEIGHT-50), (WIDTH, HEIGHT-50), 5)
ground.friction = 1.0
self.space.add(ground)
# Crear un objeto lanzable
self.game_object = GameObject(self.space, pos=(100, HEIGHT-70))
# Tutorial
self.tutorial_state = TutorialState.START
self.drag_start = None
self.drag_end = None
def run(self):
running = True
while running:
dt = self.clock.tick(FPS) / 1000 # Delta time en segundos
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == MOUSEBUTTONDOWN and event.button == 1:
if self.tutorial_state == TutorialState.START:
self.tutorial_state = TutorialState.DRAG
if self.tutorial_state in [TutorialState.DRAG, TutorialState.START]:
mouse_pos = pygame.mouse.get_pos()
if self.game_object.shape.point_query(mouse_pos).distance <= 0:
self.drag_start = mouse_pos
elif event.type == MOUSEBUTTONUP and event.button == 1:
if self.drag_start and self.tutorial_state in [TutorialState.DRAG, TutorialState.START]:
self.drag_end = pygame.mouse.get_pos()
self.launch_object()
self.tutorial_state = TutorialState.RELEASE
# Actualizar física
self.space.step(dt)
# Dibujar todo
self.screen.fill((255, 255, 255))
self.space.debug_draw(self.draw_options)
self.game_object.draw(self.screen)
# Dibujar tutorial
self.draw_tutorial()
pygame.display.flip()
pygame.quit()
def launch_object(self):
if self.drag_start and self.drag_end:
dx = self.drag_start[0] - self.drag_end[0]
dy = self.drag_start[1] - self.drag_end[1]
force = (dx * LAUNCH_POWER, dy * LAUNCH_POWER)
self.game_object.body.body_type = pymunk.Body.DYNAMIC
self.game_object.body.apply_impulse_at_local_point(force)
# Reset drag positions
self.drag_start = None
self.drag_end = None
# Completar el tutorial
if self.tutorial_state == TutorialState.RELEASE:
self.tutorial_state = TutorialState.COMPLETED
def draw_tutorial(self):
if self.tutorial_state == TutorialState.START:
text = self.font.render("Haz clic y arrastra sobre el objeto para lanzarlo.", True, (0,0,0))
self.screen.blit(text, (20, 20))
elif self.tutorial_state == TutorialState.DRAG and self.drag_start:
current_pos = pygame.mouse.get_pos()
# Dibujar una línea desde el objeto hasta la posición actual del mouse
pygame.draw.line(self.screen, (255, 0, 0), self.drag_start, current_pos, 3)
text = self.font.render("Arrastra para determinar la dirección y fuerza.", True, (0,0,0))
self.screen.blit(text, (20, 20))
elif self.tutorial_state == TutorialState.RELEASE:
text = self.font.render("¡Bien hecho! Has lanzado el objeto. Completa el tutorial.", True, (0,0,0))
self.screen.blit(text, (20, 20))
elif self.tutorial_state == TutorialState.COMPLETED:
text = self.font.render("Tutorial completado. ¡Resuelve los puzzles lanzando objetos!", True, (0,0,0))
self.screen.blit(text, (20, 20))
Aquí estás intentando cambiar el tipo de cuerpo del objeto lanzable a DYNAMIC justo antes de aplicar la fuerza de impulso. Sin embargo, el cuerpo ya debería ser dinámico desde su creación para poder ser manipulado de esa manera. El problema parece estar en que en pymunk.Body el tipo de cuerpo por defecto es DYNAMIC, por lo tanto al crear el objeto no necesitas cambiarlo después.
Si todavía no ves el objeto moverse, revisa la lógica de dibujo en el bucle del juego para confirmar que siempre se está llamando a self.game_object.draw(self.screen) después de actualizar la física (self.space.step(dt)). Esto asegura que la posición actualizada del objeto se dibuja en pantalla.