数据结构之循环链表,实现浪漫星空

//Star.h

//开发工具:vs2019
//图形库:EasyX_2019
//qq:1020785391
//作者:王杰
#pragma once
#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;		//星星状态
	unsigned radius;	//星星半径
	int step;			//星星移动步数
	int color;			//星星颜色
}STAR;

typedef struct _LinkNode {
	STAR data;
	struct _LinkNode* next;
}LinkNode, LinkList;

void initStar(STAR &star);
void moveStar(LinkNode* node);

bool initList(LinkList*& L);
bool listInsert_back(LinkList*& L, LinkNode* node, STAR star);
bool listDelete(LinkNode* node);

//Star循环链表.cpp

#include <iostream>
#include "Star.h"

using namespace std;

bool initList(LinkList* &L){
	L = new LinkNode;
	if (!L) return false;
	L->next = L;		//头结点自己指向自己,形成循环链表
	return true;
}

//尾插发
bool listInsert_back(LinkList* &L,LinkNode *node, STAR star) {
	node = new LinkNode;
	node->data = star;
	if (!L || !node) return false;
	if (L==L->next){
		node->next = L;
		L->next = node;
		return true;
	}
	LinkNode* last=NULL;
	last = L->next;
	while ((last->next) != L) last = last->next;
	node->next = last->next;
	last->next = node;
	return true;
}

bool listDelete(LinkNode* node)
{
	if (!node) return false;
	LinkNode* p = NULL;
	p = node->next;
	node->next = p->next;
	delete p;
	return true;
}

//Star.cpp

#include "Star.h"

//初始化星星
void initStar(STAR &star)
{
	star.x = rand() % SCREEN_WIDTH;
	star.y = rand() % SCREEN_HIGHT;
	star.step = rand() % MAX_STEP + 1;
	star.radius = rand() % MAX_RADIUS + 1;
	star.status = (STATUS)(rand() % ALL_STATUS);
	int rgb = rand() % MAX_RGB + 1;
	star.color = RGB(rgb, rgb, rgb);
}

//移动星星
void moveStar(LinkNode* node)
{
	STAR& star = node->next->data;
	setfillcolor(BLACK);
	solidcircle(star.x, star.y, star.radius);
	switch (star.status)
	{
	case STOP:
		break;
	case UP:
		star.y = star.y - star.step;
		if (star.y < 0) listDelete(node);
		break;
	case DOWN:
		star.y = star.y + star.step;
		if (star.y > SCREEN_HIGHT) listDelete(node);
		break;
	case LEFT:
		star.x = star.x - star.step;
		if (star.x < 0) listDelete(node);
		break;
	case RIGHT:
		star.x = star.x + star.step;
		if (star.x > SCREEN_WIDTH) listDelete(node);
		break;
	case RANDOM:
		star.status = (STATUS)(rand() % RANDOM);
		switch (star.status)
		{
		case STOP:
			break;
		case UP:
			star.y = star.y - star.step;
			if (star.y < 0) listDelete(node);
			break;
		case DOWN:
			star.y = star.y + star.step;
			if (star.y > SCREEN_HIGHT) listDelete(node);
			break;
		case LEFT:
			star.x = star.x - star.step;
			if (star.x < 0) listDelete(node);
			break;
		case RIGHT:
			star.x = star.x + star.step;
			if (star.x > SCREEN_WIDTH) listDelete(node);
			break;
		default:
			break;
		}
		break;
	default:
		break;
	}
	setfillcolor(star.color);
	solidcircle(star.x, star.y, star.radius);
}

int main()
{
	initgraph(SCREEN_WIDTH, SCREEN_HIGHT);
	LinkList* L;
	LinkNode* node = NULL;
	STAR star;
	initList(L);

	//初始化星星
	for (int i = 0; i < MAX_STAR; i++)
	{
		initStar(star);
		listInsert_back(L, node, star);

	}
	//画星星
	for (node=L->next;node!=L;node=node->next)
	{
		setfillcolor(node->data.color);
		solidcircle(node->data.x, node->data.y, node->data.radius);
	}
	//实现漫天星空移动版
	while (true)
	{
		for (node = L; node->next != L; node = node->next)
		{
			moveStar(node);
		}
		Sleep(100);
	}
	system("pause");
	closegraph();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值