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

//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 _LinkList
{
	STAR data;
	struct _LinkList* next;
}LinkList, ListNode;

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

bool initList(LinkList*& L);
bool listInsert_back(LinkList*& L, ListNode* node,STAR star);
bool listDelete(ListNode* node);
void listDestroy(LinkList*& L);

//StarLinkList.cpp

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

using namespace std;
//初始化
bool initList(LinkList*& L)
{
	L = new ListNode;
	if (!L) return false;
	L->next = NULL;
	return true;
}

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

//按位置,删除元素
bool listDelete(ListNode* node)
{
	if (!node) return false;
	ListNode* q;
	q = node->next;
	node->next = q->next;
	delete q;
	return true;
}

//销毁链表
void listDestroy(LinkList*& L)
{
	ListNode* p;
	p = L;
	while (p)
	{
		L = L->next;
		delete p;
		p = L;
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值