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
| #include<stdio.h>
#include<stdlib.h>
#include<graphics.h> #include<conio.h> #define STR_SIZE 20 #define RAIN_NUM 128 #define WIDTH 960 #define HEIGHT 640 #define STR_W 15
struct rain { int x; int y; int speed; char str[STR_SIZE]; }rain[RAIN_NUM];
char createch() { char ch = 0; int flag = rand() % 3; if (flag == 0) { ch = rand() % 10 + '0'; } else if (flag == 1) { ch = rand() % 26 + 'a'; } else { ch = rand() % 26 + 'A'; } return ch; }
void GameInit() { for (int i = 0; i < RAIN_NUM; i++) { rain[i].x = i*STR_W; rain[i].y = rand() % HEIGHT; rain[i].speed = rand() % 5 +5 ; for (int j = 0; j < STR_SIZE; j++) { rain[i].str[j] = createch(); }
} }
void GameDraw() { BeginBatchDraw(); cleardevice(); for (int i = 0; i < RAIN_NUM; i++) { for (int j = 0; j < STR_SIZE; j++) { settextcolor(RGB(0,255-j*13,0)); outtextxy(rain[i].x,rain[i].y-15*j,rain[i].str[j]); } } EndBatchDraw(); } void changeCh() { for (int i = 0; i < RAIN_NUM; i++) { for (int j = 0; j < STR_SIZE; j++) { rain[i].str[rand() % STR_SIZE] = createch(); } } }
void GamePlay() { for (int i = 0; i < RAIN_NUM; i++) { rain[i].y+=rain[i].speed; if (rain[i].y-STR_SIZE*STR_W >= HEIGHT) { rain[i].speed = rand() % 5 + 5; rain[i].y = 0; } changeCh(); } }
void stop() { if (_kbhit() && _getch() == ' ') { while (_getch() != ' '); } } int main(void) { initgraph(WIDTH, HEIGHT); srand(GetTickCount()); GameInit(); while (1) { GamePlay(); GameDraw(); changeCh(); stop(); } getchar(); return 0; }
|
参考视频:https://www.bilibili.com/video/BV1kK411M7qo?t=5027