太阳是黑色的

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

Archive for 7 月, 2004

C语言的问题

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

附上源程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#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;
   }
  }
 }