//开发工具:vs2019
//图形库:EasyX_2019
//qq:1020785391
//作者:WJ
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#define MAX_STAR 100 //最大星星数
#define SCREEN_WIDTH 720 //屏幕宽度
#define SCREEN_HIGHT 480 //屏幕高度
#define MAX_STEP 5 //星星移动步数
#define MAX_RADIUS 3 //星星半径
#define MAX_RGB 255 //星星颜色
//星星状态
typedef enum STATUS
{
STOP,
UP,
DOWN,
LEFT,
RIGHT,
RANDOM,
ALL_STATUS
}STATUS;
//星星
typedef struct STAR
{
int x; //星星x坐标
int y; //星星y坐标
STATUS status; //星星状态
int radius; //星星半径
int step; //星星移动步数
int color; //星星颜色
}STAR;
STAR star[MAX_STAR]; //声明星星数组
//初始化星星
void initStar(int i)
{
star[i].x = rand() % SCREEN_WIDTH;
star[i].y = rand() % SCREEN_HIGHT;
star[i].step = rand() % MAX_STEP + 1;
star[i].radius = rand() % MAX_RADIUS + 1;
star[i].status = (STATUS)(rand() % ALL_STATUS);
int rgb = rand() % MAX_RGB + 1;
star[i].color = RGB(rgb, rgb, rgb);
}
//移动星星
void moveStar(int i)
{
setfillcolor(BLACK);
solidcircle(star[i].x, star[i].y, star[i].radius);
switch (star[i].status)
{
case STOP:
break;
case UP:
star[i].y = star[i].y - star[i].step;
break;
case DOWN:
star[i].y = star[i].y + star[i].step;
break;
case LEFT:
star[i].x = star[i].x - star[i].step;
break;
case RIGHT:
star[i].x = star[i].x + star[i].step;
break;
case RANDOM:
star[i].status = (STATUS)(rand() % RANDOM);
switch (star[i].status)
{
case STOP:
break;
case UP:
star[i].y = star[i].y - star[i].step;
break;
case DOWN:
star[i].y = star[i].y + star[i].step;
break;
case LEFT:
star[i].x = star[i].x - star[i].step;
break;
case RIGHT:
star[i].x = star[i].x + star[i].step;
break;
default:
break;
}
break;
default:
break;
}
setfillcolor(star[i].color);
solidcircle(star[i].x, star[i].y, star[i].radius);
}
int main()
{
initgraph(SCREEN_WIDTH, SCREEN_HIGHT);
//初始化星星
for (int i = 0; i < MAX_STAR; i++)
{
initStar(i);
}
//画星星
for (int i = 0; i < MAX_STAR; i++)
{
setfillcolor(star[i].color);
solidcircle(star[i].x, star[i].y, star[i].radius);
}
//实现漫天星空移动版
while (true)
{
for (int i = 0; i < MAX_STAR; i++)
{
moveStar(i);
}
Sleep(100);
}
system("pause");
closegraph();
return 0;
}
数据结构之浪漫星空
最新推荐文章于 2021-07-29 12:43:51 发布