用链表实现的屏幕飘雪程序
int luoxue(struct xhbiao *tou,struct xhbiao *wei)
{
struct xhbiao *p; //处理雪花的物理动作
p=tou;
for(;p!=NULL;)
{
p->xh.nexty=p->xh.y+p->xh.shudu; //下降
p->xh.nextx=p->xh.x+FENG; //(风向风速)
p=p->next;
}
return 0;
}
//雪堆更新到屏幕
int xuehuagdi(struct xhbiao *tou,struct xhbiao *wei)
{
HDC hdc=GetDC(NULL);
COLORREF color,color1;
int R,G,B,RGB;
struct xhbiao *p1; //显示雪堆链表的数据
p1=tou;
for(;p1!=NULL;)
{
SetPixel(hdc,p1->xh.x,p1->xh.y,p1->xh.oldcolor);
if((p1->xh.nexty<=MINY)&&(p1->xh.nextx>=0)&&(p1->xh.nextx<=MINX)) //超出屏幕边界则删除节点
{
color=GetPixel(hdc,p1->xh.nextx,p1->xh.nexty);
if(color!=snowcolor) //如果雪点下一个显示目标点与别的雪点重合则不处理等待维护函数删除此节点
{
color1=GetPixel(hdc,p1->xh.nextx,(p1->xh.nexty-1));
B=abs((color>>16)&0xff-(color1>>16)&0xff);
G=abs((color>>8)&0xff-(color1>>8)&0xff);
R=abs((color)&0xff-(color1)&0xff);
RGB=(R+G+B)/3; //根据对比度判断是否堆积雪花
if(RGB<50){
p1->xh.oldcolor=color;
SetPixel(hdc,p1->xh.nextx,p1->xh.nexty,snowcolor);
p1->xh.x=p1->xh.nextx;
p1->xh.y=p1->xh.nexty;
}else if(RGB>50){
SetPixel(hdc,p1->xh.nextx,(p1->xh.nexty-1),snowcolor1);
p1->xh.del=1;
}
}else if(color==snowcolor){
p1->xh.del=1;
}
} else if((p1->xh.nexty>MINY)||(p1->xh.nextx<0)||(p1->xh.nextx>MINX)){
p1->xh.del=1;
SetPixel(hdc,p1->xh.x,p1->xh.y,p1->xh.oldcolor);
}
p1=p1->next;
}
ReleaseDC(NULL,hdc);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
srand(time(NULL));
struct xhbiao *tou,*p,*wei; //tou为新建表的表头wei为表尾p为新节点的指针
tou=NULL;
wei=NULL;
FENG=0;
int i,j,k;
k=0;
////////////////////////////////////////////////////////////////////////////////
MINY=1100;
MINX=1300;
link(&tou,&wei);
for(i=0;i<=8;)
{
for(j=0;j<=k;j++)
{
linkCHA(&tou,&wei);
}
luoxue(tou,wei);
xuehuagdi(tou,wei);
linkWUI(&tou,&wei);
Sleep(30);
k=rand()%22;
}
return 0;
}
评论