pymunk.pygame_util
模块¶
此子模块包含帮助器函数,用于帮助使用pymunk和pygame或pygame-ce进行快速原型设计。
旨在帮助调试和原型设计,而不是在完整应用程序中进行实际生产使用。此模块中包含的方法是针对您的坐标系的,并且没有以任何方式优化。
- class pymunk.pygame_util.DrawOptions(surface: pygame.Surface)[源代码]¶
-
- __init__(surface: pygame.Surface) None [源代码]¶
在一个pygame.Surface对象上绘制一个pymunk.空格。
这个类应该与Pygame和Pygame-CE一起使用。
典型用法::
>>> import pymunk >>> surface = pygame.Surface((10,10)) >>> space = pymunk.Space() >>> options = pymunk.pygame_util.DrawOptions(surface) >>> space.debug_draw(options)
通过将shape.color设置为希望绘制形状的颜色,可以控制形状的颜色::
>>> c = pymunk.Circle(None, 10) >>> c.color = pygame.Color("pink")
有关完整示例,请参阅pyGame_util.demo.py
由于PYGAME使用的是坐标系统,其中y指向下方(与许多其他情况形成对比),因此您要么必须使Pymunk的物理模拟也以这种方式运行,要么在您绘制时翻转所有内容。
最简单的方法可能是使模拟的行为与PyGame的行为方式相同。这样,所有使用的坐标都在相同的方向上,并且很容易推断:
>>> space = pymunk.Space() >>> space.gravity = (0, -1000) >>> body = pymunk.Body() >>> body.position = (0, 0) # will be positioned in the top left corner >>> space.debug_draw(options)
要翻转绘图,可以设置模块属性
positive_y_is_up
致True。然后,在绘制之前,pyGame绘制将颠倒模拟::>>> positive_y_is_up = True >>> body = pymunk.Body() >>> body.position = (0, 0) >>> # Body will be position in bottom left corner
- 参数:
- 曲面pygame.Surface
将在其上绘制对象的曲面
- property collision_point_color: SpaceDebugColor¶
碰撞的颜色。
应为介于0和255(r,g,b,a)之间的4个整数的元组。
示例:
>>> import pymunk >>> s = pymunk.Space() >>> b = pymunk.Body(1,10) >>> c1 = pymunk.Circle(b, 10) >>> c2 = pymunk.Circle(s.static_body, 10) >>> s.add(b, c1, c2) >>> s.step(1) >>> options = pymunk.SpaceDebugDrawOptions() >>> s.debug_draw(options) draw_circle (Vec2d(0.0, 0.0), 0.0, 10.0, SpaceDebugColor(r=44.0, g=62.0, b=80.0, a=255.0), SpaceDebugColor(r=52.0, g=152.0, b=219.0, a=255.0)) draw_circle (Vec2d(0.0, 0.0), 0.0, 10.0, SpaceDebugColor(r=44.0, g=62.0, b=80.0, a=255.0), SpaceDebugColor(r=149.0, g=165.0, b=166.0, a=255.0)) draw_segment (Vec2d(8.0, 0.0), Vec2d(-8.0, 0.0), SpaceDebugColor(r=231.0, g=76.0, b=60.0, a=255.0)) >>> options.collision_point_color = (10,20,30,40) >>> s.debug_draw(options) draw_circle (Vec2d(0.0, 0.0), 0.0, 10.0, SpaceDebugColor(r=44.0, g=62.0, b=80.0, a=255.0), SpaceDebugColor(r=52.0, g=152.0, b=219.0, a=255.0)) draw_circle (Vec2d(0.0, 0.0), 0.0, 10.0, SpaceDebugColor(r=44.0, g=62.0, b=80.0, a=255.0), SpaceDebugColor(r=149.0, g=165.0, b=166.0, a=255.0)) draw_segment (Vec2d(8.0, 0.0), Vec2d(-8.0, 0.0), SpaceDebugColor(r=10.0, g=20.0, b=30.0, a=40.0))
- property constraint_color: SpaceDebugColor¶
约束的颜色。
应为介于0和255(r,g,b,a)之间的4个整数的元组。
示例:
>>> import pymunk >>> s = pymunk.Space() >>> b = pymunk.Body(1, 10) >>> j = pymunk.PivotJoint(s.static_body, b, (0,0)) >>> s.add(j) >>> options = pymunk.SpaceDebugDrawOptions() >>> s.debug_draw(options) draw_dot (5.0, Vec2d(0.0, 0.0), SpaceDebugColor(r=142.0, g=68.0, b=173.0, a=255.0)) draw_dot (5.0, Vec2d(0.0, 0.0), SpaceDebugColor(r=142.0, g=68.0, b=173.0, a=255.0)) >>> options.constraint_color = (10,20,30,40) >>> s.debug_draw(options) draw_dot (5.0, Vec2d(0.0, 0.0), SpaceDebugColor(r=10.0, g=20.0, b=30.0, a=40.0)) draw_dot (5.0, Vec2d(0.0, 0.0), SpaceDebugColor(r=10.0, g=20.0, b=30.0, a=40.0))
- draw_circle(pos: Vec2d, angle: float, radius: float, outline_color: SpaceDebugColor, fill_color: SpaceDebugColor) None [源代码]¶
- draw_fat_segment(a: tuple[float, float], b: tuple[float, float], radius: float, outline_color: SpaceDebugColor, fill_color: SpaceDebugColor) None [源代码]¶
- draw_polygon(verts: Sequence[tuple[float, float]], radius: float, outline_color: SpaceDebugColor, fill_color: SpaceDebugColor) None [源代码]¶
- property flags: int¶
位标志应绘制哪些形状、关节和碰撞。
默认情况下,所有3个标志都已设置,这意味着将绘制形状、关节和碰撞。
仅使用基本文本的示例IngDraw实现(通常您会使用所需的后台,例如 pygame_util.DrawOptions 或 pyglet_util.DrawOptions ):
>>> import pymunk >>> s = pymunk.Space() >>> b = pymunk.Body() >>> c = pymunk.Circle(b, 10) >>> c.mass = 3 >>> s.add(b, c) >>> s.add(pymunk.Circle(s.static_body, 3)) >>> s.step(0.01) >>> options = pymunk.SpaceDebugDrawOptions()
>>> # Only draw the shapes, nothing else: >>> options.flags = pymunk.SpaceDebugDrawOptions.DRAW_SHAPES >>> s.debug_draw(options) draw_circle (Vec2d(0.0, 0.0), 0.0, 10.0, SpaceDebugColor(r=44.0, g=62.0, b=80.0, a=255.0), SpaceDebugColor(r=52.0, g=152.0, b=219.0, a=255.0)) draw_circle (Vec2d(0.0, 0.0), 0.0, 3.0, SpaceDebugColor(r=44.0, g=62.0, b=80.0, a=255.0), SpaceDebugColor(r=149.0, g=165.0, b=166.0, a=255.0))
>>> # Draw the shapes and collision points: >>> options.flags = pymunk.SpaceDebugDrawOptions.DRAW_SHAPES >>> options.flags |= pymunk.SpaceDebugDrawOptions.DRAW_COLLISION_POINTS >>> s.debug_draw(options) draw_circle (Vec2d(0.0, 0.0), 0.0, 10.0, SpaceDebugColor(r=44.0, g=62.0, b=80.0, a=255.0), SpaceDebugColor(r=52.0, g=152.0, b=219.0, a=255.0)) draw_circle (Vec2d(0.0, 0.0), 0.0, 3.0, SpaceDebugColor(r=44.0, g=62.0, b=80.0, a=255.0), SpaceDebugColor(r=149.0, g=165.0, b=166.0, a=255.0)) draw_segment (Vec2d(1.0, 0.0), Vec2d(-8.0, 0.0), SpaceDebugColor(r=231.0, g=76.0, b=60.0, a=255.0))
- shape_dynamic_color: SpaceDebugColor = (52, 152, 219, 255)¶
- shape_kinematic_color: SpaceDebugColor = (39, 174, 96, 255)¶
- property shape_outline_color: SpaceDebugColor¶
形状的轮廓颜色。
应为介于0和255(r,g,b,a)之间的4个整数的元组。
示例:
>>> import pymunk >>> s = pymunk.Space() >>> c = pymunk.Circle(s.static_body, 10) >>> s.add(c) >>> options = pymunk.SpaceDebugDrawOptions() >>> s.debug_draw(options) draw_circle (Vec2d(0.0, 0.0), 0.0, 10.0, SpaceDebugColor(r=44.0, g=62.0, b=80.0, a=255.0), SpaceDebugColor(r=149.0, g=165.0, b=166.0, a=255.0)) >>> options.shape_outline_color = (10,20,30,40) >>> s.debug_draw(options) draw_circle (Vec2d(0.0, 0.0), 0.0, 10.0, SpaceDebugColor(r=10.0, g=20.0, b=30.0, a=40.0), SpaceDebugColor(r=149.0, g=165.0, b=166.0, a=255.0))
- shape_sleeping_color: SpaceDebugColor = (114, 148, 168, 255)¶
- shape_static_color: SpaceDebugColor = (149, 165, 166, 255)¶
- property transform: Transform¶
转换在绘制之前应用,例如用于缩放或平移。
示例:
>>> import pymunk >>> s = pymunk.Space() >>> c = pymunk.Circle(s.static_body, 10) >>> s.add(c) >>> options = pymunk.SpaceDebugDrawOptions() >>> s.debug_draw(options) draw_circle (Vec2d(0.0, 0.0), 0.0, 10.0, SpaceDebugColor(r=44.0, g=62.0, b=80.0, a=255.0), SpaceDebugColor(r=149.0, g=165.0, b=166.0, a=255.0)) >>> options.transform = pymunk.Transform.scaling(2) >>> s.debug_draw(options) draw_circle (Vec2d(0.0, 0.0), 0.0, 20.0, SpaceDebugColor(r=44.0, g=62.0, b=80.0, a=255.0), SpaceDebugColor(r=149.0, g=165.0, b=166.0, a=255.0)) >>> options.transform = pymunk.Transform.translation(2,3) >>> s.debug_draw(options) draw_circle (Vec2d(2.0, 3.0), 0.0, 10.0, SpaceDebugColor(r=44.0, g=62.0, b=80.0, a=255.0), SpaceDebugColor(r=149.0, g=165.0, b=166.0, a=255.0))
备注
并非所有转换都受到调试绘制逻辑的支持。支持均匀的缩放和平移,但不支持旋转、线性拉伸或剪切。
- pymunk.pygame_util.from_pygame(p: tuple[float, float], surface: pygame.Surface) tuple[int, int] [源代码]¶
一种将PYGAME曲面局部坐标转换为PYMUNK坐标的简便方法
- pymunk.pygame_util.get_mouse_pos(surface: pygame.Surface) tuple[int, int] [源代码]¶
获取鼠标指针在Pymunk坐标中的位置。
- pymunk.pygame_util.positive_y_is_up: bool = False¶
使y的递增值指向上方。
如果为True::
y ^ | . (3, 3) | | . (2, 2) | +------ > x
如果为False::
+------ > x | | . (2, 2) | | . (3, 3) v y