C语言+EasyX编写键盘控制角色移动

今天用C语言+EasyX写一个键盘控制角色移动的项目
原理其实和前面写的贴图的打字母游戏差不多
#include
#include
#include
IMAGE background, boy1, boy2;
int num = 0;
int x0 = 0;
int y0 = 450;
void InitInstance()
{
	loadimage(&background, "background.jpg");//加载游戏背景
	loadimage(&boy1, "boy1.bmp");//背景图
	loadimage(&boy2, "boy2.bmp");//屏蔽图
}
void Interface()//初始化基本数据
{
	initgraph(1024, 768);//初始化画布大小
	putimage(0, 0, &background);
	putimage(x0, 450, 70, 124, &boy2, 70 * num, 124 * 2, SRCAND);//角色初始位置
	putimage(x0, 450, 70, 124, &boy1, 70 * num, 124 * 2, SRCPAINT);
}
void key_Down()
{
	char key = _getch();
	//Sleep(30);
		switch (key)//获取按键
		{
			//向上
		case 72:
		case 'w':
		case 'W':
			putimage(0, 0, &background);
			putimage(x0, y0, 70, 124, &boy2, 70 * num, 124 * 3, SRCAND);
			putimage(x0, y0, 70, 124, &boy1, 70 * num, 124 * 3, SRCPAINT);
			num++;
			if (num == 4)
				num = 0;
			y0 -= 7;
			if (y0 <= 0)
			{
				y0 = 0;
			}
			break;
			//向下
		case 80:
		case 's':
		case 'S':
			putimage(0, 0, &background);
			putimage(x0, y0, 70, 124, &boy2, 70 * num, 0, SRCAND);
			putimage(x0, y0, 70, 124, &boy1, 70 * num, 0, SRCPAINT);
			num++;
			if (num == 4)
				num = 0;
			y0 += 7;
			if (y0 >= 760)
			{
				y0 = 760;
			}
			break;
			//向右
		case 77:
		case 'd':
		case 'D':
			putimage(0, 0, &background);
			putimage(x0, y0, 70, 124, &boy2, 70 * num, 124 * 2, SRCAND);
			putimage(x0, y0, 70, 124, &boy1, 70 * num, 124 * 2, SRCPAINT);
			num++;
			if (num == 4)
				num = 0;
			x0 += 7;
			if (x0 >= 1010)
			{
				x0 = 1010;
			}
			break;
			//向左
		case 75:
		case 'a':
		case 'A':
			putimage(0, 0, &background);
			putimage(x0, y0, 70, 124, &boy2, 70 * num, 124, SRCAND);
			putimage(x0, y0, 70, 124, &boy1, 70 * num, 124, SRCPAINT);
			num++;
			if (num == 4)
				num = 0;
			x0 -= 7;
			if (x0 <= 0)
			{
				x0 = 0;
			}
					break;
		}
}
int main()
{
	InitInstance();
	Interface();
	while (1)
	{
		key_Down();
	}
	getchar();
	closegraph();
	return 0;
}

C语言+EasyX编写键盘控制角色移动_第1张图片

C语言+EasyX编写键盘控制角色移动_第2张图片     C语言+EasyX编写键盘控制角色移动_第3张图片


效果图:


你可能感兴趣的:(C语言+EasyX编写键盘控制角色移动)