今天学的指针,但还没太学懂,所以先发个娱乐性代码叭,感兴趣的可以自己试一下
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <time.h>
#include <Mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define METEOR_NUM 75
#define STAR_NUM 500
IMAGE bk,img4,img2,img3;
struct Star
{
int x, y;
int r;
int speed;
COLORREF color;
void init(int x, int y, int r, int speed, COLORREF color)
{
this->x = x;
this->y = y;
this->r = r;
this->speed = speed;
this->color = color;
}
void draw()
{
setfillcolor(color);
solidcircle(x, y, r);
}
void move()
{
x += speed;
if (x > getwidth()) x = 0;
}
};
struct Star stars[STAR_NUM];
struct Meteor
{
int x, y;
int speed;
int style;
void init(int x, int y, int speed, int style)
{
this->x = x;
this->y = y;
this->speed = speed;
this->style = style;
}
void move()
{
x += speed;
y += speed;
if (x > getwidth()) x = 0;
}
}
meteor[METEOR_NUM];
void initStars();
void initMeteor();
void showStars();
void showMeteor();
void starsMove();
void meteorMove();
void loadImg();
void initProject();
void welcome();
int main()
{
initProject();
initStars();
initMeteor();
loadImg();
welcome();
while (1)
{
BeginBatchDraw();
putimage(0, 0, &bk );
showMeteor();
showStars();
meteorMove();
starsMove();
Sleep(150);
EndBatchDraw();
}
getchar();
system("pause");
return 0;
}
void showStars()
{
for (int i = 0; i < STAR_NUM; i++)
stars[i].draw();
}
void showMeteor()
{
for (int i = 0; i < METEOR_NUM; i++)
{
if (meteor[i].style == 0)
{
putimage(meteor[i].x, meteor[i].y, &img4, SRCPAINT);
} if (meteor[i].style == 1)
{
putimage(meteor[i].x, meteor[i].y, &img2, SRCPAINT);
} if (meteor[i].style == 2)
{
putimage(meteor[i].x, meteor[i].y, &img3, SRCPAINT);
}
}
}
void starsMove()
{
for (int i = 0; i < STAR_NUM; i++)
stars[i].move();
}
void meteorMove()
{
for (int i = 0; i < METEOR_NUM; i++)
{
meteor[i].move();
if (meteor[i].x > getwidth() || meteor[i].y > getheight())
{
initMeteor();
}
}
}
void initStars()
{
for (int i = 0; i < STAR_NUM; i++)
{
stars[i].init(rand() % getwidth(), rand() % getheight(), rand() % 3 + 1, rand() % 5, rand() % RGB(rand() % 256, rand() % 256, rand() % 256));
}
}
void initMeteor()
{
for (int i = 0; i < METEOR_NUM; i++)
{
meteor[i].init(rand() % getwidth(), rand() % getheight(), rand() % 20+1, rand() % 3);
}
}
void welcome()
{
setbkmode(TRANSPARENT);
settextstyle(40, 0, L"华文行楷");
int tx = (getwidth() - textwidth(L"少年何妨梦摘星,敢挽桑弓射玉衡")) / 2;
srand(time(NULL));
while (1)
{
if (_kbhit()) break;
cleardevice();
putimage(0, 0, &bk);
settextcolor(RGB(rand() % 256, rand() % 256, rand() % 256));
outtextxy(tx, 20, L"少年何妨梦摘星,敢挽桑弓射玉衡");
outtextxy(75, 100, L"追求自由是永恒的主题,");
outtextxy(500, 100, L"在星空中翱翔无羁。");
outtextxy(75, 150, L"仰望星空,心潮澎湃,");
outtextxy(500, 150, L"感叹人生短暂如流星,");
outtextxy(75, 200, L"渴望留下永恒的痕迹,");
outtextxy(500, 200, L"在宇宙中闪耀光芒。");
outtextxy(75, 250, L"梦想不止,追逐不息,");
outtextxy(500, 250, L"流星的瞬间也是美好,");
outtextxy(275, 300, L"梦想在心中永不消逝。");
Sleep(1000);
}
}
void loadImg()
{
loadimage(&bk, L"bk.jpg", 889, 500);
loadimage(&img4, L"img1.png", 40, 39);
loadimage(&img2, L"img2.png", 55, 62);
loadimage(&img3, L"img3.png", 10, 12);
}
void initProject()
{
initgraph(889, 500);
mciSendString(L"open黑夜问白天.mp3", 0, 0, 0);
mciSendString(L"play黑夜问白天.mp3", 0, 0, 0);
Sleep(5000);
}
音乐素材可以私我,是JJ的黑夜问白天呀。