Sign in to follow this  

making my paddle move

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

im tring to make a simple, game that bats a ball . . you know the type im sure ive drawn my paddle and am trying to make it move and used this code . .
void cPaddle::Draw()
{
	D3DXVECTOR2 vTrans;
	D3DXVECTOR2 vScale;

	vTrans.x = 0.0f + ( GetXPos() );
	vTrans.y = 0.0f + ( GetYPos() );

	vScale.x = 0.5f;
	vScale.y = 1.0f;

	m_Graphics->GetSprite()->Draw(m_PaddleTex.GetTexture(), NULL, &vScale, NULL, 
									NULL, &vTrans, D3DCOLOR_RGBA(255,255,255,255));
}

void cPaddle::Move()
{
	if(KEYDOWN(VK_DELETE))
	{
		float a = GetXPos();
		a += 0.05f;
	}
}


how would i get this drawn image to move? im sure my move doesnt work as it doesnt know what its moving???

Share this post


Link to post
Share on other sites
Your not storing a in anything, so the increment is lost.

what you need is an x variable stored in the CPaddle class and the increment that...

void cPaddle::Move()
{
if(KEYDOWN(VK_DELETE))
{
this->x += 0.05f;
}
}

Share this post


Link to post
Share on other sites
EDIT: And AGAIN I seem to be just that bit too slow today... [rolleyes]

Seems to me like a fairly simple syntax/semantics error in your code [oh]

if(KEYDOWN(VK_DELETE))
{
float a = GetXPos();
a += 0.05f;
}




Unless 'a' were a pointer of some kind, that operation will not actually alter the X position at all... the new/temporary value of 'a' will be discarded at the scope {}...

Maybe you need a SetXPos() function?

if(KEYDOWN(VK_DELETE))
{
SetXPos( GetXPos() + 0.05f );
}




hth
JAck

Share this post


Link to post
Share on other sites
How are you storing your coordinates? (The ones you get with GetXPos() and GetYPos() )

Moving your paddle would be as simple as adjusting those coordinates. Like this:

void cPaddle::MoveLeft()
{
XPos -= 0.05f;
}

void cPaddle::MoveRight()
{
XPos += 0.05f;
}

then in your game loop do something like this:

if(KEYDOWN(left_key_code_here))
{
paddle.MoveLeft();
}

and so on...


edit: WAY too slow, man sorry...

Share this post


Link to post
Share on other sites
so my follow up question

if i create an image of .png and i want all of the white in the image to be transparent . . .how do i do this?

thanks people

Share this post


Link to post
Share on other sites
Use the png's alpha channel. "Erase" the white so it becomes transparent (after enabling the alpha channel).

Share this post


Link to post
Share on other sites
can u not do what i said thois . .specify a colour to alpha? . .if so say i loaded a png, what would the alpha specific code look like?

Share this post


Link to post
Share on other sites
ok so most of this is sorted but now i need pong ball code, i dont want crazy crap just rebounds at 45 degress. is there anywhere i can browse good easy to read code for this?

Share this post


Link to post
Share on other sites
Ok, here's some pseudo code:

if(ball hit paddle)
{
YVel *= -1;
}

It will reverse the ball's velocity so it goes upward. If you hit side walls, you do this:

XVel *= -1;

If you hit top side, you do this:

YVel *= -1;

Just set the ball velocity to 45 degree movement in beginning, and those will take care of your physics.

So, example, XVel = 5, YVel = -5 will cause the ball to go up and right at exactly 45 degrees.

Hope this helps

Share this post


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