Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the effect of meteor shower in C language

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how C language to achieve the effect of meteor shower, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article. Let's learn about it!

Forget to mention last time, because we are written in C language, so it is the console program, can not create the interface, then how to do, we have to use Windows's distant cousin EasyX graphics library to build the interface, forgot to say last time, hope to understand!

Today, as usual, we will teach you step by step how to complete this "meteor shower" confession program!

I. header file

Recently, I have always been complained about why there is no header file. The reason is that I think the main purpose of sharing the project is to let everyone learn knowledge. The header file, which is not nutritious, was not considered at the beginning, but in view of the fact that there are too many friends in need, I'll send it out!

# include#include / / third-party graphics library, need to install # include#include # include#pragma comment (lib, "winmm.lib") II, structure

There he is again, old friend structure. Needless to say, just look at it.

Struct Star / / small star {int x; int y; int r; int speed; / / Speed COLORREF color; / / Color}; struct Meteor {int x; int y; int speed;}; III. Initialization

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

/ / initialize the 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 12 3 4 star.color = RGB (rand ()% 256, rand ()% 256, rand ()% 256);} / / initialize meteor void initMeteor (int I) {/ / [0mai2400) / / [- 1200 getwidth 1200) meteor [I] .x = rand ()% (2 * getwidth ())-color (); / / [- 1200 getwidth () 1200) meteor [I] .y = 20-200 Meteorology [I] .speed = rand () + 1;} IV. Drawing function

Draw stars and meteors, simple mapping is fine.

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); }}//绘制流星void drawMeteor(){ for (size_t i = 0; i < METEOR_NUM; i++) { putimage(meteor[i].x, meteor[i].y, img + rand() % 2,SRCPAINT); }}五、移动函数 不仅流星要动,星星也要跟着动,怎么动才能更自然,自己也可以优化一下。 //星星的移动void moveStar(){ for (size_t i = 0; i < STAR_NUM; i++) { star[i].x+=star[i].speed; if (star[i].x >

Getwidth () {starI] .x = 0;} / / moving void moveMeteor () {for (size_t I = 0; I) of meteors

< METEOR_NUM; i++) { meteor[i].x += meteor[i].speed; meteor[i].y += meteor[i].speed; if (meteor[i].x >

= getwidth () | | initMeteor [I] .y > = getheight () {initMeteor (I);}} VI. Interface design

Next is our interface design function, this is also where everyone is free to play, you can check some interface settings functions, design your own interface, inside the love words can be played by yourself!

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: words of confession * / / set background mode setbkmode (TRANSPARENT); / / set text style settextstyle (40, 0, _ T ("Chinese Xingkai")) / / if no key is pressed, cycle through 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 a blood bag, I just want you")) Outtextxy (200,150, _ T ("teacher I love you more in bed"); outtextxy (200,200, _ T ("you think the source weapon in cf is cheap but no one wants it"); outtextxy (200,250, _ T ("I only love you in the spring breeze") Outtextxy (200,300, _ T ("you are the only main function in my program"); 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 a rainbow"); outtextxy (200,450, _ T); outtextxy (200,500, _ T ("I never enjoy the future, I've been thinking about you every day since I met you"). Outtextxy (200,550, _ T ("- Love XXX"); for (size_t I = 0; I < 10; iTunes +) {settextcolor (RGB (rand ()% 256, rand ()% 256, rand ()% 256) Outtextxy (rand ()% getwidth (), rand ()% getheight (), _ T ("♥"));} Sleep (1000);}} VII, main function

Finally, there is our main function. Let's call the previous functions together.

Int main () {/ / 1, create graphics window initgraph (1200); / / 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) For (size_t I = 0; I < METEOR_NUM; I +) {initMeteor (I);} loadImg (); welcome (); / / a double buffer drawing BeginBatchDraw (); while (true) {cleardevice () Putimage (0,0, & bk); drawStar (); moveStar (); drawMeteor (); moveMeteor (); FlushBatchDraw ();} EndBatchDraw (); getchar (); return 0 } these are all the contents of the article "how to achieve the effect of meteor shower in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report