模拟小球匀速运动

如何自动模拟小球匀速运动,在碰到边界的时候,无能量损失继续运动。

背景和小球加载处理完毕。

小球运动坐标。

void MyPaint(HDC hdc)
{
 SelectObject(bufdc,bg);
 BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);

 SelectObject(bufdc,ball);
 BitBlt(mdc,x,y,60,91,bufdc,60,0,SRCAND);
 BitBlt(mdc,x,y,60,91,bufdc,0,0,SRCPAINT);

 BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);

 x+=vx;
 if (x<=0)
 {
  x=0;
  vx=-vx;
 }
 else if (x>=rect.right-60)
 {
  x=rect.right-60;
  vx=-vx;
 }
 y+=vy;
 if (y<=0)
 {
  y=0;
  vy=-vy;
 }
 else if (y>=rect.bottom-91)
 {
  y=rect.bottom-91;
  vy=-vy;
 }

 tPre=GetTickCount();
}

 

首先想到是在主消息循环里面加

while (GetMessage(&msg, NULL, 0, 0))
 {
  if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
   while(true)
   {
    MyPaint(hdc);
    Sleep(1000);
   }
  }
 }

效果很不明显。

posted @ 2013-08-26 11:15  露水上的青蛙  阅读(278)  评论(0编辑  收藏  举报