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

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

小球运动坐标。

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);
   }
  }
 }

效果很不明显。

相关文章:

  • 2022-02-18
  • 2022-12-23
  • 2021-08-09
  • 2021-10-17
  • 2022-01-02
  • 2021-08-08
  • 2022-01-26
  • 2022-12-23
猜你喜欢
  • 2021-05-05
  • 2021-05-20
  • 2022-12-23
  • 2021-12-02
  • 2021-10-12
  • 2021-10-04
  • 2022-12-23
相关资源
相似解决方案