Santa Claus interface (animation effects)

Christmas next week! ! ! Here is a Christmas interface animation for everyone in C language (isn’t this more fragrant than a Christmas tree)

First look at the renderings

Development environment: vs2019+easyx (need to install graphics library)

full source code

#include<stdio.h>
#include<time.h>
//#include<easyx.h>	
#include"tool.h"

#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")


/*

* 开发环境:vs2019+easyx
*/
//图片相关操作
IMAGE imgLand;
IMAGE imgTree[10];	//圣诞树
IMAGE imgOldman;	//老人
IMAGE imgSnow[2];
bool Timer(clock_t ms, int id);
//加载资源
void loadResource()
{
	//加载图片
	loadimage(&imgLand, "Resource/land.png", getwidth(), 290);

	for (int i = 0; i < 10; i++)
	{
		char imgFile[50] = { 0 };
		sprintf_s(imgFile, "Resource/mtree/0000%d.png", i);
		loadimage(imgTree + i, imgFile, 300, 300);
	}

	loadimage(&imgOldman, "Resource/oldman.png");

	loadimage(imgSnow + 0, "Resource/snow.png");
	loadimage(imgSnow + 1, "Resource/snow1.png", 23, 32);
}

//来一个精灵结构体
typedef struct
{
	int x;
	int y;
	int w;
	int h;
	int frame;		//当前帧
	int maxFrame;	//总帧数
	int speed;		//速度
}Sprite;
void sprite_init(Sprite* sprite, int x, int y, int maxFrame,int w,int h)
{
	sprite->x = x;
	sprite->y = y;
	sprite->frame = 0;
	sprite->maxFrame = maxFrame;
	sprite->w = w;
	sprite->h = h;
	sprite->speed = 1;
}
void sprite_runAnimation(Sprite* sprite)
{
	//没写一步都要考虑一下是否有隐藏的bug 
	if(sprite->maxFrame != 0)
		sprite->frame = (sprite->frame + 1) % sprite->maxFrame;
}
void sprite_moveBy(Sprite* sprite, int dx, int dy)
{
	sprite->x += dx;
	sprite->y += dy;
}

Sprite tree;
Sprite oldMan;
Sprite snow[50];	//雪花


//初始化
void init()
{
	loadResource();
	//初始化圣诞树数据
	sprite_init(&tree, 530, 430, 10, imgTree->getwidth(), imgTree->getheight());
	//初始化老人数据
	sprite_init(&oldMan, 700, 600, 7, 220, 154);
	//初始化雪花
	for (int i = 0; i < 50; i++)
	{
		//随机生成雪花的坐标 rand() 
		sprite_init(snow + i, rand() % getwidth(), rand() % getheight(), 11, 23, 32);
		//让雪花的当前帧不一样
		if (snow[i].maxFrame)
			snow[i].frame = rand() % snow[i].maxFrame;
		snow[i].speed = rand() % 5 + 1;
	}
}
//绘制
void draw()
{
	//输出底下的地面
	drawImg(0, 500, &imgLand);
	//圣诞树动画
	drawImg(tree.x, tree.y, imgTree + tree.frame);
	//圣诞老人滑雪动画
	drawImg(oldMan.x, oldMan.y, oldMan.w, oldMan.h, &imgOldman, oldMan.frame *220, 0);
	//绘制雪花
	for (int i = 0; i < 50; i++)
	{
		drawImg(snow[i].x, snow[i].y, snow[i].w, snow[i].h, imgSnow + 0, snow[i].frame* snow[i].w, 0);
	}

}
//数据更新
void update()
{
	sprite_runAnimation(&tree);
	sprite_runAnimation(&oldMan);
	if (Timer(100, 0))
	{
		for (int i = 0; i < 50; i++)
		{
			sprite_runAnimation(snow + i);
		}
	}
	if (Timer(20, 1))
	{
		for (int i = 0; i < 50; i++)
		{
			//雪花下落
			sprite_moveBy(snow + i, 0, snow[i].speed);
			//是否超出边界
			if (snow[i].y > getheight())
			{
				snow[i].y = snow[i].h;
			}
		}
	}
	//Sleep(200);


	//让圣诞老人溜达一下
	sprite_moveBy(&oldMan, -oldMan.speed, 0);
	if (oldMan.x+oldMan.w <= 0)
	{
		oldMan.x = getwidth();
	}

	
}
//让程序更丝滑:定时器
bool Timer(clock_t ms, int id)
{
	static clock_t beg[5] = { 0 };
	clock_t end = clock();
	if (end - beg[id] >= ms)
	{
		beg[id] = end;
		return true;
	}
	
	return false;
}

int main()
{
	//界面,绘图,控制台
	//创建窗口
	initgraph(1024, 768, EW_SHOWCONSOLE);
	init();

	//播放音乐
	mciSendString("open Resource/song.mp3 alias bgm", NULL, 0, NULL);
	mciSendString("play bgm repeat", NULL, 0, NULL);

	BeginBatchDraw();	//开始双缓冲绘图
	while (true)
	{
		cleardevice();	//清屏
		draw();
		FlushBatchDraw();	//刷新
		update();
	}
	EndBatchDraw();

	getchar();
	return 0;
}

 END

If you want source code and esayx, please add my c/c++ programming data exchange Q group: 214574728

Guess you like

Origin blog.csdn.net/C214574728/article/details/128255873