The programming language uses C language to see the "meteor shower". It is said that making a wish to a meteor will have good luck

Keywords: Algorithm Dynamic Programming security

The programming language uses C language to see the "meteor shower". It is said that making a wish to a meteor will have good luck

order

  No matter how bright a meteor is, it will flash by.

Hi! This is the fox~~

Yes, I'm here again. Last time“ Fireworks ”Have you learned the confession program? This time, I'll give you a move again. I've learned "fireworks". Let's see it together“ meteor shower ”Let's go!!!     Direct upper interface

  I forgot to say last time. Because we are written in C language, it is a console program and can't create an interface. What should we do? We need to use it Windows Distant cousin EasyX The graphics library has come to build the interface. I forgot to say it last time. Please understand!!!

Let's teach you how to complete the "meteor shower" confession program step by step as usual today!

1, Header file

What's the make complaints about Tucao recently? Why? I think the main purpose of my sharing project is to let everyone learn knowledge. The first buddy is not nutritious. At the beginning, I didn't think about it. But in view of the fact that there are too many small partners, I'll send them out!!!

#include
#include 	// Third party graphics library, need to install
#include
#include

#include
#pragma comment(lib,"winmm.lib")

II structural morphology

Old friend, he's here again. Needless to say, look directly

struct Star		//Little star
{
	int x;
	int y;
	int r;
	int speed;			//speed
	COLORREF color;		//colour
};
struct Meteor
{
	int x;
	int y;
	int speed;
};

3, Initialization

To initialize stars and meteors, you need to use random functions to make stars and meteors look more natural.

