#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <time.h>
#define FONT_HEIGHT 13
#define FONT_WIDTH 11
#define MAX_DELAY 10
#define MIN_DELAY 1
#define DELAY_TIME 5000
typedef struct StringObj_tag
{
int max_delay;
int cur_delay;
int head_x;
int head_y;
int length;
char head_char;
} StringObj;
int charsInX;
int charsInY;
StringObj* ptStrObj;
void InitMatrix(void);
void Matrix(void);
void EndMatrix(void);
void InitStringObj(StringObj* obj, int i);
void DrawStringObj(StringObj* obj);
void InitGraph(void);
int main()
{
InitMatrix();
Matrix();
EndMatrix();
return 0;
}
void InitMatrix(void)
{
int i;
InitGraph();
for (i = 0; i < charsInX; i++)
InitStringObj(&ptStrObj[i], i);
}
void Matrix(void)
{
int i;
while (!kbhit())
{
for (i = 0; i < charsInX; i++)
{
if ( ptStrObj[i].head_y > charsInY + ptStrObj[i].length)
InitStringObj(&ptStrObj[i], i);
else
DrawStringObj(&ptStrObj[i]);
}
delay(DELAY_TIME);
}
}
void EndMatrix(void)
{
closegraph();
free((void*)ptStrObj);
}
void InitGraph(void)
{
int graph_mode;
int graph_driver = DETECT;
registerbgidriver(EGAVGA_driver);
initgraph(&graph_driver, &graph_mode, "");
charsInX = getmaxx()/FONT_WIDTH;
charsInY = getmaxy()/FONT_HEIGHT;
ptStrObj = (StringObj*)malloc(sizeof(StringObj)*charsInX);
}
void DrawStringObj(StringObj* obj)
{
char chs[2] = {'\0', '\0'};
int rear_x = obj->head_x * FONT_WIDTH;
int rear_y = (obj->head_y - obj->length)*FONT_HEIGHT;
int color;
if (obj->cur_delay > 0)
{
obj->cur_delay--;
return ;
}
color = ( random(2) ? LIGHTGREEN : GREEN);
setcolor(color);
chs[0] = obj->head_char;
outtextxy(obj->head_x * FONT_WIDTH, obj->head_y * FONT_HEIGHT, chs);
obj->head_y += 1;
setcolor(WHITE);
obj->head_char = chs[0] = random(0x7e - 0x21) + 0x21;
outtextxy(obj->head_x * FONT_WIDTH, obj->head_y * FONT_HEIGHT, chs);
setfillstyle(EMPTY_FILL, BLACK);
bar(rear_x, rear_y, rear_x + FONT_WIDTH - 1, rear_y + FONT_HEIGHT - 1);
obj->cur_delay = obj->max_delay;
}
void InitStringObj(StringObj* obj, int i)
{
obj->max_delay = obj->cur_delay = random(MAX_DELAY-MIN_DELAY)+MIN_DELAY;
obj->head_x = i;
obj->head_y = 0;
obj->head_char = random(0x7e-0x21) + 0x21;
obj->length = random(charsInY-10)+3;
}
源码下载: