【MFC】利用双缓冲和随机函数rand()实现蒲公英飞舞
原始日期:2014-05-29 22:44
这几天有些懒,几乎没怎么学MFC了,好容易有个题目:用双缓冲实现蒲公英飞舞,想来想去也没想到好方法,索性动手开始 写了 ,这一写,得,出来了,呵呵,无意中产生 了一个新算法,不知道网上有没有,反正我是没在百度搜到
//change为坐标变换参数
for (int i=1;i<=450;i++)
{
x=10+rand()%100;//随机产生“变换横坐标”,范围10~100
y=1+rand()%100;//随机产生“变换纵坐标”,范围1~100
//这里如果横纵坐标范围一样,那么产生的蒲公英种子会相对静止的整体移动
c=0+rand()%5;//change乘以随机数c,产生新的随机坐标变换参数“change*c”
d=-1+rand()%10;
//使用MoveTo、LienTo直接画出单个蒲公英种子
CPen NewPen,*pOldPen;
NewPen.CreatePen(PS_SOLID,1,RGB(200,200,200));
pOldPen=pDC->SelectObject(&NewPen);
pDC->MoveTo(-752+x+change*d,-288+y+change*c);
pDC->LineTo(-750+x+change*d,-300+y+change*c);
pDC->LineTo(-758+x+change*d,-290+y+change*c);
pDC->MoveTo(-740+x+change*d,-290+y+change*c);
pDC->LineTo(-750+x+change*d,-300+y+change*c);
pDC->LineTo(-746+x+change*d,-288+y+change*c);
NewPen.DeleteObject();
NewPen.CreatePen(PS_SOLID,2,RGB(100,100,100));
pOldPen=pDC->SelectObject(&NewPen);
pDC->MoveTo(-750+x+change*d,-300+y+change*c);
pDC->LineTo(-753+x+change*d,-306+y+change*c);
}