圣诞节来临的时候,最大的愿望是什么呢?
是不是想祝福自己心爱的女孩圣诞快乐呢,今天小基诺将带领大家,用C语言绘制一个属于自己的圣诞贺卡,送给自己心爱的女孩吧!
头文件准备作:
//头文件准备
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<Windows.h>
#include<time.h>
#include<conio.h>
#include<math.h>
#include<vector>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define PI 3.14159265
#define MAXSnow 2000 // 雪花总数
#define SCREEN_W 1200 // 窗口宽度
#define SCREEN_H 700 // 窗口高度
#define SOWN_RADIO 3 // 雪花大小
#define SNOW_SLEEP 0 // 雪花下落速度
using namespace std;
IMAGE background;
准备就绪后,我们先设置一个初始界面吧!!:
void welcome() {
settextcolor(RGB(255, 0, 128));
for (int i = 0; i < 100; i++) {
int x = 600 + int(180 * sin(PI * 2 * i / 60));
int y = 200 + int(180 * cos(PI * 2 * i / 60));
cleardevice();
settextstyle(i, 0, L"华文行楷");
outtextxy(x - 80, y + 100, L"祝xxx同学");
outtextxy(x - 10, y + 200, L"圣诞快乐");
Sleep(65);
}
Sleep(200);
cleardevice();
settextcolor(RGB(255, 0, 0));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 50, L"把最深的思念刻在雪花上");
Sleep(2000);
settextcolor(RGB(255, 128, 64));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 100, L"把最沉的牵挂放到清风里");
Sleep(2000);
settextcolor(RGB(255, 255, 0));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 150, L"把最热的内心放到歌声里");
Sleep(2000);
settextcolor(RGB(0, 255, 64));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 200, L"把最真的祝福写成代码:");
Sleep(2000);
settextcolor(RGB(128, 255, 255));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 250, L"祝你平安夜幸福快乐");
Sleep(2000);
settextcolor(RGB(0, 0, 255));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 300, L"圣诞节欢乐无边");
Sleep(2000);
settextcolor(RGB(128, 0, 255));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 350, L"新年好运连连");
Sleep(2000);
settextcolor(RGB(255, 0, 255));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 400, L"愿你平安如意,圣诞快乐!");
Sleep(2000);
settextcolor(RGB(164, 158, 255));
settextstyle(30, 0, L"华文行楷");
outtextxy(350, 500, L"---------Jero Youken");
Sleep(5000);
settextcolor(RGB(255, 0, 255));
settextstyle(30, 0, L"仿宋");
outtextxy(50, 650, L"请按任意键继续————");
getchar();
_getch();
cleardevice();
}
接下来,我们的祝福送到了,是不是该绘制一幅美丽的动态图呢,不过小基诺有点懒惰,对于圣诞树,圣诞老人,代码实现可能过于复杂,那就直接贴一张背景图上去吧。不过,我们可以用C语言里面的图形库来生成雪花哦!
struct Snow//定义雪花类
{
double x; //坐标
int y;
double step; //速度
int color; //颜色
int radui; //大小
};
Snow snow[MAXSnow];//保存所有雪花
void Initsnow(int i) {//初始化雪花
snow[i].x = rand() % SCREEN_W;
snow[i].y = rand() % SCREEN_H;
snow[i].radui = rand() % SOWN_RADIO + 1;
snow[i].step = (rand() % 5000) / 1000.0 + 1;
snow[i].color = (int)(snow[i].step * 255 / 6.0 + 0.5); // 速度越快,颜色越亮
snow[i].color = RGB(snow[i].color, snow[i].color, snow[i].color);
}
void movesnow(int i)//控制雪花的速度
{
// 计算新位置
snow[i].y += snow[i].step;
if (snow[i].y > SCREEN_H) Initsnow(i);
// 画新雪花
setfillcolor(snow[i].color);
setlinecolor(snow[i].color);
solidcircle((int)snow[i].x, snow[i].y, snow[i].radui);
}
所有准备工作就绪后,接下来,我们应该设置主函数了。。。
int main() {
srand((unsigned)time(NULL));//播撒随机数种子
initgraph(SCREEN_W, SCREEN_H);//图形化初始界面
mciSendString(L"open 1.mp3 alias music", 0, 0, 0);
mciSendString(L"play music", 0, 0, 0);//打开音频,播放圣诞歌曲
loadimage(&background, L"圣诞节.jpg", SCREEN_W, SCREEN_H, 1);//初始化图片
welcome();//初始界面送女孩祝福
putimage(0, 0, &background);//贴上背景图
for (int i = 0; i < MAXSnow; i++)//绘制雪花
{
Initsnow(i);
snow[i].x = rand() % SCREEN_W;
}
BeginBatchDraw();//防止闪屏
while (!_kbhit())
{
cleardevice();
putimage(0, 0, &background);
for (int i = 0; i < MAXSnow; i++)
{
movesnow(i);
}
FlushBatchDraw();
Sleep(SNOW_SLEEP);
}
EndBatchDraw();
while (1);
closegraph();
return 0;
}
OK,一个圣诞贺卡就写完了,完整代码如下:
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<Windows.h>
#include<time.h>
#include<conio.h>
#include<math.h>
#include<vector>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define PI 3.14159265
#define MAXSnow 2000 // 雪花总数
#define SCREEN_W 1200 // 窗口宽度
#define SCREEN_H 700 // 窗口高度
#define SOWN_RADIO 3 // 雪花大小
#define SNOW_SLEEP 0 // 雪花下落速度
using namespace std;
IMAGE background;
void welcome() {
settextcolor(RGB(255, 0, 128));
for (int i = 0; i < 100; i++) {
int x = 600 + int(180 * sin(PI * 2 * i / 60));
int y = 200 + int(180 * cos(PI * 2 * i / 60));
cleardevice();
settextstyle(i, 0, L"华文行楷");
outtextxy(x - 80, y + 100, L"祝xxx同学");
outtextxy(x - 10, y + 200, L"圣诞快乐");
Sleep(65);
}
Sleep(200);
cleardevice();
settextcolor(RGB(255, 0, 0));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 50, L"把最深的思念刻在雪花上");
Sleep(2000);
settextcolor(RGB(255, 128, 64));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 100, L"把最沉的牵挂放到清风里");
Sleep(2000);
settextcolor(RGB(255, 255, 0));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 150, L"把最热的内心放到歌声里");
Sleep(2000);
settextcolor(RGB(0, 255, 64));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 200, L"把最真的祝福写成代码:");
Sleep(2000);
settextcolor(RGB(128, 255, 255));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 250, L"祝你平安夜幸福快乐");
Sleep(2000);
settextcolor(RGB(0, 0, 255));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 300, L"圣诞节欢乐无边");
Sleep(2000);
settextcolor(RGB(128, 0, 255));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 350, L"新年好运连连");
Sleep(2000);
settextcolor(RGB(255, 0, 255));
settextstyle(30, 0, L"仿宋");
outtextxy(200, 400, L"愿你平安如意,圣诞快乐!");
Sleep(2000);
settextcolor(RGB(164, 158, 255));
settextstyle(30, 0, L"华文行楷");
outtextxy(350, 500, L"---------Jero Youken");
Sleep(5000);
settextcolor(RGB(255, 0, 255));
settextstyle(30, 0, L"仿宋");
outtextxy(50, 650, L"请按任意键继续————");
getchar();
_getch();
cleardevice();
}
struct Snow
{
double x; //坐标
int y;
double step; //速度
int color; //颜色
int radui; //大小
};
Snow snow[MAXSnow];//保存所有雪花
void Initsnow(int i) {
snow[i].x = rand() % SCREEN_W;
snow[i].y = rand() % SCREEN_H;
snow[i].radui = rand() % SOWN_RADIO + 1;
snow[i].step = (rand() % 5000) / 1000.0 + 1;
snow[i].color = (int)(snow[i].step * 255 / 6.0 + 0.5); // 速度越快,颜色越亮
snow[i].color = RGB(snow[i].color, snow[i].color, snow[i].color);
}
void movesnow(int i)
{
// 计算新位置
snow[i].y += snow[i].step;
if (snow[i].y > SCREEN_H) Initsnow(i);
// 画新雪花
setfillcolor(snow[i].color);
setlinecolor(snow[i].color);
solidcircle((int)snow[i].x, snow[i].y, snow[i].radui);
}
int main() {
srand((unsigned)time(NULL));
initgraph(SCREEN_W, SCREEN_H);
mciSendString(L"open 1.mp3 alias music", 0, 0, 0);
mciSendString(L"play music", 0, 0, 0);
loadimage(&background, L"圣诞节.jpg", SCREEN_W, SCREEN_H, 1);
welcome();
putimage(0, 0, &background);
for (int i = 0; i < MAXSnow; i++)
{
Initsnow(i);
snow[i].x = rand() % SCREEN_W;
}
BeginBatchDraw();
while (!_kbhit())
{
cleardevice();
putimage(0, 0, &background);
for (int i = 0; i < MAXSnow; i++)
{
movesnow(i);
}
FlushBatchDraw();
Sleep(SNOW_SLEEP);
}
EndBatchDraw();
while (1);
closegraph();
return 0;
}
快按照基诺教大家的步骤去送给自己心中的那个女孩吧!!!
关注并私信基诺可获得背景图片和音乐素材哦