1 Attachment(s)
[MFC] How to make a ball bounce off from an object?
How would i make the ball bounce off from the object (bitmap) in MFC? (look at the picture below)
How would i set the boundaries and stuff?
Does anyone have the code?
I really really need help....i need to complete my project soon. :confused: :eek:
just in case you want to know, the bitmap size is 40 x 40.
don't know if it matters though.
and remember, i want the ball to bounce off from all four sides of the bitmap, and it would just repeat this process over and over.
1 Attachment(s)
bouncing ball project file
The project file contain the program of bouncing ball. This require DirectX SDK to compile. But you can run the executable inside the release folder. And i written this program just for fun :D
The program segment you may interest is
Code:
void CBallDlg::OnTimer(UINT nIDEvent)
{
if(nIDEvent==1){
if(curX+circleWidth>=windowRect.right){
offsetx=-offsetx;
m_pEdgeSound->Play();
}
if(curX<=0){
offsetx=-offsetx;
m_pEdgeSound->Play();
}
if(curY+circleWidth>=windowRect.bottom){
offsety=-offsety;
m_pEdgeSound->Play();
}
if(curY<=0){
offsety=-offsety;
m_pEdgeSound->Play();
}
if(curY+circleWidth==curBarRect.top||curY==curBarRect.bottom){
if(curX>=curBarRect.left&&curX<=curBarRect.right){
offsety=-offsety;
m_pBarSound->Play();
}
}
curX+=offsetx;
curY+=offsety;
//curX=rand()%windowRect.right;
//curY=rand()%windowRect.bottom;
m_pMemDC->FillSolidRect(&windowRect,RGB(0,0,0));
m_pMemDC->Ellipse(curX,curY,curX+circleWidth,curY+circleWidth);
m_pMemDC->FillSolidRect(&curBarRect,RGB(0,255,0));
sprintf(mess,"X: %d\nY: %d",curX,curY);
//m_status.SetWindowText("X:\nY:");
m_status.SetWindowText(mess);
Invalidate(NULL);
}
CDialog::OnTimer(nIDEvent);
}
Re: bouncing ball project file
Quote:
Originally posted by patricksky
The project file contain the program of bouncing ball. This require DirectX SDK to compile. But you can run the executable inside the release folder. And i written this program just for fun :D
The program segment you may interest is
Code:
void CBallDlg::OnTimer(UINT nIDEvent)
{
if(nIDEvent==1){
if(curX+circleWidth>=windowRect.right){
offsetx=-offsetx;
m_pEdgeSound->Play();
}
if(curX<=0){
offsetx=-offsetx;
m_pEdgeSound->Play();
}
if(curY+circleWidth>=windowRect.bottom){
offsety=-offsety;
m_pEdgeSound->Play();
}
if(curY<=0){
offsety=-offsety;
m_pEdgeSound->Play();
}
if(curY+circleWidth==curBarRect.top||curY==curBarRect.bottom){
if(curX>=curBarRect.left&&curX<=curBarRect.right){
offsety=-offsety;
m_pBarSound->Play();
}
}
curX+=offsetx;
curY+=offsety;
//curX=rand()%windowRect.right;
//curY=rand()%windowRect.bottom;
m_pMemDC->FillSolidRect(&windowRect,RGB(0,0,0));
m_pMemDC->Ellipse(curX,curY,curX+circleWidth,curY+circleWidth);
m_pMemDC->FillSolidRect(&curBarRect,RGB(0,255,0));
sprintf(mess,"X: %d\nY: %d",curX,curY);
//m_status.SetWindowText("X:\nY:");
m_status.SetWindowText(mess);
Invalidate(NULL);
}
CDialog::OnTimer(nIDEvent);
}
oh thank you for the code....i'll try it.
i am from hong kong too wor. :D