求一个C语言的数字雨代码~来者不拒~
打算实现个数字雨效果~不过上网搜到的都是API或者MFC编程的~感觉这个工程比较大~为了弄这个感觉要弄到炸天了~有没有谁能弄一个参考样板出来~C语言~最好用图形库~来者不拒啊~
回复 2楼 烟雨晨曦
终于简单用数组完成了规定数量的数字雨~等下我试试改用链表提高效率~谢啦~真的就几百行~~~~
程序代码:
void Del_Clum(PClum* p)
{
PClum pt=(*p)->prior;
pt->next=(*p)->next;
if ((*p)->next!=NULL)
(*p)->next->prior=pt;
Free_Node((void** )p);
}
void Link_Clum(PClum p1,PClum p2)
{
if (p1==NULL||p2==NULL)
return ;
p1->next=p2;
p2->prior=p1;
}
void Show_Init_Map() //初始特效设计
{
COLORREF re_linecolor=getlinecolor();
COLORREF bgcolor=TRANSPARENT;
Game_Start=false;
int widht=(BOX2_WIDTH)/(MAP_SIZE); //Main盒子的宽
int height=(BOX2_HEIGHT)/(MAP_SIZE); //Main盒子的高
int i=0;
int j=0;
int t=0;
int show_Map[(BOX2_WIDTH)/(MAP_SIZE)*(BOX2_HEIGHT)/(MAP_SIZE)]={0};
int temp_Map[(BOX2_WIDTH)/(MAP_SIZE)*(BOX2_HEIGHT)/(MAP_SIZE)]={0};
int show_Map2[(BOX2_WIDTH)/(MAP_SIZE)*(BOX2_HEIGHT)/(MAP_SIZE)]={0}; //记录数据纵坐标
Map_Data show[(BOX2_WIDTH)/(MAP_SIZE)*(BOX2_HEIGHT)/(MAP_SIZE)]={0};
PClum head=NULL; //头节点
PClum end=NULL; //尾节点
for (i=0;i<widht*height;++i)
temp_Map[i]=i;
for (i=0,t=widht*height;i<widht*height;i++,t--) //生成方块数量的随机数
{
int s=rand()%t;
show_Map[i]=temp_Map[s];
show_Map[i]%=widht;
for (j=s;j<t-1;++j)
temp_Map[j]=temp_Map[j+1];
}
setlinecolor(bgcolor);
Creat_Node((void** )&head,sizeof(Clum)); //创建头节点
Creat_Node((void** )&end,sizeof(Clum)); //创建尾节点
Link_Clum(head,end);
for (t=0;t<widht*height;++t) //初始化方块数据
{
show[t].color=Map[widht-show_Map2[show_Map[t]]++-1][show_Map[t]].color; //获取初始化方块颜色
show[t].x=show_Map[t]; //获取方块下落的横坐标
}
memset(show_Map2,0,sizeof(show_Map2)); //重置数组
for (t=0;t<widht*height;++t)
{
PClum pclum=end->prior;
PClum pt=NULL;
PClum ps=head;
Creat_Node((void** )&pt,sizeof(Clum));
pt->map_data=show[t];
Link_Clum(pclum,pt); //链接两个节点
Link_Clum(pt,end);
BeginBatchDraw();
while ((ps=ps->next)&&(ps!=end))
{
int left=Box_Main.point.left+ps->map_data.x*MAP_SIZE; //获取方块的顶点信息
int top=Box_Main.point.top+ps->map_data.y*MAP_SIZE;
int right=left+MAP_SIZE;
int bottom=top+MAP_SIZE;
if (ps->map_data.y>0)
{
setfillcolor(bgcolor);
fillrectangle(left,top-MAP_SIZE,right,bottom-MAP_SIZE);
}
setfillcolor(ps->map_data.color); //设置方块颜色
fillrectangle(left,top,right,bottom); //填充方块颜色
Draw_Line_Rectangle //绘制边框信息
(
Box_Main.point.left,
Box_Main.point.top,
Box_Main.point.left+Box_Main.width,
Box_Main.point.top+Box_Main.height,
Box_Main.color.line_color,
bgcolor
);
if (ps->map_data.y<widht-show_Map2[ps->map_data.x]-1)
++ps->map_data.y;
else
{
show_Map2[ps->map_data.x]++;
PClum pt=ps->prior;
Del_Clum(&ps);
ps=pt;
}
}
EndBatchDraw(); //结束批量绘图
Sleep(FALL_TIME); //缓冲时间
}
Free_Node((void** )&head); //释放头节点
Free_Node((void** )&end); //释放尾节点
Data.timer.TIME_START=clock(); //游戏时间重新开始
FlushMouseMsgBuffer(); //清理鼠标缓冲区
}
[此贴子已经被作者于2017-5-16 03:52编辑过]

