​01

编码过程

* 设置星星的属性,初始化星星,画星星,进行星星的移动
* 设置流星的属性,初始化流星
* 初始化加载数据,通过贴图来画流星,进行流星的移动
* 设置表白文字
* 主函数设置数组,加载界面
02

效果展示

03

代码分享

#include

#include//图形界面库

#include

#include

#include

#pragma comment(lib,"winmm")

#define MAXSTAR 1000

#define MAXMETEOR 100

IMAGE img1, img2, img3;

//星星的属性

struct Star

{

int x, y;

int step;

int color;

}star[MAXSTAR];

//流星的属性

struct Meteor

{

int x, y;

int step;

int style;

}meteor[MAXMETEOR];

//初始化星星

void initStar(int i)

{

star[i].x = rand() % 1000; //[0,999]

star[i].y = rand() % 600;

star[i].step = rand() % 6+3; //[0,5]

star[i].color = RGB(rand() % 255, rand() % 255, rand() % 255);

}

//画星星和移动星星

void moveStar(int i)

{

//画像素点

putpixel(star[i].x, star[i].y, star[i].color);

//移动处理

star[i].x += star[i].step;

//如果移动到了窗口外面

if (star[i].x > 1000)

{

putpixel(star[i].x, star[i].y, star[i].color);

initStar(i);

}

}

//初始化流星

void initMeteor(int i)

{

meteor[i].x = rand() % 2000-1000;//[-1000,999]

meteor[i].y = -600;//[0,599]-600-->[-600,-1]

meteor[i].step = rand() % 30 + 2;

meteor[i].style = rand() % 3; //[0,2]-->0,1,2

}

//初始化数据

void initDate(int i)

{

//加载图片

loadimage(&img1, "1.jpg", 30, 30);

loadimage(&img2, "2.jpg",40, 40);

loadimage(&img3, "3.jpg", 50, 50);

for (i = 0; i < MAXSTAR; i++)

{

initStar(i);

}

for (i = 0; i < MAXMETEOR; i++)

{

initMeteor(i);

}

}

//画流星:贴图

void drawMeteor(int i)

{

for (i = 0; i < MAXMETEOR; i++)

{

switch (meteor[i].style)

{

case 0:

putimage(meteor[i].x, meteor[i].y, &img1,SRCPAINT);

break;

case 1:

putimage(meteor[i].x, meteor[i].y, &img2, SRCPAINT);

break;

case 2:

putimage(meteor[i].x, meteor[i].y, &img3, SRCPAINT);

break;

}

}

}

//移动流星

void moveMeteor(int i)

{

for (i = 0; i < MAXMETEOR; i++)

{

meteor[i].x += meteor[i].step;

meteor[i].y += meteor[i].step;

//如果流星跑到窗口外面

if (meteor[i].x>1000 || meteor[i].y > 600)

{

initMeteor(i);

}

}

}

//文字输出

void text()

{

settextcolor(RGB(rand() % 255, rand() % 255, rand() % 255));

settextstyle(30, 0, "楷体");

char* text[50] = { "X", "X", "X", "我", "喜", "欢", "你", "做", "我", "女", "朋", "友",
"吧", "!", };

int x = 450, y = 100;

for (int i = 0; i < 14; i++)

{

if (i<7)

outtextxy(x, y + i * 50, text[i]);

else

outtextxy(x+100, y + (i-7) * 50, text[i]);

}

}

int main()

{

initgraph(1000, 600);

mciSendString("open 2.mp3", 0, 0, 0);

mciSendString("play 2.mp3 repeat", 0, 0, 0);

srand((unsigned int)time(NULL));//随机函数种子

setbkmode(TRANSPARENT);

int i = 0;

initDate(i);

while (1)

{

cleardevice();

for (i = 0; i < MAXSTAR; i++)

{

moveStar(i);

}

drawMeteor(i);

moveMeteor(i);

text();

Sleep(100);

}

_getch();

closegraph();

//system("pause");

return 0;

}

技术
©2019-2020 Toolsou All rights reserved,
css中上下左右居中的几种实现方法[CISCN 2019 初赛]Love Mathc/c++语言实现登陆界面Unity3D 人称设置(第一人称视角、第三人称视角)Fastadmin框架自定义搜索操作流程2021最新Python自动化软件测试笔试题(含答案)黑客帝国装逼的代码雨mysql数据库设置字符集配置修改my.ini文件(windows)python之panda模块1Python学习笔记:基础+进阶10道练习题