龙空技术网

「源码分享」C语言浪漫流星雨表白程序

C语言学习基地 714

前言:

今天朋友们对“c语言打印星星”可能比较着重,各位老铁们都需要知道一些“c语言打印星星”的相关资讯。那么小编同时在网摘上网罗了一些有关“c语言打印星星””的相关知识,希望我们能喜欢,各位老铁们快快来了解一下吧!

​01

编码过程

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

02

效果展示

03

代码分享

#include<stdlib.h>

#include<graphics.h>//图形界面库

#include<conio.h>

#include<time.h>

#include<mmsystem.h>

#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;

}

标签: #c语言打印星星