Jump to content
  • Advertisement
Sign in to follow this  
SKATIN_HARD

limit sprite movement within an app using SDL/C++

This topic is 4439 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have define my app as so: #define SCREEN_WIDTH 640 #define SCREEN_HEIGHT 480 int xpos = 0, ypos = 0; Now I have some cpp code:
 void CheckHeight()
  {

	if (ypos > SCREEN_HEIGHT)
	
      {

         ypos -= 2; // need to subtract something to take ship back?

      }
   }
   
          void CheckWidth()
   {
          if (xpos > SCREEN_WIDTH)
          
          {
                  xpos -= 2;  // subtract to take ship back?
          }
   }

My questions is: how can I have it so that the ship cannot go out of bounds?? I don't want the ship to be able to pass out of the defined screen area and go out of the players sight. That would be a problem. Am I going about doing this the wrong way? I'm using SDL on this project and c++. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
if (xpos > SCREEN_WIDTH)
xpos = SCREEN_WIDTH - 1;

and change y accordingly.
If you want, you can also subtract the entire sprite's width from the xpos, so that none of it exits the window.

Share this post


Link to post
Share on other sites
Thanks but I can still go off the screen. I also have the variables:

int bgX = 0, bgY = 0; // background offset


Would I want to set those to the value of SCREEN_WIDTH - 1??


This might help to further understand. A block of my code from main()

source lang="cpp"]
if(!GamePaused)
{
bgX -= 3; // Was originally bgX -= 2;

if(bgX <= -background->w)
{
bgX = 0; //Reset the offset
}

apply_surface(bgX, bgY, background, screen);
apply_surface(bgX + background->w, bgY, background, screen);

if (keys[SDLK_UP]) { ypos -= 3; }
if (keys[SDLK_DOWN]) { ypos += 3; }
if (keys[SDLK_LEFT]) { xpos -= 3; }
if (keys[SDLK_RIGHT]) { xpos += 3; }


}
[/source]
Maybe that can help some. Thanks again.

Share this post


Link to post
Share on other sites
I would code it the following way. nX is the object's position on the X axis, nY is the object's position on the Y axis. All other variables should be self explainitory.
void CheckBounds(int& nX, int& nY, int& OBJECT_WIDTH, int& OBJECT_HEIGHT)
{
if(nX < 0)
nX = 0;
else if(nX + OBJECT_WIDTH > SCREEN_WIDTH)
nX = SCREEN_WIDTH - OBJECT_WIDTH;

if(nY < 0)
nY = 0;
else if(nY + OBJECT_HEIGHT > SCREEN_HEIGHT)
nY = SCREEN_HEIGHT - OBJECT_HEIGHT;
}



Then your other code would look like this:
if(!GamePaused)
{
bgX -= 3; // Was originally bgX -= 2;

if(bgX <= -background->w)
bgX = 0; //Reset the offset

apply_surface(bgX, bgY, background, screen);
apply_surface(bgX + background->w, bgY, background, screen);

if (keys[SDLK_UP]) { ypos -= 3; }
if (keys[SDLK_DOWN]) { ypos += 3; }
if (keys[SDLK_LEFT]) { xpos -= 3; }
if (keys[SDLK_RIGHT]) { xpos += 3; }

CheckBounds(xpos, ypos, IMAGE_WIDTH, IMAGE_HEIGHT);
}

Share this post


Link to post
Share on other sites
Thanks for the tips. My code for calling the method is as follows:


CheckHeight();
CheckWidth();
DrawScene();


Should they be somewhere else instead of there above the DrawScene function?

Share this post


Link to post
Share on other sites
Sadly no, I have not figured it out yet. I can get the code to compile but once my sprite moves to the top of the screen or goes left or right off the screen it is out of sight. This really shouldn't be happening.

I declared 2 new variables ybalance and xbalance and when they are greater than either the screen width or height I subtract 2 from the x or y balance. I just don't want my spirte to go off the screen.

Share this post


Link to post
Share on other sites
I cover this in my motion tutorial.

What exactly don't you understand about this?:


//Move the dot left or right
x += xVel;

//If the dot went too far to the left or right
if( ( x < 0 ) || ( x + DOT_WIDTH > SCREEN_WIDTH ) )
{
//move back
x -= xVel;
}

//Move the dot up or down
y += yVel;

//If the dot went too far up or down
if( ( y < 0 ) || ( y + DOT_HEIGHT > SCREEN_HEIGHT ) )
{
//move back
y -= yVel;
}

Share this post


Link to post
Share on other sites
I'm not sure how a give my sprite a value. Like I can define ship height and width but when it comes to running the program the function doesn't seem to be working. It still allows everything to pass through.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!