上升星河(C/C++)
静态效果图
#include#include #include #include #include #include #pragma comment(lib,"winmm.lib") #define int_MAX 100//星星最大值 #define HIGH 640//高 #define WIDE 560//宽 #define RADIUS_MAX 3//半径 #define INTERVAL_MAX 5//间隔 void initStart(int MAx); struct STARS { int x; int y; unsigned radius;//半径 int mobile;//移动距离 int color; enum STATE stat; }; struct STARS stars[int_MAX]; enum STATE{ STOP, UP, DOWN, LEFT, RIGHT, RANDOM, ALL_STATUS }; bool isEnd() { for (int i = 0; i < int_MAX; i++) { if (stars[int_MAX].x > 0 && stars[int_MAX].x < WIDE && WIDE && stars[int_MAX].y>0 && stars[int_MAX].y < HIGH) { return false; } } return true; } void MoveStars(int Number) { if (stars[Number].stat == STOP) return; //擦除原来的星星 setfillcolor(BLACK); solidcircle(stars[Number].x, stars[Number].y, stars[Number].radius); if (stars[Number].stat == DOWN) { stars[Number].y = stars[Number].y + stars[Number].radius; if (stars[Number].y > HIGH) stars[Number].y = 0; } else if (stars[Number].stat == UP) { stars[Number].y -= stars[Number].radius; if (stars[Number].y < 0)stars[Number].y = HIGH; } else if (stars[Number].stat == LEFT) { stars[Number].x -= stars[Number].radius; if (stars[Number].x < 0)stars[Number].x = WIDE; } else if (stars[Number].stat == RIGHT) { stars[Number].x += stars[Number].radius; if (stars[Number].x > WIDE) stars[Number].x = 0; }
//重新绘制 setfillcolor(stars[Number].color); solidcircle(stars[Number].x, stars[Number].y, stars[Number].radius); } void initStart(int MAx) { if (MAx > int_MAX) { fprintf(stderr, "超出总数[%d]", MAx); return; } int rgb; stars[MAx].x = rand() % WIDE; stars[MAx].y = (rand() % (HIGH - 100)); stars[MAx].radius = rand() % (RADIUS_MAX+1);//半径1-3 stars[MAx].mobile = rand() % (INTERVAL_MAX + 1);//移动距离1-5 rgb = 255 * stars[MAx].mobile / INTERVAL_MAX; stars[MAx].color = RGB(rgb, rgb, rgb); stars[MAx].stat =UP; } int main(void) { initgraph(WIDE, HIGH); mciSendString("play 中国交响乐团 - 春节序曲.mp3 repeat", 0, 0, 0); HWND window = GetHWnd(); SetWindowText(window, "上升");
//产生随机值 for (int i = 0; i < int_MAX; i++) { initStart(i); } //绘制 for (int i = 0; i < int_MAX; i++) { setfillcolor(stars[i].color); solidcircle(stars[i].x, stars[i].y, stars[i].radius); } bool quit = false; while (quit==false) { for (int i = 0; i < int_MAX; i++) { MoveStars(i); } if (isEnd()) { quit == true; } Sleep(50); } //system("pause"); std::cin.get(); return 0; }