How to stop a bitmap from moving when it collides with the edges of the screen?

  2d-games, bitmap, c++

<I am trying to create a function that checks to see if a bitmap has collided with a game screen edge. If the bitmap collides with one of the four edges then I have to stop the ship from moving in that direction. I’ve coded the function up but the spaceship(bitmap) does not stop when it reaches the edge of the of the game screen. I’d appreciate any help and tips. The width and height of the bitmap are 120(width) and 95(height)>

`while(ProgramIsRunning())
    {
        xPos = rectSpaceShip.x;
        yPos = rectSpaceShip.y;
        checkBorder(xPos, yPos, bitMapWidth, bitMapHeight);
        rectSpaceShip.x = xPos;
        rectSpaceShip.y = yPos;
    }//end game loop
void checkBorder(int& xPosition, int& yPosition, int width, int height)
{
    int x = xPosition;
    int y = yPosition;

    //1.
    x += 120;
    if(x = 800 - 120)
    {
        x = 800 - 120;
    }

    //2.
    if(x < 0)
    {
      x = 0;
    }

    //3.
    y += 95;
    if(y < 0)
    {
        y = 600 - 95;
    }
    if(y < 0)
    {
      y = 600;
    }
}`

Source: Windows Questions C++

LEAVE A COMMENT