Trying to figure out how to get an object moving within the bounds of a window....

This is a discussion on Trying to figure out how to get an object moving within the bounds of a window.... within the C++ Programming forums, part of the General Programming Boards category; So I now have a bitmap moving at a "slow"pass from left to right in a window ap. Everytime it ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    Trying to figure out how to get an object moving within the bounds of a window....

    So I now have a bitmap moving at a "slow"pass from left to right in a window ap. Everytime it hits the right boundry it gets moved back to 0(x-plane)and down 50(y-plane) pixels. Hear is that code for that part
    Code:
        if (rect.right > 0) 
        {
          	gPosX = gPosX + 1;
    		if ( gPosX >= rect.right )
    		{
    			gPosY = gPosY + 50;
    			gPosX = 0;
    		}
    		if ( gPosY >  rect.bottom - 60 )
    		{
    			gPosY = 0;
    			gPosX=0;
    		}
            DrawBitmap(global_hdc, "c.bmp", gPosX, gPosY); 
        }
    I acheived it moving at a "slow" pace by making a call to the Sleep() function in my main loop. Beautiful! Anyways I am now that I actually have the bitmap moving in a fashion I want it to, I need to take it one step further. I need to get the bitmap to move in random directions with in the bounds of the form. How would you suggest to do this? I tried a few diffrent if statments out but it seems one defeats the other as the build up.

    Any suggestions would be great....

    Thanks

    Chad

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    This may help you out some it is an allegro based pong game it is well labeled so you should be able to find the part you need in it just use that and you should be up and running in no time. http://alleg.sourceforge.net/docs/ho...g_game.en.html

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I acheived it moving at a "slow" pace by making a call to the Sleep() function in my main loop.
    That might not work properly on slower computers.

    I tried a few [different] if [statements] out but it seems one defeats the other
    Have a variable like x_direction which only changes when the object hits the side. Or something like that. You can't just do this, as you've found out:
    Code:
    if(rand() % 2) move_left();
    else move_right();
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Thanks for the help but I think I figured it out a few days ago....Hear is what I ended up doing..
    Code:
        if (rect.right > 0) 
        {
     		gPosX = gPosX + xDirection;
    		gPosY = gPosY + yDirection;
    			
    			//Handles movement in the X-axis
    			if (gPosX > rect.right - 85)
    			{
    				PlaySound("crashA.wav",0,SND_ASYNC|SND_FILENAME);
    				xDirection = -1;
    			}
    			else if (gPosX < rect.left)
    			{
    				PlaySound("crashB.wav",0,SND_ASYNC|SND_FILENAME);
    				xDirection = 1;
    			}
    
    			//Handles movement in the Y-axis
    			if (gPosY > rect.bottom - 140)
    			{
    				PlaySound("crashA.wav",0,SND_ASYNC|SND_FILENAME);
    				yDirection = -1;
    			}
    			else if (gPosY < rect.top)
    			{
    				PlaySound("crashB.wav",0,SND_ASYNC|SND_FILENAME);
    				yDirection = 1;
    			}
    
            DrawBitmap(global_hdc, "c.bmp", gPosX, gPosY); 
        }

    By the way why won't sleep() work on slower computers...

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    By the way why won't sleep() work on slower computers...
    I already said that. You'll want to use a function like SDL_GetTicks() instead.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. Deconstraining Cursor While Moving Window
    By SMurf in forum Windows Programming
    Replies: 2
    Last Post: 08-26-2006, 07:17 AM
  3. Making a script language?
    By Blackroot in forum Game Programming
    Replies: 10
    Last Post: 02-16-2006, 02:22 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 05:33 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21