import pygame, sys, pymunk # Pygame and Pymunk setup pygame.init() info = pygame.display.Info() SCREEN_WIDTH = info.current_w SCREEN_HEIGHT = info.current_h screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.FULLSCREEN | pygame.SCALED) clock = pygame.time.Clock() space = pymunk.Space() space.gravity = (0, 980) def create_apple(space): body = pymunk.Body(1, 100, body_type=pymunk.Body.DYNAMIC) body.position = (SCREEN_WIDTH // 2, 0) shape = pymunk.Circle(body, 80) shape.elasticity = 1 space.add(body, shape) return shape def draw_apples(apples): for apple in apples: apple_pos = (int(apple.body.position.x), int(apple.body.position.y)) pygame.draw.circle(screen, (0, 0, 0), apple_pos, 80) def static_ball(space): body = pymunk.Body(body_type=pymunk.Body.STATIC) body.position = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) shape = pymunk.Circle(body, 50) shape.elasticity = 0.8 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), 50) def create_bounce_line(space): body = pymunk.Body(body_type=pymunk.Body.STATIC) body.position = (0, 0) y_position = SCREEN_HEIGHT - 300 shape = pymunk.Segment(body, (0, y_position), (SCREEN_WIDTH, y_position), 5) shape.elasticity = 1.0 shape.friction = 0.5 space.add(body, shape) return shape def draw_bounce_line(line): pv1 = line.a pv2 = line.b pygame.draw.line(screen, (255, 0, 0), pv1, pv2, 5) def create_walls(space): static_lines = [ pymunk.Segment(space.static_body, (0, 0), (SCREEN_WIDTH, 0), 5), pymunk.Segment(space.static_body, (0, SCREEN_HEIGHT), (SCREEN_WIDTH, SCREEN_HEIGHT), 5), pymunk.Segment(space.static_body, (0, 0), (0, SCREEN_HEIGHT), 5), pymunk.Segment(space.static_body, (SCREEN_WIDTH, 0), (SCREEN_WIDTH, SCREEN_HEIGHT), 5), ] for line in static_lines: line.elasticity = 0.9 line.friction = 0.5 space.add(*static_lines) def cast_rays(space, origin, num_rays=60, ray_length=1000): angle_step = 360 / num_rays for i in range(num_rays): angle = i * angle_step radians = angle * (3.14159265 / 180) dx = ray_length * pymunk.Vec2d(1, 0).rotated(radians) end_point = origin + dx hit = space.segment_query_first(origin, end_point, 0, pymunk.ShapeFilter()) if hit: pygame.draw.line(screen, (255, 100, 100), origin, hit.point, 1) else: pygame.draw.line(screen, (200, 200, 200), origin, end_point, 1) # Create objects apples = [create_apple(space)] balls = [static_ball(space)] bounce_line = create_bounce_line(space) create_walls(space) # Main game loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() keys = pygame.key.get_pressed() if keys[pygame.K_SPACE]: apples.append(create_apple(space)) if keys[pygame.K_a]: for apple in apples: apple.body.apply_force_at_local_point((-325, 0)) if keys[pygame.K_d]: for apple in apples: apple.body.apply_force_at_local_point((325, 0)) if keys[pygame.K_w]: for apple in apples: apple.body.apply_force_at_local_point((0, -2050)) screen.fill((217, 217, 217)) # Cast rays from every apple for apple in apples: cast_rays(space, apple.body.position, num_rays=60, ray_length=800) draw_apples(apples) draw_static_ball(balls) draw_bounce_line(bounce_line) space.step(1 / 50) pygame.display.update() clock.tick(120)