import pygame, sys, pymunk def create_apple(space): body = pymunk.Body(1, 100, body_type=pymunk.Body.DYNAMIC) body.position = (950, 0) shape = pymunk.Circle(body, 80) shape.elasticity = 0.8 # Make apples bounce space.add(body, shape) return shape def draw_apples(apples): for apple in apples: pygame.draw.circle(screen, (0, 0, 0), (int(apple.body.position.x), int(apple.body.position.y)), 80) def static_ball(space): body = pymunk.Body(body_type=pymunk.Body.STATIC) body.position = (950, 950) 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) shape = pymunk.Segment(body, (600, 800), (1300, 800), 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) # Pygame and Pymunk setup pygame.init() screen = pygame.display.set_mode((1900, 1100)) clock = pygame.time.Clock() space = pymunk.Space() space.gravity = (0, 980) # Create objects apples = [create_apple(space)] balls = [static_ball(space)] bounce_line = create_bounce_line(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_SPACE: apples.append(create_apple(space)) if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() screen.fill((217, 217, 217)) draw_apples(apples) draw_static_ball(balls) draw_bounce_line(bounce_line) space.step(1/50) pygame.display.update() clock.tick(120)