//雪花飞舞
//要求设计雪花的数量、位置、颜色、速度等,并能够实现颜色转换、随风向变化而作调整,且飘落中距离近的雪花可黏合。
//注意系统的功能和性能
//此程序在Visual C++ 6.0环境下运行通过
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include<iostream.h>
const int SnowNumber=500; //雪点数量
struct SnowNode{
POINT postion; //雪点位置
int iColor; //先前的颜色
int iSpeed; //下落速度
int iMove; //下落距离
int iStick; //粘贴度
};
SnowNode SnowNodes[SnowNumber]; //雪点数组
int hTimer=0;
int CrWind=0;
int CrStep=0; //当前循环步数(用于限速)
int ScreenWidth=0; //屏幕宽度
int ScreenHeight=0; //屏幕高度
void GetScreenSize();
void CALLBACK TimerProc(HANDLE hWnd,UINT uMsg,UINT idEvent,DWORD dwTime);
void InitSnowNodes();
void MoveSnowNodes();
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
MSG msg; //标准Windows消息
LARGE_INTEGER Frequency; //高性能定时器频率
LARGE_INTEGER StartCt,EndCt;//高性能定时器计数
float ElapsedTime; //时间间隔
srand((unsigned)time(NULL));
GetScreenSize();
InitSnowNodes();
QueryPerformanceFrequency(&Frequency);
hTimer=SetTimer(0,0,rand()%5*500,(TIMERPROC)TimerProc);
if(hTimer==0){
MessageBox(0,TEXT("创建定时器失败"),TEXT("提示"),MB_OK|MB_ICONINFORMATION);
return -1;
}
RegisterHotKey(0,0,MOD_CONTROL,(int)'L');
while(1){
QueryPerformanceCounter(&StartCt); //执行运算前计数值
if(PeekMessage(&msg,0,0,0,1)){
switch(msg.message){
case WM_TIMER: //预设风向改变时间已到
TimerProc(0,0,0,0);
break;
case WM_HOTKEY:
KillTimer(0,hTimer); //删除随机风向定时器
UnregisterHotKey(0,0); //删除退出热键
InvalidateRect(0,NULL,true);
exit(1);
break;
case WM_DISPLAYCHANGE:
GetScreenSize(); //重新取屏幕的尺寸
InitSnowNodes(); //初始化雪点的数组
break;
}
}
MoveSnowNodes();
QueryPerformanceCounter(&EndCt); //执行运算后的计数值
ElapsedTime=(EndCt.QuadPart-StartCt.QuadPart)/Frequency.QuadPart;
if(ElapsedTime<0.0005){
Sleep(2);
}else if(ElapsedTime<0.0010){
Sleep(1);
}else if(ElapsedTime<0.0015){
Sleep(3);
}
}
//MessageBox(0,TEXT("消息"),TEXT("消息"),MB_OK|MB_ICONINFORMATION);
return 0;
}
void GetScreenSize(){
ScreenWidth=GetSystemMetrics(SM_CXSCREEN);
ScreenHeight=GetSystemMetrics(SM_CYSCREEN);
return;
}
void CALLBACK TimerProc(HANDLE hWnd,UINT uMsg,UINT idEvent,DWORD dwTime){
//MessageBox(0,TEXT("消息"),TEXT("消息"),MB_OK|MB_ICONINFORMATION);
srand((unsigned)time(NULL));
if(hTimer==0){
MessageBox(0,TEXT("创建定时器失败"),TEXT("提示"),MB_OK|MB_ICONINFORMATION);
return;
}
SetTimer(0,hTimer,((rand()%27+4)*500),(TIMERPROC)TimerProc); //重设下次风向改变时间
if(CrWind!=0){ //修改风向
CrWind=0;
}else{
CrWind=rand()%3-1;
}
return;
}
void InitSnowNodes(){
HDC hScreenDC=0;
int j=0;
hScreenDC=CreateDC("DISPLAY",NULL,NULL,NULL);
if(hScreenDC==NULL){
MessageBox(0,"获取屏幕DC失败!","信息",MB_OK|MB_ICONERROR);
return;
}
srand((unsigned)time(NULL));
for(j=0; j<SnowNumber; j++){
SnowNodes[j].postion.x=rand()%ScreenWidth;
SnowNodes[j].postion.y=rand()%ScreenHeight;
SnowNodes[j].iColor=GetPixel(hScreenDC,SnowNodes[j].postion.x,SnowNodes[j].postion.y);
SnowNodes[j].iSpeed=(rand()%5+1); //每次下落距离(1~5)
SnowNodes[j].iStick=(30-rand()%SnowNodes[j].iSpeed); //粘贴度(几次循环做一次粘贴度判断)
//cout<<SnowNodes[j].postion.x<<" Y:"<<SnowNodes[j].postion.y<<endl;
}
DeleteDC(hScreenDC);
}
void MoveSnowNodes(){
//MessageBox(0,TEXT("消息"),TEXT("消息"),MB_OK|MB_ICONINFORMATION);
HDC hScreenDC=0;
srand((unsigned)time(NULL));
int x=0,y=0,i=0;
hScreenDC=CreateDC("DISPLAY",NULL,NULL,NULL);
if(hScreenDC==NULL){
MessageBox(0,"获取屏幕DC失败!","信息",MB_OK|MB_ICONERROR);
return;
}
TextOut(hScreenDC,0,0,"输入您要显示的信息",0);
for(i=0; i<SnowNumber; i++){ //控制雪点下降速度
if((CrStep%SnowNodes[i].iSpeed)!=0){
continue;
}
if((GetPixel(hScreenDC,SnowNodes[i].postion.x,SnowNodes[i].postion.y))==0XFFFFFF){
//恢复上次被覆盖点
SetPixel(hScreenDC,SnowNodes[i].postion.x,SnowNodes[i].postion.y,SnowNodes[i].iColor);
}
x=SnowNodes[i].postion.x+rand()%3+CrWind; //根据风向作随机飘落
y=SnowNodes[i].postion.y+SnowNodes[i].iMove;
if(((CrStep%SnowNodes[i].iStick)==0) && (GetPixel(hScreenDC,x,y)!=(GetPixel(hScreenDC,x,y+1))) &&
(GetPixel(hScreenDC,x-1,y) != GetPixel(hScreenDC,x-1,y+1)) &&
(GetPixel(hScreenDC,x+1,y) != GetPixel(hScreenDC,x+1,y+1))){
if(GetPixel(hScreenDC,x,y-1)==GetPixel(hScreenDC,x,y-2)){ //稍稍调整坐标
y--;
}else{
if(GetPixel(hScreenDC,x,y-1)==GetPixel(hScreenDC,x,y-2)){
y++;
}
x+=CrWind;
}
SetPixel(hScreenDC,x,y,0XFFFFFF);
SetPixel(hScreenDC,x+1,y+1,0XFFFFFF);
SetPixel(hScreenDC,x-1,y+1,0XFFFFFF);
SnowNodes[i].postion.x=rand()%ScreenWidth; //重生雪点
SnowNodes[i].postion.y=rand()%10;
SnowNodes[i].iColor=GetPixel(hScreenDC,SnowNodes[i].postion.x,SnowNodes[i].postion.y);
}else{
if(x<0 || x>ScreenWidth || y>ScreenHeight){
SnowNodes[i].postion.x=(rand()%10);
SnowNodes[i].postion.y=(rand()%ScreenWidth);
SnowNodes[i].iColor=GetPixel(hScreenDC,SnowNodes[i].postion.x,SnowNodes[i].postion.y);
}else{ //保存颜色并绘制雪点
SnowNodes[i].iColor=GetPixel(hScreenDC,x,y);
SetPixel(hScreenDC,x,y,0XFFFFFF);
SnowNodes[i].postion.x=x;
SnowNodes[i].postion.y=y;
}
}
}
DeleteDC(hScreenDC);
CrStep++;
}
SnowFlying.rar_雪花
版权申诉
163 浏览量
2022-09-23
23:30:25
上传
评论
收藏 1.17MB RAR 举报
林当时
- 粉丝: 100
- 资源: 1万+
最新资源
- Screenshot_20240528_103010.jpg
- 基于Python的新能源承载力计算及界面设计源码 - HAINING-DG
- 基于Java的本科探索学习项目设计源码 - 本科探索
- 基于Javascript和Python的微商城项目设计源码 - MicroMall
- 基于Java的网上订餐系统设计源码 - online ordering system
- 基于Javascript的超级美眉网络资源管理应用模块设计源码
- 基于Typescript和PHP的编程知识储备库设计源码 - study-php
- Screenshot_2024-05-28-11-40-58-177_com.tencent.mm.jpg
- 基于Dart的Flutter小提琴调音器APP设计源码 - violinhelper
- 基于JavaScript和CSS的随寻订购网页设计源码 - web-order
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