今天用C语言+EasyX写一个键盘控制角色移动的项目
原理其实和前面写的贴图的打字母游戏差不多
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
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