# 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.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))
import pygame
import pymunk
import pymunk.pygame_util
from pygame.locals import *
from enum import Enum
import math
# Configuración de constantes
WIDTH, HEIGHT = 800, 600
FPS = 60
GRAVITY = 900 # en píxeles por segundo^2
LAUNCH_POWER = 100 # Reducido de 500 a 100
MAX_FORCE = 1000 # Fuerza máxima
MAX_VELOCITY = 500 # Velocidad máxima en píxeles por segundo
# Estados del tutorial
class TutorialState(Enum):
START = 0
DRAG = 1
RELEASE = 2
COMPLETED = 3
# Clase para objetos del juego
class GameObject:
def __init__(self, space, pos, radius=20, mass=1):
self.body = pymunk.Body(mass, pymunk.moment_for_circle(mass, 0, radius))
self.body.position = pos
self.shape = pymunk.Circle(self.body, radius)
self.shape.elasticity = 0.5
self.shape.friction = 0.5
space.add(self.body, self.shape)
self.radius = radius
def draw(self, screen):
pos = int(self.body.position.x), int(self.body.position.y)
pygame.draw.circle(screen, (0, 0, 255), pos, self.radius)
# Clase del Juego
class Game:
def __init__(self):
pygame.init()
pygame.display.set_caption("Juego de Física: Lanzamiento de Objetos")
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.clock = pygame.time.Clock()
self.font = pygame.font.SysFont("Arial", 18)
# 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-100)) # Aumenta la altura inicial
# 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
# Limitar la velocidad del objeto
vx, vy = self.game_object.body.velocity
speed = math.hypot(vx, vy)
if speed > MAX_VELOCITY:
scale = MAX_VELOCITY / speed
self.game_object.body.velocity = vx * scale, vy * scale
# 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]
drag_distance = math.hypot(dx, dy)
if drag_distance != 0:
direction = (dx / drag_distance, dy / drag_distance)
else:
direction = (0, 0)
force_magnitude = min(drag_distance * LAUNCH_POWER, MAX_FORCE)
force = (direction[0] * force_magnitude, direction[1] * force_magnitude)
self.game_object.body.apply_impulse_at_local_point(force)
print(f"Impulso aplicado: {force}")
print(f"Posición después del impulso: {self.game_object.body.position}")
# 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)
pygame.draw.circle(self.screen, (0, 255, 0), self.drag_start, 5) # Punto inicial
pygame.draw.circle(self.screen, (0, 0, 255), current_pos, 5) # Punto final
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))
if __name__ == "__main__":
game = Game()
game.run()