#include<graphics.h>
#include<conio.h>
#include<time.h>//时间头文件
#include<mmsystem.h>//头文件
#pragma comment(lib,"winmm.lib")//库文件
IMAGE img_meteor[2];
//小星星,需要绘制,需要移动(速度),需要颜色
struct Star
{
int x;
int y;
int speed;
};
//初始化
void star_init(Star*star,int x,int y,int speed)
{
star->x=x;
star->y=y;
star->speed=speed;
}
//绘制
void star_draw(Star*star)
{
setfillcolor(RGB(rand()%256,rand()%256,rand()%256));
solidcircle(star->x,star->y,1 );
}
void star_moveby(Star*star,int dx,int dy)
{
star->x+=dx;
star->y+=dy;
}
//流星
typedef Star Meteor;
void meteor_init(Meteor*me,int x,int y,int speed)
{
star_init(me,x,y,speed);
}
void meteor_draw(Meteor*me)
{
putimage(me->x,me->y,img_meteor+rand()%2,SRCPAINT);
}
void meteor_moveby(Meteor*star,int dx,int dy)
{
star_moveby(star,dx,dy);
}
//定义变量,初始化,绘制
#define STAR_NUM 500
#define METEOR_NUM 100
Star stars[STAR_NUM];
Meteor mes[METEOR_NUM];
void init()
{
for(int i=0;i<STAR_NUM;i++)
{
int x=rand()%getwidth();
int y=rand()%getheight();
int speed=rand()%5+1;
star_init(stars+i,x,y,speed);
}
for(int i=0;i<METEOR_NUM;i++)
{
int x=rand()%getwidth();
int y=rand()%getheight();
int speed=rand()%5+1;
meteor_init(mes+i,x,y,speed);
}
}
void draw()
{
for(int i=0;i<STAR_NUM;i++)
{
star_draw(stars+i);
}
for(int i=0;i<METEOR_NUM;i++)
{
meteor_draw(mes+i);
}
}
void move()
{
for(int i=0;i<STAR_NUM;i++)
{
star_moveby(stars+i,stars[i].speed,0);
if(stars[i].x>getwidth())
{
stars[i].x=0;
}
}
for(int i=0;i<METEOR_NUM;i++)
{
meteor_moveby(mes+i,mes[i].speed,mes[i].speed);
if(mes[i].x>getwidth())
{
mes[i].x=-img_meteor->getwidth();
}
else if(mes[i].y>getheight())
{
mes[i].y=-img_meteor->getheight();
}
}
}
int main()
{
//创建窗口
initgraph(1200,800);
//设置随机数种子(只有这样随机数才能随机)
srand(time(NULL));
init();
//播放音乐
mciSendString("open C:\\Users\\86139\\Desktop\\浪漫空气.mp3 alias bgm",NULL,0,NULL);
mciSendString("play bgm repeat",NULL,0,NULL);
//绘制背景图
IMAGE image_bk[2];
loadimage(image_bk+0,"C:\\Users\\86139\\Desktop\\bk.png",getwidth(),getheight());//一定要用完整路径,不然就显示不出来
loadimage(image_bk+1,"C:\\Users\\86139\\Desktop\\背景.jpg",getwidth(),getheight());
loadimage(img_meteor+0,"C:\\Users\\86139\\Desktop\\1.jpg",50,50);
loadimage(img_meteor+1,"C:\\Users\\86139\\Desktop\\2.jpg",50,50);
//输出图片
//设置文字样式
settextstyle(40,0,"微软雅黑");
//设置背景模式,不是黑色底纹
setbkmode(TRANSPARENT);
//按键按下,退出循环
for(int i=0;i<20;i++)
{
//输出图片
putimage(0,0,image_bk+0);//不然心越来越多
//设置文字颜色
settextcolor(RGB(rand()%256,rand()%256,rand()%256));//为了让他闪
//绘制文字
outtextxy(300,20,"诗经·蒹葭");
outtextxy(20,100,"蒹葭苍苍,白露为霜。所谓伊人,在水一方。");
outtextxy(20,200," 溯洄从之,道阻且长。溯游从之,宛在水中央。");
outtextxy(20,300,"蒹葭萋萋,白露未晞。所谓伊人,在水之湄。");
outtextxy(20,400,"溯洄从之,道阻且跻。溯游从之,宛在水中坻。");
outtextxy(20,500,"蒹葭采采,白露未已。所谓伊人,在水之涘。");
outtextxy(20,600,"溯洄从之,道阻且右。溯游从之,宛在水中沚。");
for(int i=0;i<10;i++)
{
settextcolor(RGB(rand()%256,rand()%256,rand()%256));
outtextxy(rand()%getwidth(),rand()%getheight(),rand()%2?"爱你":"me too");
}
Sleep(500);//让闪的慢一点
}
//流星雨的界面
while(1)
{
BeginBatchDraw();//双缓冲绘图
putimage(0,0,image_bk+1);
draw();
EndBatchDraw();
move();
Sleep(10);
}
getch();//防止闪退,在devc++中无getchar
getch();
return 0;
}
devc++流星代码
最新推荐文章于 2024-10-16 19:14:02 发布