📄 雨滴.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <dos.h>
#include <graphics.h>
#define LEN sizeof(struct rainDrop)
int rainDropNumber=60;
struct rainDrop *head;
struct rainDrop
{
int startX;
int startY;
int endY;
int curX;
int curY;
int color;
int step;
int length;
struct rainDrop *next;
};
struct rainDrop *creatDrop(void)
{
struct rainDrop *p;
p=(struct rainDrop *)malloc(LEN);
p->startX=rand()%640;
p->startY=rand()%430;
p->endY=430+rand()%50;
p->curX=p->startX;
p->curY=p->startY;
p->step=8;
p->color=rand()%15+1;
p->length=rand()%10+10;
p->next=NULL;
return(p);
}
void recreatDrop(struct rainDrop *p)
{
p->startX=rand()%640;
p->startY=rand()%430;
p->endY=430+rand()%50;
p->curX=p->startX;
p->curY=p->startY;
p->step=8;
p->color=rand()%15+1;
p->length=rand()%10+10;
}
void initgraphics()
{
int gmode,gdriver;
gdriver=DETECT;
initgraph(&gdriver,&gmode,"");
}
void drawLine(struct rainDrop *p){
setcolor(p->color);
line(p->startX,p->startY,p->curX,p->curY);
}
void clearLine(struct rainDrop *p){
setcolor(BLACK);
line(p->startX,p->startY,p->curX,p->curY);
}
void updateLine(struct rainDrop *p){
if(p->step%5==0)
p->step++;
p->curY+=p->step;
p->startY=p->curY-p->length;
}
void rainDropDown(struct rainDrop *p){
if(p->curY>=p->endY)
{
clearLine(p);
recreatDrop(p);
}else
{
clearLine(p);
updateLine(p);
drawLine(p);
}
}
void creatRain(void)
{
struct rainDrop *p1,*p2;
int i;
p1=p2=creatDrop();
head=p1;
for(i=0;i<rainDropNumber;i++)
{
p2=creatDrop();
p1->next=p2;
p1=p2;
}
}
void clearAll()
{
struct rainDrop *p,*pf;
p=head;
while(p!=NULL)
{
pf=p;
p=p->next;
free(pf);
}
closegraph();
}
main()
{
struct rainDrop *p;
initgraphics();
creatRain();
p=head;
while(!kbhit()){
if(p==NULL){
p=head;
}
else{
rainDropDown(p);
}
p=p->next;
delay(50);
}
getch();
clearAll();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -