代碼雨鴨(作者:小權權
)
#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;
}