//Initialize stars
void initStar(int i)
{
	star[i].x = rand() % getwidth();
	star[i].y = rand() % getheight();
	star[i].r = rand() % 3 + 1;
	star[i].speed = rand() % 5;	//0 1 2 3 4
	star[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
}
//Initialize meteor
void initMeteor(int i)
{
	//[0,2400)
	//[-1200,1200)
	meteor[i].x = rand() % (2 * getwidth()) - getwidth();	//[-1200,1200)
	meteor[i].y = 20 - 200;
	meteor[i].speed = rand()%15+1;
}

4, Draw function

Draw stars and meteors, simple mapping is OK

Draw stars
void drawStar()
{
	for (size_t i = 0; i < STAR_NUM; i++)
	{
		//putpixel(star[i].x, star[i].y, star[i].color);
		setfillcolor(star[i].color);
		solidcircle(star[i].x, star[i].y, star[i].r);
	}
}
//Draw meteors
void drawMeteor()
{
	for (size_t i = 0; i < METEOR_NUM; i++)
	{
		putimage(meteor[i].x, meteor[i].y, img + rand() % 2,SRCPAINT);
	}
}

5, Moving function

Not only the meteor should move, but also the stars should follow. How to move can be more natural. You can also optimize yourself.

//The movement of stars
void moveStar()
{
	for (size_t i = 0; i < STAR_NUM; i++)
	{
		star[i].x+=star[i].speed;
		if (star[i].x > getwidth())
		{
			star[i].x = 0;
		}
	}
}

//Meteor movement
void moveMeteor()
{
	for (size_t i = 0; i < METEOR_NUM; i++)
	{
		meteor[i].x += meteor[i].speed;
		meteor[i].y += meteor[i].speed;
		if (meteor[i].x >= getwidth() || meteor[i].y >= getheight())
		{
			initMeteor(i);
		}
	}
}

6, Interface design

Next is our interface design function, which is also a place for everyone to play freely. You can check some interface setting functions and design your own interface. You can play your own love words!!!

void welcome()
{
	//Play music mci media device interface 
	mciSendString(_T("open ./images/Romantic air.mp3 alias bgm"), NULL, 0, NULL);
	mciSendString(_T("play bgm"), NULL, 0, NULL);

	//Set random number seed
	srand((unsigned)time(NULL));
	/*@Thinking: confessed words*/

	//Set background mode
	setbkmode(TRANSPARENT);

	//Set text style
	settextstyle(40, 0, _T("Chinese block letters"));

	//If no key is pressed, the cycle continues
	while (!_kbhit())
	{
		//Clear screen
		cleardevice();
		putimage(0, 0, &bk);

		//Set text color
		settextcolor(RGB(rand()%256, rand() % 256, rand() % 256));

		//Output text
		int tx = (getwidth() - textwidth(_T("XXX I like you!"))) / 2;
		outtextxy(tx, 20, _T("XXX I like you!"));

		outtextxy(200, 100, _T("I don't want blood, I just want you"));
		outtextxy(200, 150, _T("Teacher, I love you more in bed"));
		outtextxy(200, 200, _T("Bao, you just want to cf The source weapons in the are cheap, but no one wants them"));
		outtextxy(200, 250, _T("Spring breeze ten miles, I only love you"));
		outtextxy(200, 300, _T("My program has only one main function for you"));
		outtextxy(200, 350, _T("Loneliness is not born, but from the moment you fall in love with someone."));
		outtextxy(200, 400, _T("If miracles have color, it must be the color of the rainbow"));
		outtextxy(200, 450, _T("The mountain has trees and branches, and the heart is happy with you. You don't know"));
		outtextxy(200, 500, _T("I never enjoy the future. After meeting you, I think every day"));
		outtextxy(200, 550, _T("------------ love you XXX"));
		
		for (size_t i = 0; i < 10; i++)
		{
			settextcolor(RGB(rand() % 256, rand() % 256, rand() % 256));
			outtextxy(rand()%getwidth(), rand()%getheight(), _T("♥"));
		}
		
		
		Sleep(1000);
	}
}

7, Main function

Finally, our main function. Call the previous functions together

int main()
{
	//1. Create a graphics window
	initgraph(1200, 800);
	//Set background color
	//setbkcolor(RGB(99, 99, 99));
	//cleardevice();

	
	loadimage(&bk, _T("./images/bk.png"),getwidth(),getheight());


	for (size_t i = 0; i < STAR_NUM; i++)
	{
		initStar(i);
	}
	for (size_t i = 0; i < METEOR_NUM; i++)
	{
		initMeteor(i);
	}
	loadImg();
	welcome();

	//Double buffer drawing
	BeginBatchDraw();
	while (true)
	{
		cleardevice();
		putimage(0, 0, &bk);

		drawStar();
		moveStar();

		drawMeteor();
		moveMeteor();

		FlushBatchDraw();
	}
	EndBatchDraw();
getchar();
	return 0;
}

summary

Well, in this way, you have got the "meteor shower" confession program. Isn't that enough? Don't say that our programmers are not romantic in the future. They don't want to do it. No one can resist romance. The code is not difficult. Anyone can do it with his heart. Come on!!!

Video Explanation

If you want to see the video explanation, look here. It's very detailed. You can listen carefully

It's said that making a wish to the meteor will bring good luck. Uploading now... Re uploading cancelled https://www.bilibili.com/video/BV1Tv411g7xL/

In the follow-UP, the UP master will release more project source codes and learning materials. I hope you can continue to pay attention to it and reply to any questions. If you want C/C + + learning materials and the source code of other projects, you can add group [684197747] [806041599]. Interested in the future development of programmers, you can focus on WeChat official account: Fox's encoding time, hope to learn progress with everyone!!!

thank

I'd like to say thank you. My "fireworks" confession program ranks first in the hot list. I'm very moved. I also thank you for your support. It makes me feel that my efforts are not empty. I also hope you can really learn something, instead of just looking at the results of the program or simply showing it to that person, I don't know the result of this article, but anyway, I hope you can not give up the opportunity to learn no matter what you encounter and see in the future. Thank you again!

Posted by robembra on Mon, 22 Nov 2021 22:12:13 -0800