#include<time.h>
//#include<mmsystem.h>此头文件有些问题.....
#include <graphics.h>//图形库头文件
#pragma comment(lib,"winmm.lib")
#define MAXStar 1000 //星星
#define MAXStar0 300 //流星
IMAGE img1, img2, img3, img4;
void initData();
void initStar(int i);
void initStar0(int i);
void moveStar(int i);
void moveStar0();
void drawStar0();
struct STAR//星星
{
int x, y;
int v;
int color;
}star[MAXStar];
struct STAR0//流星
{
int x, y;
int v;
int style;//图片样式
}star0[MAXStar0];
int main()
{
initData();
mciSendString("open 666.mp3 alias music", 0, 0, 0);//背景音乐
mciSendString("play music repeat", 0, 0, 0);
while (1)
{
BeginBatchDraw();
cleardevice();
for (int i = 0; i < MAXStar; i++)
{
moveStar(i);
}
moveStar0();
drawStar0();
Sleep(100);
EndBatchDraw();
}
closegraph();
return 0;
}
void initData()
{
srand((unsigned int)time(NULL));
initgraph(980,720);
//初始化数据
loadimage(&img1, "P1.jpg", 50, 50);
loadimage(&img2, "P2.jpg", 50, 50);
loadimage(&img3, "P3.jpg", 50, 50);
loadimage(&img4, "P4.jpg", 50, 50);
//初始化星星
int i;
for (i = 0; i<MAXStar; i++)
{
initStar(i);
star[i].x = rand() % 640;
}
//初始化流星
for (i = 0; i<MAXStar0; i++)
{
initStar0(i);
star0[i].x = rand() % 640;
}
}
void initStar(int i)
{
star[i].x = 0;
star[i].y = rand() % 480;
star[i].v = rand() % 5;
star[i].color = RGB(star[i].v * 51, star[i].v * 51, star[i].v * 51);//255/5=51;
}
void initStar0(int i)
{
star0[i].x = rand() % 1280 - 2080;
star0[i].y = -100;
do
{
star0[i].v = rand() % 30 + 1;
} while (star0[i].v<5);//避免出现速度过慢的
star0[i].style = rand() % 4 + 1;//1-4
}
void moveStar(int i)
{
putpixel(star[i].x, star[i].y, 0);
star[i].x += star[i].v;
if (star[i].x>640)
initStar(i);
putpixel(star[i].x, star[i].y, star[i].color);
}
void moveStar0( )
{
for (int i = 0; i < MAXStar0; i++)
{
star0[i].x += star0[i].v;
star0[i].y += star0[i].v;
}
}
void drawStar0()
{
for (int i = 0; i < MAXStar0; i++)
{
switch (star0[i].style)
{
case 1:
putimage(star0[i].x, star0[i].y, &img1, SRCPAINT);
break;
case 2:
putimage(star0[i].x, star0[i].y, &img2, SRCPAINT);
break;
case 3:
putimage(star0[i].x, star0[i].y, &img3, SRCPAINT);
break;
case 4:
putimage(star0[i].x, star0[i].y, &img4, SRCPAINT);
break;
}
}
}