c++控制台做的基于粒子系统的礼花组字:春节快乐

圣手书生,恭祝大家,春节快乐

代码并不复杂,这里分别讲解一下。

粒子系统的设计:

struct Cell{
	int x, y, tx, ty, len;
	int s, e;
	int color;
	void show(int t){
		if (t>=s and t<=e){
			int x1= x+ (tx-x)*(t-s)/(e-s);
			int y1= y+ (ty-y)*(t-s)/(e-s);
			if (x1>=0 && y1>0 && y1<MAXY){
				setColor(color);
				gotoxy(x1,y1);
				cout << "*";
			}
		}
		if (t>=s+len and t<=t+len){
			int tt= t-len;
			int x1= x+ (tx-x)*(tt-s)/(e-s);
			int y1= y+ (ty-y)*(tt-s)/(e-s);
			if (x1>=0 && y1>0 && y1<MAXY){
				gotoxy(x1,y1);
				cout << " ";
			}
		}
	}
} cells[2000];

粒子的信息基本参数:起点x,y,终点x,y,有效长度,可见时间,消失时间,颜色。

只有一个控制粒子显示的函数:当粒子在时间区域内,显示一个星号作为粒子的形态,在有效长度之后,显示空格以隐藏它。

显示所有粒子,只通过这样一个非常简单的函数:

void show(){
	int total= 100;
	for (int t=0; t<=total; t++){
		for (int i=0; i<n; i++){
			cells[i].show(t);
		}
		Sleep(100);
	}
}

添加圆形礼花:

void addCircle(int cx, int cy, int start, int color){
	int r= 20;
	double pi=3.14;
	for (int i=0; i<10; i++){
		int j= i*36;
		int x1= cx+ r*cos(j*pi/180);
		int y1= cy+ r*sin(j*pi/180);
		cells[n++]={cx, cy, x1, y1, 5, start, start+10, color};
	}
	addCell(cx, cy+20, cx, cy, 3, start-7, start, color);
}

添加字形礼花:

void addWord(int cx, int cy, int start, int color, int zi){
	for (int i=0; i<16; i++){
		int a= data[zi][i];
		for (int j=0; j<16; j++){
			int b= a%2;
			a/=2;
			if (b>0) {
				addCell(cx, cy, cx+8-j, cy-8+i, 2, start, start+15, color);
				addCell(cx+8-j, cy-8+i, cx+8-j, cy-8+i, 8, start+15, start+25, color);
			}
		}
	}
	addCell(cx, cy+20, cx, cy, 3, start-7, start, color);
}

字形礼花涉及了字库的信息,这里为了让代码简洁,直接将用到的字提取为数组,嵌入到了代码中。

int data[20][16]={{0,16368,2080,1088,640,256,1728,6192,57614,256,8176,256,256,256,32764,0},
			{16,248,16128,256,256,16376,256,256,256,65534,256,256,256,256,1280,512},
			{544,528,520,16352,544,544,544,544,65532,516,516,516,516,552,528,512},
			{1088,1088,16376,1088,1088,65534,1088,2080,4624,8712,49702,4752,4680,8776,2560,1024},
			{8192,5116,4612,64004,2564,4612,5116,14480,21648,38032,4240,4370,4370,4626,5134,6144},
			{256,256,256,256,256,65534,256,256,640,640,1088,1088,2080,4112,8200,49158},
			{512,256,32766,16386,32772,32764,512,3336,29072,672,3264,29088,1688,6278,57984,256},
			{256,256,32764,256,16376,512,65534,2080,4112,12264,51238,2080,4064,2080,2080,4064},
			{2080,2080,65534,2080,2080,0,32760,520,520,520,520,592,544,512,512,512},
			{4160,4160,4160,5112,6216,21576,20552,20552,38910,4160,4256,4256,4368,4368,4616,5126},
			{32,240,7936,4096,4352,8448,8448,16380,256,2336,2320,4360,8452,16644,1280,512},
			{256,4352,4352,4352,16380,8448,16640,33024,256,16376,256,256,256,256,65534,0} 
			};

最后,主程序进行组装:

int main(){
	hideCursor();
	addCircle(20, 5, 12, 9);
	addCircle(40, 7, 13, 10);
	addCircle(80, 7, 14, 11);
	addCircle(60, 5, 14, 12);
	addWord(20, 10, 32, 14, 0);
	addWord(40, 12, 33, 14, 1);
	addWord(60, 10, 34, 14, 2);
	addWord(80, 12, 34, 14, 11);
	addWord(20, 12, 52, 13, 3);
	addWord(40, 10, 53, 13, 4);
	addWord(60, 12, 54, 13, 5);
	addWord(80, 10, 54, 13, 6);
	addWord(25, 12, 72, 12, 7);
	addWord(45, 10, 73, 12, 8);
	addWord(65, 12, 74, 12, 9);
	addWord(85, 10, 74, 12, 10);
	show();
	return 0;
}

4个圆形礼花,12个字形礼花。

完整源代码下载:

dev-c++开发的控制台粒子系统放礼花组字-C++文档类资源-CSDN下载圣手书生,恭祝大家,春节快乐更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/xiaorang/77996645


版权声明:本文为xiaorang原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。