程序代码:#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#define mm 200 //单位时间数字雨最大容量
#define u 0.33 //事件发生概率
#define sp 100 //单位时间
typedef struct datrain
{
char a; //数字雨储存信息
int x; //数字雨的横坐标
int y; //数字雨的纵坐标
int s; //数字雨下落速度
int sc; //当前执行帧数(相对于速度而言)
}Datrain;
void Set(Datrain* a); //初始化数字雨
void setCursor(int flg); //设置光标,flg=1:显示光标 flg=0:隐藏光标
void setLocate(int x,int y); //设置字符显示位置
int getkey(); //获得键值
int main()
{
int i=0; //下标信息兼循环变量
int j=0; //循环变量
int t=0; //时间变量
Datrain a[mm]={0}; //数字雨a结果体
Datrain b[mm]={0}; //数字雨b结构体
srand((unsigned)time(NULL)); //随机数初始化
setCursor(0); //关闭光标
while(getkey()!=27) //Esc键退出
{
t=(int)clock(); //获取当前时间
for(i=0;i<mm&&a[i].a;i++); //获取空闲的结构体下标信息
if(i<mm)
Set(&a[i]);
for(i=0;i<mm;i++)
{
if (a[i].a==0)
continue;
if(b[i].a) //获取赋值的b结构体信息
{
setLocate(b[i].x,b[i].y); //设置输出位置
printf(" "); //刷新当前数据(相当于删除光标)
}
b[i]=a[i]; //把a结构体的信息赋值给b
setLocate(a[i].x,a[i].y); //设置输出位置
printf("%c",a[i].a); //输出字符信息
++a[i].sc; //当前执行帧数+1
if(a[i].sc>a[i].s) //如果执行帧数大于设定帧数(相当于完成了一个动作--s相当于速度)
{
++a[i].y; //当前光标的纵坐标+1
a[i].sc=0; //当前帧数清零
}
if(a[i].y>23) //如果下落超出范围(23代表下落的最大高度)
{
setLocate(b[i].x,b[i].y);
printf(" ");
a[i].a=0; //从新把数字雨清零
}
}
while(((int)clock()-t)<sp); //缓冲输出时间
}
return 0;
}
void setCursor(int flg) //设置光标,flg=1:显示光标 flg=0:隐藏光标
{
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE); //获取输出句柄
CONSOLE_CURSOR_INFO cursor_info={100,flg}; //获取光标信息参数 info={光标透明度百分比,是否显示光标};
SetConsoleCursorInfo(hOut,&cursor_info); //设置光标信息
}
void setLocate(int x,int y) //设置字符显示位置
{
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE); //获取输出取句柄
COORD loc={x,y}; //获取坐标信息
SetConsoleCursorPosition(hOut,loc); //设置当前字符显示位置
}
int getkey() //获得键值
{
unsigned char a=0; //设定
int n=0;
if(!kbhit()) //如果当前没有键盘信息输入
return 0; //则返回
a=getch(); //获取键盘信息
if(a>127)
{
n=a*256;
a=getch();
}
return n+=a;
}
void Set(Datrain* a) //初始化数字雨
{
if (rand()/16383.0<1-u)
return ;
a->a=rand()%10+'0'; //赋予随机值
a->x=rand()%60+10; //行随机化
a->y=0; //从顶端开始
a->s=rand()%9+1; //表示下落速度(最大为1最小为1/8)
a->sc=0;
}
[此贴子已经被作者于2017-5-17 03:51编辑过]
