Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

paddle and breakout and direct x

This topic is 3051 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

well I am still working on animating my paddle in my game, it skips around when I press the keys, I want it to move smoothly back and forth. Here is my code.
    
if (KEY_DOWN(VK_LEFT))
	{			

		d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(255,255,255));
		//copy the surface to the backbuffer
        RECT rectw;
        rectw.left = m--;
        rectw.right = n--;
		rectw.top = 550;
        rectw.bottom = 600;

		d3ddev->StretchRect(surface, NULL, backbuffer, &rectw, D3DTEXF_NONE);

		d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(0,0,0));
		//copy the surface to the backbuffer
        RECT rectx;
        rectx.left = o--;
        rectx.right = p--;
		rectx.top = 550;
        rectx.bottom = 600;

		d3ddev->StretchRect(surface, NULL, backbuffer, &rectx, D3DTEXF_NONE);
	}	

    if (KEY_DOWN(VK_RIGHT))
	{			

		d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(0,0,0));
		//copy the surface to the backbuffer
        RECT recty;
        recty.left = m++;
        recty.right = n++;
		recty.top = 550;
        recty.bottom = 600;

		d3ddev->StretchRect(surface, NULL, backbuffer, &recty, D3DTEXF_NONE);

		d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(255,255,255));
		//copy the surface to the backbuffer
        RECT rectz;
        rectz.left = o++;
        rectz.right = p++;
		rectz.top = 550;
        rectz.bottom = 600;

		d3ddev->StretchRect(surface, NULL, backbuffer, &rectz, D3DTEXF_NONE);
	}	

Share this post


Link to post
Share on other sites
Advertisement
My first question for you is what are the values of the variables m, n, o, p?

You're skipping around because each time you check to see that the key is pressed you are adding one to m, n, o, or p and then adding that new value to the rect.left and rect.right.

For example, if m = 1 initially, each time you loop and the key is down m = m + 1.
I think what you are wanting would be more like this:



velocity = 1;
if (KEY_DOWN(VK_LEFT))
{

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(255,255,255));
//copy the surface to the backbuffer
RECT rectw;
rectw.left += velocity;
rectw.right += velocity;
rectw.top = 550;
rectw.bottom = 600;

d3ddev->StretchRect(surface, NULL, backbuffer, &rectw, D3DTEXF_NONE);

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(0,0,0));
//copy the surface to the backbuffer
RECT rectx;
rectx.left -= velocity;
rectx.right -= velocity;
rectx.top = 550;
rectx.bottom = 600;

d3ddev->StretchRect(surface, NULL, backbuffer, &rectx, D3DTEXF_NONE);
}



This will move the paddle by one each time the key is pressed. Using one variable for it allows you to go in and change the speed that it moves by setting one value.

Also, if you aren't familiar 'rectx.left -= velocity;' is the same as saying 'rectx.left = rectx.left - velocity;'

Share this post


Link to post
Share on other sites
In which line of your code do you get that error?

The code scone posted shouldn't result in that error. Perhaps if you post your code and indicate the line where you get the error we can help out.

Share this post


Link to post
Share on other sites
Oops! Yeah, scone forgot to put something in to initialize the RECTs.

It appears the code you posted should work, provided you render before you process another keystroke. Do you do that?

Share this post


Link to post
Share on other sites
Sorry, I may not have been clear. Scone's code wasn't properly setup. I'm not sure what he had in mind as he doesn't initialize the RECTs before they're used. Perhaps if he's still following this thread he can clarify.

In the meanwhile, the code you originally posted should work, provided you render after each keystroke. Do you do that?

Share this post


Link to post
Share on other sites
Your code is alright. Are you running vsync'ed or just as many frames as possible?

It may as well be that your paddle is moving too fast. If you call that update routine for every displayed frame 100 frames would move the paddle 100 pixels.

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!