Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am using pymunk for my pinball game.
I am using four circle shapes, three as a bumper and one as a ball.
I need to change color of the shape that has collided with the ball.
bumper code :
for p in [(230, 100), (370, 100),(300,140)]:
body = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
body.position = p
shape2 = pymunk.Circle(body, 20)
shape2.elasticity = 1.5
shape2.collision_type = 3
shape2.color = (31, 163, 5, 255)
space.add(body, shape2)
ball code :
global ballbody,shape1
mass = 1
radius = 14
inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
ballbody = pymunk.Body(mass, inertia)
ballbody.position = 500,460
shape1 = pymunk.Circle(ballbody, radius, (0, 0))
shape1.elasticity = 0.96
shape1.collision_type = 0
space.add(ballbody, shape1)
balls.append(shape1)
What I have tried is to change color of the shape on collision by adding a collision handler.
def bounceOnBumpers(space, arbiter,dummy):
shape2.color = (0, 255, 0, 255)
h = space.add_collision_handler(COLLTYPE_BALL, COLLTYPE_GOAL)
h.begin = bounceOnBumpers
But it doesn't work, Only one bumper's color is changed.
And is there anyway to get names of the shapes that are collided ?
The second argument in the collision handler callback is the arbiter. The pymunk.Arbiter
object encapsulates a pair of colliding shapes. With shapes
property you can get the shapes in the order that they were defined in the collision handler:
def bounceOnBumpers(space, arbiter, dummy):
shapes = arbiter.shapes
shapes[0].color = (0, 255, 0, 255)
shapes[1].color = (0, 255, 0, 255)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.