我想创建一个弹球游戏,但球有时不会与其他物体发生碰撞。示例:https ://youtu.be/HwSXwJ4-d2w
这是代码
import pyglet, pymunk
from pymunk.pyglet_util import DrawOptions
win = pyglet.window.Window(1280, 720, resizable=False)
options = DrawOptions()
space = pymunk.Space()
space.gravity = 0, -1000
def Ball(mass, radius, coords):
circle_moment = pymunk.moment_for_circle(mass, 0, radius)
circle_body = pymunk.Body(mass, circle_moment)
circle_shape = pymunk.Circle(circle_body, radius)
circle_shape.elasticity = 1.0
circle_body.position= coords
space.add(circle_body, circle_shape)
def BouncyCircle(mass, coords, radius):
circle_moment = pymunk.moment_for_circle(mass, 0, radius)
circle_body = pymunk.Body(mass, circle_moment, pymunk.Body.STATIC)
circle_shape = pymunk.Circle(circle_body, radius)
circle_shape.elasticity = 2.0
circle_body.position= coords
space.add(circle_body, circle_shape)
def Segment(mass, PointA, PointB, thickness):
segment_moment = pymunk.moment_for_segment(mass, PointA, PointB, thickness)
segment_body = pymunk.Body(mass, segment_moment, pymunk.Body.STATIC)
segment_shape = pymunk.Segment(segment_body, PointA, PointB, thickness)
segment_shape.elasticity = 0.7
segment_body.position = 0,0
space.add(segment_body, segment_shape)
Ball(0.1, 15, (640, 550)) #Falling Ball
BouncyCircle(1, (650,100), 40) #Ball
Segment(10, (105,55), (1195,55), 5) #Border
Segment(10, (100, 50), (100,680), 5)
Segment(10, (105, 675), (1195,675), 5)
Segment(10, (1200, 50), (1200, 680), 5)
@win.event
def on_draw():
win.clear()
space.debug_draw(options)
def update(dt):
space.step(dt)
if __name__ == "__main__":
pyglet.clock.schedule_interval(update, 1/60)
pyglet.app.run()
当它非常慢时也会发生这种情况,有时它会反弹 10 次直到它停止工作。有人知道如何解决吗?