import pygame, sys, pymunk def create_apple(space, pos, velocity): body = pymunk.Body(1, 100, body_type=pymunk.Body.DYNAMIC) body.position = pos body.velocity = velocity # Set the velocity from the release drag shape = pymunk.Circle(body, 50) space.add(body, shape) return shape def draw_apples(apples): for apple in apples: pos_x = int(apple.body.position.x) pos_y = int(apple.body.position.y) pygame.draw.circle(screen, (250,250,250), (pos_x, pos_y), 50) def static_ball(space, pos): body = pymunk.Body(body_type=pymunk.Body.STATIC) body.position = pos shape = pymunk.Circle(body, 30) space.add(body, shape) return shape def draw_static_ball(balls): for ball in balls: pos_x = int(ball.body.position.x) pos_y = int(ball.body.position.y) pygame.draw.circle(screen, (0,0,0), (pos_x, pos_y), 30) def ghost(pos, surface): temp_surface = pygame.Surface((100, 100), pygame.SRCALPHA) pygame.draw.circle(temp_surface, (0, 0, 0, 128), (50, 50), 50) pygame.draw.circle(temp_surface, (0, 0, 0, 179), (50, 50), 30) screen.blit(temp_surface, (pos[0] - 50, pos[1] - 50)) pygame.init() screen = pygame.display.set_mode((800, 800)) surface = pygame.Surface((800, 800), pygame.SRCALPHA) clock = pygame.time.Clock() space = pymunk.Space() space.gravity = (0, 200) apples = [] balls = [] # Removed velocity history and previous mouse pos. dragging_apple = None prev_drag_pos = None last_drag_velocity = (0, 0) last_move_time = 0 # in milliseconds while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: # When left click is pressed, create a new apple (if not already dragging one) if dragging_apple is None: apple = create_apple(space, event.pos, (0, 0)) apples.append(apple) dragging_apple = apple prev_drag_pos = event.pos last_drag_velocity = (0, 0) last_move_time = pygame.time.get_ticks() if event.type == pygame.MOUSEBUTTONUP and event.button == 1: # On left click release, launch the dragged apple using the last computed drag velocity. if dragging_apple is not None: # Use last_drag_velocity even if mouse stops moving at release dragging_apple.body.velocity = (last_drag_velocity[0] * 20, last_drag_velocity[1] * 20) dragging_apple = None prev_drag_pos = None last_drag_velocity = (0, 0) last_move_time = 0 if event.type == pygame.MOUSEBUTTONDOWN and event.button == 3: balls.append(static_ball(space, event.pos)) if event.type == pygame.MOUSEBUTTONDOWN and event.button == 2: for apple in apples: space.remove(apple, apple.body) apples.clear() for ball in balls: space.remove(ball, ball.body) balls.clear() # If an apple is being dragged, update its position to follow the mouse and compute drag velocity. if dragging_apple is not None: current_mouse = pygame.mouse.get_pos() current_time = pygame.time.get_ticks() delta = (current_mouse[0] - prev_drag_pos[0], current_mouse[1] - prev_drag_pos[1]) # If the mouse moved, update last_drag_velocity and the timestamp. if delta != (0, 0): last_drag_velocity = delta last_move_time = current_time else: # If no movement for more than 1000 ms, reset the drag velocity. if current_time - last_move_time > 1000: last_drag_velocity = (0, 0) dragging_apple.body.position = current_mouse prev_drag_pos = current_mouse screen.fill((217, 217, 217)) draw_static_ball(balls) draw_apples(apples) ghost(pygame.mouse.get_pos(), surface) space.step(1/50) pygame.display.update() clock.tick(150)