提问者:小点点

循环Pong Javascript冲突问题


我正在尝试用JavaScript创建一个循环乒乓游戏。 圆形乒乓球是指最后触球的球员在球出界时将得到一分。 球当前从大圆圈容器上反弹。 我想让球从球拍上弹开,就像乒乓球一样。 有人知道我怎么解决这个问题吗?

function collide(shape, ball)
{
if(
    ball.pos.x >= (shape.pos.x + shape.size.x)
    && ball.pos.y >= (shape.pos.y + shape.size.y)
  ){
      ball.vel.x *= -.38 * ball.vel.x;
      ball.vel.y *= -.38 * ball.vel.y;
  }
}

这里是指向我的源代码的链接:https://jsfiddle.net/chernovd2411/habfcj3o/

提前谢谢你,阿特拉兹


共1个答案

匿名用户

这管用吗? 你需要检查球是否在桨够得着的范围内。

null

function collide(shape, ball)
{
    if (
        ball.pos.x > (shape.pos.x - shape.size.x/2)
     && ball.pos.x < (shape.pos.x + shape.size.x/2)
     && ball.pos.y > (shape.pos.y - shape.size.y/2)
     && ball.pos.y < (shape.pos.y + shape.size.y/2)
    )
    {
        ball.vel.x *= -.38 * ball.vel.x;
        ball.vel.y *= -.38 * ball.vel.y;
    }
}