Pygame基础10-物理模拟-案例

时间:2024-04-05 16:18:33

下面是一个完整示例,模拟苹果掉落的过程。
在这里插入图片描述

import sys
import pygame
import pymunk


pygame.init()
screen_size = (800, 600)
screen = pygame.display.set_mode(screen_size)
clock = pygame.time.Clock()

space = pymunk.Space()
space.gravity = (0.0, 100.0)


def create_apple(space, pos):
    body = pymunk.Body(mass=1, moment=10, body_type=pymunk.Body.DYNAMIC) # DYNAMIC 类型的物体会受到力的影响
    body.position = pos
    shape = pymunk.Circle(body, radius=10)
    space.add(body, shape)
    return shape


def draw_apple(apples):
    for apple in apples:
        pos_x = int(apple.body.position.x)
        pos_y = int(apple.body.position.y)
        pygame.draw.circle(screen, (255, 0, 0), (pos_x, pos_y), 10)



def create_static_ball(space, pos):
    body = pymunk.Body(body_type=pymunk.Body.STATIC) # STATIC 类型的物体 不会移动
    body.position = pos
    shape = pymunk.Circle(body, radius=10)
    space.add(body, shape)
    return shape

def draw_balls(balls):
    for ball in balls:
        pos_x = int(ball.body.position.x)
        pos_y = int(ball.body.position.y)
        pygame.draw.circle(screen, (0, 255, 0), (pos_x, pos_y), 10)

apples =[]

balls = []
balls.extend([create_static_ball(space, (400, 300)),
             create_static_ball(space, (500, 400))
            ])

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            apple = create_apple(space, pos)
            apples.append(apple)

    screen.fill((35, 35, 35))
    draw_apple(apples)
    draw_balls(balls)

    space.step(1/60.0)

   
    pygame.display.flip()
    clock.tick(60)