数据结构之浪漫星空

//开发工具: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;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值