学过C语言的我们,相信大家都能做出来
主要用到的知识点列举出来了
- 结构体
- 随机函数
- 图形库
基本实现
写了两个 :一个是带贴图的 只显示小星星
贴图之后既可以显示小星星也可以显示流星
星星结构体
//星星
struct Star
{
int x, y;//位置
int speed;//速度
int color;//颜色
}star[MAXSTAR];
流星结构体
//流星
struct Meteor
{
int x, y;//位置
int speed;//速度
int style;//风格
}meteor[MAXMETEOR];
星星和流星的初始化
//初始化星星
void initStar(int i)
{
star[i].x = rand() % 1200;
star[i].y = rand() % 800;
star[i].speed = rand() % 40;
star[i].color = RGB(rand() & 256, rand() % 256, rand() % 256);
}
//初始化流星
void initMetor(int i)
{
meteor[i].x = rand() % 2200 - 1200;
meteor[i].y = -400;
meteor[i].speed = rand() % 40;
meteor[i].style = rand() % 2;
}
源代码
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
#include<time.h>
#include<conio.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define PI 3.1415926
#define MAXSTAR 1024
#define MAXMETEOR 300
//定义图片对象
IMAGE img1, img2;
//星星
struct Star
{
int x, y;//位置
int speed;//速度
int color;//颜色
}star[MAXSTAR];
//流星
struct Meteor
{
int x, y;//位置
int speed;//速度
int style;//风格
}meteor[MAXMETEOR];
//初始化星星
void initStar(int i)
{
star[i].x = rand() % 1200;
star[i].y = rand() % 800;
star[i].speed = rand() % 40;
star[i].color = RGB(rand() & 256, rand() % 256, rand() % 256);
}
//画星星并且移动星星
void drawAndMoveStar(int i)
{
//画像素点
putpixel(star[i].x, star[i].y, star[i].color);
//移动
star[i].x += star[i].speed;
//setfillcolor(star[i].color);
//solidcircle(star[i].x, star[i].y, rand() % 3 + 1);
//移动到窗口外面
if (star[i].x > 1200)
{
initStar(i);
//setfillcolor(star[i].color);
//solidcircle(star[i].x, star[i].y, rand() % 3 + 1);
putpixel(star[i].x, star[i].y, star[i].color);
}
}
//初始化流星
void initMetor(int i)
{
meteor[i].x = rand() % 2200 - 1200;
meteor[i].y = -400;
meteor[i].speed = rand() % 40;
meteor[i].style = rand() % 2;
}
//画流星
void drawMeteor(int i)
{
for (i = 0; i < MAXMETEOR; i++)
{
if (meteor[i].style == 0)
{
putimage(meteor[i].x, meteor[i].y, &img1, SRCPAINT);
}
if (meteor[i].style == 1)
{
putimage(meteor[i].x, meteor[i].y, &img2, SRCPAINT);
}
}
}
void moveMeteor(int i)
{
for (i = 0; i < MAXMETEOR; i++)
{
meteor[i].x += meteor[i].speed;
meteor[i].y += meteor[i].speed;
if (meteor[i].x>1200 || meteor[i].y > 800)
{
initMetor(i);
}
}
}
void textPrint()
{
for (int i = 0; i < 60; i++)
{
cleardevice();;
settextcolor(RGB(rand() & 256, rand() % 256, rand() % 256));
//圆心位置 600,300 半径 150
int x = 600 + int(150 * sin(PI * 2 * i / 60));
int y = 300 + int(150 * cos(PI * 2 * i / 60));
outtextxy(x-50, y-100, L"一首小幸运送上");
settextstyle(i,0,L"宋体");
outtextxy(x , y, L"献给所有人");
Sleep(30);
}
Sleep(30);
cleardevice();
settextstyle(25, 0, L"楷体");
outtextxy(400, 200, L"原来你是我最想留住的幸运");
outtextxy(400, 250, L"原来我们和爱情曾经靠得那么近");
outtextxy(400, 300, L"那为我对抗世界的决定");
outtextxy(400, 350, L"那陪我淋的雨");
outtextxy(400, 400, L"一幕幕都是你");
outtextxy(400, 450, L"-尘不染的真心");
outtextxy(650, 500, L"小幸运");
Sleep(400);
cleardevice();
}
int main()
{
loadimage(&img1, L"1.jpg", 50, 50);
loadimage(&img2, L"2.jpg", 50, 50);
srand((unsigned int)time(NULL));
initgraph(1200, 800);
mciSendString(L"open 小幸运.mp3 alias music", 0, 0, 0);
mciSendString(L"play music", 0, 0, 0);
setbkmode(0);
textPrint();
int i;
for (i = 0; i < MAXSTAR; i++)
{
initStar(i);
}
for (int i = 0; i <MAXMETEOR; i++)
{
initMetor(i);
}
while (1)
{
BeginBatchDraw();
cleardevice();
for (i = 0; i < MAXSTAR; i++)
{
drawAndMoveStar(i);
}
drawMeteor(i);
moveMeteor(i);
Sleep(100);
EndBatchDraw();
}
system("pause");
return 0;
}
运行效果![在这里插入图片描述](https://img-blog.csdnimg.cn/20200520233914687.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1ODkzOTk5,size_16,color_FFFFFF,t_70)