How do I create collision detections for my bouncing balls?

后端 未结 7 1232
星月不相逢
星月不相逢 2021-02-04 18:13

I have coded an animation (in python) for three beach balls to bounce around a screen. I now wish to have them all collide and be able to bounce off each other. I would really a

7条回答
  •  情歌与酒
    2021-02-04 18:25

    I made a python collision detection if statement, here it is:

    if beach ball 1 x < beach ball 2 x + beach ball 1 width and beach ball 1 x + beach ball 2 width > beach ball 2 x and beach ball 1 y < beach ball 2 y + beach ball 1 height and beach ball 2 height + beach ball 1 y > beach ball 2 y:
        #put needed code here
    

    In your case, with 3 bouncing balls, you will have to make 2 if statements of this format for each ball to make sure that the collision detection is flawless. I hope this helps.

提交回复
热议问题