生活随笔
收集整理的這篇文章主要介紹了
C语言数字雨--90行代码
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
C語(yǔ)言數(shù)字雨–90行代碼
上圖
前期準(zhǔn)備:
因?yàn)槲覀冇肅語(yǔ)言寫會(huì)用到圖形庫(kù),所以需要進(jìn)行環(huán)境的搭建;
我這里用的VS2019,先到官方下載一個(gè)EasyX.exe;
具體在我的文章"雷霆戰(zhàn)機(jī)"敘述過(guò)
#include<stdio.h>
#include<time.h>
#include<Windows.h>
#include<graphics.h>
#define MAXSTRNUMBER 128
#define MAXLONG 20
#define SPACE 15
#define X 1920
#define Y 1080 struct Posiation
{int x
, y
;int speed
;char str
[MAXLONG
];
}strNumber
[MAXSTRNUMBER
];char getRandChar() {char temp
;int flag
= rand() % 3;if (flag
== 0) {return temp
= rand() % 10 + '0';}else if (flag
== 1) {return temp
= rand() % 26 + 'a';}else if (flag
== 2) {return temp
= rand() % 26 + 'A';}
}
void initializationStr() {for (int i
= 0; i
< MAXSTRNUMBER
;++i
) {strNumber
[i
].x
= i
* SPACE
;strNumber
[i
].y
= rand()%Y
;strNumber
[i
].speed
= rand() % 5 + 5;}for (int i
= 0; i
< MAXSTRNUMBER
;++i
) {for (int j
= 0; j
< MAXLONG
;++j
) {strNumber
[i
].str
[j
] = getRandChar();}}
}
void printXY() {cleardevice();for (int i
= 0; i
< MAXSTRNUMBER
;++i
) {for (int j
= 0; j
< MAXLONG
;++j
) {outtextxy(strNumber
[i
].x
, strNumber
[i
].y
- j
*SPACE
, strNumber
[i
].str
[j
]);settextcolor(RGB(0,255 - j
*13,0));}}
}
void changeXY() {for (int i
= 0; i
< MAXSTRNUMBER
;++i
) {strNumber
[i
].y
+= strNumber
[i
].speed
;if (strNumber
[i
].y
- SPACE
*MAXLONG
> Y
) {strNumber
[i
].y
= 0;}}
}
void changeStr() {for (int i
= 0; i
< MAXSTRNUMBER
;++i
) {strNumber
[rand()%MAXSTRNUMBER
].str
[rand()%MAXLONG
] = getRandChar();}
}
int main() {srand(GetTickCount());initgraph(X
,Y
);initializationStr();DWORD time1
, time2
;time1
= time2
= GetTickCount();while (1) {BeginBatchDraw();printXY(); changeStr();if (time2
- time1
> 20) { changeXY();time1
= time2
;}time2
= GetTickCount();EndBatchDraw();}getchar();closegraph();return 0;
}
總結(jié)
以上是生活随笔為你收集整理的C语言数字雨--90行代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。