前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >流星雨代码

流星雨代码

作者头像
一枕眠秋雨
发布2024-03-11 17:41:28
1330
发布2024-03-11 17:41:28
举报
文章被收录于专栏:司钰秘籍司钰秘籍

今天学的指针,但还没太学懂,所以先发个娱乐性代码叭,感兴趣的可以自己试一下

代码语言:text
复制
 #include <stdio.h>
 #include <conio.h>
 #include <graphics.h>
 #include <time.h>
 #include <Mmsystem.h>
 #pragma comment(lib,"winmm.lib")
 #define METEOR_NUM 75
 #define STAR_NUM   500
 
 IMAGE bk,img4,img2,img3;
 
 struct Star
 {
     int x, y;
     int r;
     int speed;
     COLORREF color;
 
     void init(int x, int y, int r, int speed, COLORREF color)
     {
         this->x = x;
         this->y = y;
         this->r = r;
         this->speed = speed;
         this->color = color;
     }
     void draw()
     {
         setfillcolor(color);
         solidcircle(x, y, r);
     }
     void move()
     {
         x += speed;
         if (x > getwidth()) x = 0;
     }
 };
 struct Star  stars[STAR_NUM];
 
 struct Meteor
 {
     int x, y;
     int speed;
     int style;
     void init(int x, int y, int speed, int style)
     {
         this->x = x;
         this->y = y;
         this->speed = speed;
         this->style = style;
     }
 
     void move()
     {
         x += speed;
         y += speed;
         if (x > getwidth()) x = 0;
     }
 
 }
 meteor[METEOR_NUM];
 
 void initStars();
 void initMeteor();
 void showStars();
 void showMeteor();
 void starsMove();
 void meteorMove();
 void loadImg();
 void initProject();
 void welcome();
 int main()
 {
     initProject();
 
     initStars();
 
     initMeteor();
 
     loadImg();
 
     welcome();
     while (1)
     {
         BeginBatchDraw();
         putimage(0, 0, &bk );
         showMeteor();
         showStars();
         meteorMove();
         starsMove();
         Sleep(150);    
         EndBatchDraw();
     }
     getchar();
     system("pause");
     return 0;
 }
 void showStars()
 {
     for (int i = 0; i < STAR_NUM; i++)
         stars[i].draw();
 }
 void showMeteor()
 {
     for (int i = 0; i < METEOR_NUM; i++)
     {
         if (meteor[i].style == 0)
         {
             putimage(meteor[i].x, meteor[i].y, &img4, SRCPAINT);
         }   if (meteor[i].style == 1)
         {
             putimage(meteor[i].x, meteor[i].y, &img2, SRCPAINT);
         }    if (meteor[i].style == 2)
         {
             putimage(meteor[i].x, meteor[i].y, &img3, SRCPAINT);
         }   
     }
 
 }
 
 
 void starsMove()
 {
     for (int i = 0; i < STAR_NUM; i++)
         stars[i].move();
 }
 void meteorMove()
 {
     for (int i = 0; i < METEOR_NUM; i++)
     {
         meteor[i].move();
         if (meteor[i].x > getwidth() || meteor[i].y > getheight())
         {
             initMeteor();
         }
     }
 }
 
 
 
 void initStars()
 {
     for (int i = 0; i < STAR_NUM; i++)
     {
         stars[i].init(rand() % getwidth(), rand() % getheight(), rand() % 3 + 1, rand() % 5, rand() % RGB(rand() % 256, rand() % 256, rand() % 256));
     }
 
 }
 void initMeteor()
 {
     for (int i = 0; i < METEOR_NUM; i++)
     {
         meteor[i].init(rand() % getwidth(), rand() % getheight(), rand() % 20+1, rand() % 3);
     }
 }
 
 
 
 
 void welcome()
 {
     setbkmode(TRANSPARENT);
     settextstyle(40, 0, L"华文行楷");
 
     int tx = (getwidth() - textwidth(L"少年何妨梦摘星,敢挽桑弓射玉衡")) / 2;
     srand(time(NULL));
 
     while (1)
     {
         if (_kbhit()) break;
         cleardevice();
         putimage(0, 0, &bk);
 
         settextcolor(RGB(rand() % 256, rand() % 256, rand() % 256));
         outtextxy(tx, 20, L"少年何妨梦摘星,敢挽桑弓射玉衡");
         outtextxy(75, 100, L"追求自由是永恒的主题,");
         outtextxy(500, 100, L"在星空中翱翔无羁。");
         outtextxy(75, 150, L"仰望星空,心潮澎湃,");
         outtextxy(500, 150, L"感叹人生短暂如流星,");
         outtextxy(75, 200, L"渴望留下永恒的痕迹,");
         outtextxy(500, 200, L"在宇宙中闪耀光芒。");
         outtextxy(75, 250, L"梦想不止,追逐不息,");
         outtextxy(500, 250, L"流星的瞬间也是美好,");
         outtextxy(275, 300, L"梦想在心中永不消逝。");
         Sleep(1000);
 
     }
 }
 
 void loadImg()
 {
     loadimage(&bk, L"bk.jpg", 889, 500);
     loadimage(&img4, L"img1.png", 40, 39);
     loadimage(&img2, L"img2.png", 55, 62);
     loadimage(&img3, L"img3.png", 10, 12);
 }
 
 void initProject()
 {
     initgraph(889, 500);
 
     mciSendString(L"open黑夜问白天.mp3", 0, 0, 0);
     mciSendString(L"play黑夜问白天.mp3", 0, 0, 0);
     Sleep(5000);
 }
 
 

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-03-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档