pymunk 官网
pymunk GitHub

# 一、安装pymunk

pip3 pymunk -i https://mirrors.aliyun.com/pypi/simple


# 二、简单使用

## 1.导入包

import random

import pygame
from pygame.key import *
from pygame.locals import *
from pygame.color import *

import pymunk
import pymunk.pygame_util


## 2.创建空间及初始化

# pymunk初始化
space = pymunk.Space()        # 空间
space.gravity = (0.0, -900.0) # 设置重力

# pygame初始化
pygame.init()
screen = pygame.display.set_mode((600, 600))
clock = pygame.time.Clock()
# 在pygame上创建画板
draw_options = pymunk.pygame_util.DrawOptions(screen)

FPS = 60
balls = []  # 所有的球
ticks_to_next_ball = 10  # 多少帧后出现下一个球
exact = 10  # 一帧计算几次


## 3.创建静态物体

pymunk.Segment 是静态物体类，我们通过它实例化物体。

static_body = space.static_body
static_lines = [
pymunk.Segment(static_body, (111.0, 280.0), (407.0, 246.0), 0.0),
pymunk.Segment(static_body, (407.0, 246.0), (407.0, 343.0), 0.0)
]
for line in static_lines:
line.elasticity = 0.95  # 弹性系数 0-1
line.friction = 0.9     # 摩擦系数 0-1


## 4.创建物体

pymunk.Body 是物体类
pymunk.Circle是圆类（其父类是Shape 形状类）

# 创建一个球
def create_ball():
mass = 10   # 质量
inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
body = pymunk.Body(mass, inertia)
x = random.randint(115, 350)
body.position = x, 400
shape = pymunk.Circle(body, radius, (0, 0))
shape.elasticity = 0.95
shape.friction = 0.9
balls.append(shape)


## 5.移除物体

space.remove(Shape, Body)

def update_balls():
global ticks_to_next_ball
# 根据需要创建/移除球。每帧只调用一次。
ticks_to_next_ball -= 1
if ticks_to_next_ball <= 0:
create_ball()
ticks_to_next_ball = 100
# 移除低于100的球
balls_to_remove = [ball for ball in balls if ball.body.position.y < 100]
for ball in balls_to_remove:
space.remove(ball, ball.body)
balls.remove(ball)


## 6.写侦听事件

pygame.image.save(screen, “bouncing_balls.png”)

def my_events():
for event in pygame.event.get():
if event.type == QUIT:
exit()
elif event.type == KEYDOWN and event.key == K_ESCAPE:
exit()
elif event.type == KEYDOWN and event.key == K_p:
# 截图
pygame.image.save(screen, "bouncing_balls.png")


## 7.其他函数

# 清除屏幕
def clear_screen():
screen.fill(THECOLORS["white"])

# 画对象
def draw_objects():
space.debug_draw(draw_options)


## 8.主循环

space.step(time)

while True:
# 计算下一帧位置
for x in range(exact):
space.step(1/FPS/exact)

my_events()
update_balls()
clear_screen()
draw_objects()
pygame.display.flip()
clock.tick(FPS)
pygame.display.set_caption("fps: " + str(clock.get_fps()))


08-12 1708

08-10 3519
10-06 1万+
03-31 7014
08-29 2732
11-07 4054
02-01 1万+
05-22 4545
08-30 1859