Sign in to follow this  
phil67rpg

paddle and breakout and direct x

Recommended Posts

phil67rpg    443
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
scone    100
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
Buckeye    10747
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
Buckeye    10747
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
Buckeye    10747
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
Endurion    5411
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
Buckeye    10747
What does your game loop look like?

For what you are doing, it should be something like:

CheckKeysForInput() { your original code }
RenderTheScene() { .. }
DelayBeforeNextFrame(some delta time);

The idea is that if VK_LEFT or VK_RIGHT is pressed, you make the one-pixel change to your paddle and immediately render to show the change before you check the key again.

Share this post


Link to post
Share on other sites
phil67rpg    443
well here is my code


void Game_Run(HWND hwnd)
{
//make sure the Direct3D device is valid
if (!d3ddev) return;

//start rendering
if (d3ddev->BeginScene())
{

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(255,0,0));

RECT rect2;
rect2.left = 0;
rect2.right = 200;
rect2.top = 0;
rect2.bottom = 50;

brick1=1;

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

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(0,255,0));

RECT rect3;
rect3.left = 200;
rect3.right = 400;
rect3.top = 0;
rect3.bottom = 50;

brick2=1;

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

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(0,0,255));

RECT rect4;
rect4.left = 400;
rect4.right = 600;
rect4.top = 0;
rect4.bottom = 50;

brick3=1;

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

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(255,0,0));

RECT rect5;
rect5.left = 600;
rect5.right = 800;
rect5.top = 0;
rect5.bottom = 50;

brick4=1;

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


d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(0,255,0));

RECT rect6;
rect6.left = 0;
rect6.right = 200;
rect6.top = 50;
rect6.bottom = 100;

brick5=1;

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

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(0,0,255));

RECT rect7;
rect7.left = 200;
rect7.right = 400;
rect7.top = 50;
rect7.bottom = 100;

brick6=1;

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

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(255,0,0));

RECT rect8;
rect8.left = 400;
rect8.right = 600;
rect8.top = 50;
rect8.bottom = 100;

brick7=1;

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

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(0,255,0));

RECT rect9;
rect9.left = 600;
rect9.right = 800;
rect9.top = 50;
rect9.bottom = 100;

brick8=1;

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

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(0,0,255));

RECT rect10;
rect10.left = 0;
rect10.right = 200;
rect10.top = 100;
rect10.bottom = 150;

brick9=1;

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

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(255,0,0));

RECT rect11;
rect11.left = 200;
rect11.right = 400;
rect11.top = 100;
rect11.bottom = 150;

brick10=1;

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

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(0,255,0));

RECT rect12;
rect12.left = 400;
rect12.right = 600;
rect12.top = 100;
rect12.bottom = 150;

brick11=1;

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

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(0,0,255));

RECT rect13;
rect13.left = 600;
rect13.right = 800;
rect13.top = 100;
rect13.bottom = 150;

brick12=1;

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

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(0,0,0));
//copy the surface to the backbuffer
RECT recta;
recta.left = x3+=20;
recta.right = x4+=20;
recta.top = y1-=20;
recta.bottom = y2-=20;
Sleep(50);
d3ddev->StretchRect(surface, NULL, backbuffer, &recta, D3DTEXF_NONE);

d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(255,255,255));
//copy the surface to the backbuffer
RECT rectb;
rectb.left = x1+=20;
rectb.right = x2+=20;
rectb.top = y3-=20;
rectb.bottom = y4-=20;
Sleep(50);
d3ddev->StretchRect(surface, NULL, backbuffer, &rectb, D3DTEXF_NONE);



//stop rendering
d3ddev->EndScene();

//display the back buffer on the screen
d3ddev->Present(NULL, NULL, NULL, NULL);
}

//check for escape key (to exit program)
if (KEY_DOWN(VK_ESCAPE))
PostMessage(hwnd, WM_DESTROY, 0, 0);
int 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 = rectw.left + 1;
rectw.right = rectw.right + 1;
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 = rectx.left - 1;
rectx.right = rectx.right - 1;
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+=5;
recty.right = n+=5;
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+=5;
rectz.right = p+=5;
rectz.top = 550;
rectz.bottom = 600;

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

/**
** Game shutdown function
**/

void Game_End(HWND hwnd)

Share this post


Link to post
Share on other sites
Buckeye    10747
Looks like you may have a couple of problems.

First, use your originally posted code! It should work - except that you don't delay before rendering.

What's probably happening is you don't have any delay between rendering frames.

To see if that's the problem, after you change back to your original code, try putting a Sleep(100) statement just before the line if( KEYDOWN(VK_LEFT) ) ... That will slow down the rendering loop a little so you can, perhaps, see what's going on better.

Also, you really should update the paddles between BeginScene and EndScene. You could even put the if(KEYDOWN(VK_LEFT)) and if(KEYDOWN(VK_RIGHT)) inside BeginScene/EndScene.

Share this post


Link to post
Share on other sites
Buckeye    10747
If you have more questions, you'll have to ask them. "I am stuck" isn't much information to go on.

What's the problem? What have you done to figure out the problem?

Share this post


Link to post
Share on other sites
Endurion    5411
One possible change:

Calculate the elapsed time between frames. Use floating point coordinates (cast to int when rendering). During the update don't add full pixels, but use a speed value multiplied with the elapsed time. This way you can have smoother movement.


Pseudocode:

float ElapsedTime = CalculateDeltaSinceLastFrame();

if ( KEY_DOWN( VK_LEFT ) )
{
m -= 20.0f * ElapsedTime;
rectw.left = (int)m;
}
if ( KEY_DOWN( VK_RIGHT ) )
{
m += 20.0f * ElapsedTime;
rectw.left = (int)m;
}

Share this post


Link to post
Share on other sites
goodgod    100
Sorry, I may not have been clear. Scone's code wasn't properly setup.he doesn't initialize the RECTs before they're used.
http://www.01com.com/

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this