用鼠标射击子弹

shoot bullet with mouse

    double bullet::distance(int x1, int y1, int x2, int y2)
{
    return sqrt( pow( x2 - x1, 2 ) + pow( y2 - y1, 2 ) );
}

距离函数^

speedX.push_back((x - targetx) / distance(x, y, targetx, targety)*7);//x player pos, targetx mouse pos
speedY.push_back((y - targety) / distance(x, y, targetx, targety)*7);

计算每8ms变化多少像素^

sbullet[i].setX(sbullet[i].getX() - (int)round(speedX[i]));
sbullet[i].setY(sbullet[i].getY() - (int)round(speedY[i]));

实际移动^

所以这是我的子弹与鼠标一起移动,但它并没有完全对准鼠标。

如何让它更准确?

一旦你到达鼠标,你需要放慢速度。在您的代码中,您总是朝目标方向移动 7 个像素。如果你距离小于 7 个像素,你就过头了。

您可以添加一条语句,如果距离小于 7,则将新位置设置为目标。