代码雨鸭(作者:小权权)
#include "stdio.h" #include "windows.h" #include "graphics.h" #include "stdlib.h" #include "time.h" #define SCREEN_WIDTH 1366 //界面宽度 #define SCREEM_HEIGHT 768 //界面长度 #define DEF_RAIN_NUM 91 //雨的列数 //数字雨开始的位置 int g_nRainPos[DEF_RAIN_NUM] = { 0 }; //数字雨的字符串 char g_strRain[DEF_RAIN_NUM][10] = {0}; //随机产生一个字母 char RamdomLetter(){ char Letter = 0; while(1){ Letter = rand() % 123; //随机产生0-122的一个字符 if((Letter >= 65 && Letter <= 90) || Letter >= 97){ return Letter; } } } //随机产生数字雨开始的位置 void randrain(){ for(int i = 0; i < DEF_RAIN_NUM; i++){ g_nRainPos[i] = rand() % SCREEM_HEIGHT; } } //初始化数字雨 void Digitalrain(){ for(int i = 0; i < DEF_RAIN_NUM; i++){ for(int j = 0; j < 10; j++){ g_strRain[i][j] = RamdomLetter(); } } } //显示雨 void ShowNumRain(){ for(int i = 0 ; i < DEF_RAIN_NUM; i++){ //设置字体 settextcolor(RGB(255,255,255)); for(int j = 0; j < 10; j++){ outtextxy(i * 15,g_nRainPos[i] - 15 * j, g_strRain[i][j]); settextcolor(RGB(0, 255 - 28 * j, 0)); } } for(i = 0; i < DEF_RAIN_NUM; i++){ g_nRainPos[i]+= 15; //屏幕刷新后雨的位置下降15 } for(i = 0; i < DEF_RAIN_NUM; i++){ if(g_nRainPos[i] - 10 * 15 >= SCREEM_HEIGHT){ g_nRainPos[i] = 0; } } } int main() { srand((unsigned)time(NULL)); initgraph(1366,768);//显示屏蔽大小 randrain(); Digitalrain(); BeginBatchDraw(); while(1){ Digitalrain(); ShowNumRain(); FlushBatchDraw(); Sleep(100);//延迟0.1秒 cleardevice();//清空 } EndBatchDraw(); closegraph(); return 0; }