少年听雨歌楼上,红烛昏罗帐.壮年听雨客舟中,江阔云低断雁叫西风. 而今听雨僧庐下,鬓已星星也! 悲欢离合总无情,一任阶前点滴到天明。

2004年7月1日星期四

C语言的问题

昨天把下雨的题终于编完了。对Graphics.h里面的一些函数有了一个初步的了解。我觉得我设计的这个程序是一种多线程的程序的雏形,因为两个雨滴是相互独立的,互不影响。这是放在一个数组里面实现的。目前的问题是如果把间隔时间设置的相对短些的话,动画要光滑些,但是有闪烁的感觉。这也是所有编程语言的擦坪函数所带来的,怎么解决,确实是一个问题。

附上源程序:


#include <stdio.h>

#include <math.h>

#include <graphics.h>

#include <stdlib.h>

#include <time.h>

#include <dos.h>

#include <conio.h>


#define PI 3.1415926


#define RAINDROPLEN 15 /* The length of a raindrop */

#define RAINTANGLE 0 /* The tangle of each raindrop */

#define RAINDROPZ 0 /* The internal of each raindrop */

#define RAINCOLOR white /* The color of each raindrop */

#define RAINWAVELEVEL 40 /* The level of each wave */

#define DELAYTIME 2000 /* The internal of each flash of the screen */


/* Calculate the next X,Y of a raindrop */

#define RAINDROPNEXTX(n,m) (int)((RAINDROPLEN/m+n)*sin(RAINTANGLE*(PI/180)))

#define RAINDROPNEXTY(n,m) (int)((RAINDROPLEN/m+n)*cos(RAINTANGLE*(PI/180)))


#define RAINDROPS 100 /* The total numbers of raindrops */



struct raindrop

{

int posx;

int posy;

int state; /* The current state of raindrop. 1 for raindrop. 2 for wave. */

int wavelevel; /* The current wave level */

};


void Init(struct raindrop *drops);

void DrawDrops(struct raindrop *drops); /* Draw all the raindrops */

void CalDrops(struct raindrop *drops); /* Calculate the next raindrops position */



int main(void)

{

struct raindrop drops[RAINDROPS];

int gdriver=DETECT,gmode,i=0;

initgraph(&gdriver,&gmode,""); /* Init graphic mode */

Init(drops);

setbkcolor(0);

while(!kbhit())

{

cleardevice();

DrawDrops(drops);

delay(DELAYTIME);

CalDrops(drops);

}

return 0;

}


void Init(struct raindrop *drops)

{

int i;

randomize();

for(i=0;i<RAINDROPS;i++,drops++)

{

drops->posx=random(600);

drops->posy=random(300);

drops->state=1;

drops->wavelevel=0;

}

}


void DrawDrops(struct raindrop *drops)

{

int i;

int endx,endy;

setcolor(WHITE);

for(i=0;i<RAINDROPS;i++,drops++)

{

if (drops->state==1)

{

endx=RAINDROPNEXTX(0,1);

endy=RAINDROPNEXTY(0,1);

line(drops->posx,drops->posy,drops->posx+endx,drops->posy+endy);

}

else

{


ellipse(drops->posx,drops->posy,0,360,1+(drops->wavelevel)*0.75,0.05+(drops->wavelevel)*0.15);

}

}

}


void CalDrops(struct raindrop *drops)

{

int i;

int endx,endy;

int temp;

for(i=0;i<RAINDROPS;i++,drops++)

{

if (drops->state==1)

{

endx=RAINDROPNEXTX(RAINDROPZ,10);

endy=RAINDROPNEXTY(RAINDROPZ,10);

if ((endx+drops->posx)>=640(endy+drops->posy)>=400)

{

temp=random(100);

if (temp%2==0(endx+drops->posx)>=680(endy+drops->posy)>=550)

{

drops->state=2;

drops->wavelevel=1;

}

else

{

drops->posx=drops->posx+endx;

drops->posy=drops->posy+endy;

}


}

else

{

drops->posx=drops->posx+endx;

drops->posy=drops->posy+endy;

}

}

else

{

if ((drops->wavelevel)<RAINWAVELEVEL)

{


drops->wavelevel++;

}

else

{

drops->posx=random(600);

drops->posy=random(100);

drops->wavelevel=0;

drops->state=1;

}

}

}

没有评论: