同样的,我也是采用EasyX库
先准备好素材(该素材是从RPG maker MV 中找的几张)
#include
#include
//像素宽度
int width = 1000;
//像素高度
int high = 740;
//创建图片对象(背景)
IMAGE BG;
//创建图片对象(人物)
IMAGE AC1,AC2;
//动作编号
int id = 0;
//移动方向
char direction;
//人物坐标
int p_x = width/2;
int p_y = high/2;
int main()
{
//设置窗口大小
initgraph(width,high);
//加载背景图片
loadimage(&BG,"background.png");
//绘制背景图片
putimage(0,0,&BG);
//加载人物图片
loadimage(&AC1,"actor1.jpg");
loadimage(&AC2,"actor2.jpg");
//绘制人物图片
putimage(p_x,p_y,50,50,&AC2,0,0,NOTSRCERASE);
putimage(p_x,p_y,50,50,&AC1,0,0,SRCINVERT);
direction = getch();
BeginBatchDraw();
while(1)
{
if (kbhit())
{
direction = getch();
}
else if(direction =='a')
{
p_x-=8;
if (p_x<=0)
{
p_x=0;
}
//清空绘制区域
clearrectangle(0,0,width,high);
//绘制背景图片
putimage(0,0,&BG);
//绘制人物图片
putimage(p_x,p_y,50,50,&AC2,50*id,50,NOTSRCERASE);
putimage(p_x,p_y,50,50,&AC1,50*id,50,SRCINVERT);
id++;
Sleep(30);
if (id==3)
{
id = 0;
}
}
else if(direction =='d')
{
p_x+=8;
if (p_x>=width-50)
{
p_x = width-50;
}
//清空绘制区域
clearrectangle(0,0,width,high);
//绘制背景图片
putimage(0,0,&BG);
//绘制人物图片
putimage(p_x,p_y,50,50,&AC2,50*id,100,NOTSRCERASE);
putimage(p_x,p_y,50,50,&AC1,50*id,100,SRCINVERT);
id++;
Sleep(30);
if(id==3)
{
id = 0;
}
}
else if(direction =='w')
{
p_y-=8;
if (p_y<=0)
{
p_y=0;
}
//清空绘制区域
clearrectangle(0,0,width,high);
//绘制背景图片
putimage(0,0,&BG);
//绘制人物图片
putimage(p_x,p_y,50,50,&AC2,50*id,150,NOTSRCERASE);
putimage(p_x,p_y,50,50,&AC1,50*id,150,SRCINVERT);
id++;
Sleep(30);
if(id==3)
{
id = 0;
}
}
else if(direction =='s')
{
p_y+=8;
if (p_y>=high-50)
{
p_y=high-50;
}
//清空绘制区域
clearrectangle(0,0,width,high);
//绘制背景图片
putimage(0,0,&BG);
//绘制人物图片
putimage(p_x,p_y,50,50,&AC2,50*id,0,NOTSRCERASE);
putimage(p_x,p_y,50,50,&AC1,50*id,0,SRCINVERT);
id++;
Sleep(30);
if(id==3)
{
id = 0;
}
}
FlushBatchDraw();
}
EndBatchDraw();
//关闭窗口
closegraph();
return 0;
}
#include
#include
#include
//像素宽度
int width = 1000;
//像素高度
int high = 740;
//创建图片对象(背景)
IMAGE BG;
//创建图片对象(人物)
IMAGE AC1,AC2,BC1,BC2;
//动作编号
int id = 0;
int id2 = 0;
//方向编号
int d_id;
int d_id2;
//人物坐标
int p_x = width/4;
int p_y = high/2;
//人物2坐标
int p2_x = width/4*3;
int p2_y = high/2;
int main()
{
//设置窗口大小
initgraph(width,high);
//加载背景图片
loadimage(&BG,"background.png");
//绘制背景图片
putimage(0,0,&BG);
//加载人物图片
loadimage(&AC1,"actor1.jpg");
loadimage(&AC2,"actor2.jpg");
//绘制人物图片
putimage(p_x,p_y,50,50,&AC2,0,0,NOTSRCERASE);
putimage(p_x,p_y,50,50,&AC1,0,0,SRCINVERT);
//加载人物2图片
loadimage(&BC1,"girl1.jpg");
loadimage(&BC2,"girl2.jpg");
//绘制人物2图片
putimage(p2_x,p2_y,50,50,&BC2,0,0,NOTSRCERASE);
putimage(p2_x,p2_y,50,50,&BC1,0,0,SRCINVERT);
BeginBatchDraw();
while(1)
{
if(GetAsyncKeyState(0x41) & 0x8000) //a
{
d_id=1;
p_x-=8;
if (p_x<=0)
{
p_x=0;
}
id++;
if (id==3)
{
id = 0;
}
}
if(GetAsyncKeyState(0x44) & 0x8000) //d
{
d_id=2;
p_x+=8;
if (p_x>=width-50)
{
p_x = width-50;
}
id++;
if(id==3)
{
id = 0;
}
}
if(GetAsyncKeyState(0x57) & 0x8000) //w
{
d_id=3;
p_y-=8;
if (p_y<=0)
{
p_y=0;
}
id++;
if(id==3)
{
id = 0;
}
}
if(GetAsyncKeyState(0x53) & 0x8000) //s
{
d_id=0;
p_y+=8;
if (p_y>=high-50)
{
p_y=high-50;
}
id++;
if(id==3)
{
id = 0;
}
}
if(GetAsyncKeyState(VK_LEFT) & 0x8000) //左键
{
d_id2=1;
p2_x-=8;
if (p2_x<=0)
{
p2_x=0;
}
id2++;
if(id2==3)
{
id2 = 0;
}
}
if(GetAsyncKeyState(VK_RIGHT) & 0x8000) //右键
{
d_id2=2;
p2_x+=8;
if (p2_x>=width-50)
{
p2_x=width-50;
}
id2++;
if(id2==3)
{
id2 = 0;
}
}
if(GetAsyncKeyState(VK_UP) & 0x8000) //上键
{
d_id2=3;
p2_y-=8;
if (p2_y<=0)
{
p2_y=0;
}
id2++;
if(id2==3)
{
id2 = 0;
}
}
if(GetAsyncKeyState(VK_DOWN) & 0x8000) //下键
{
d_id2=0;
p2_y+=8;
if (p2_y>=high-50)
{
p2_y=high-50;
}
id2++;
if(id2==3)
{
id2 = 0;
}
}
//清空绘制区域
clearrectangle(0,0,width,high);
//绘制背景图片
putimage(0,0,&BG);
//绘制人物图片
putimage(p_x,p_y,50,50,&AC2,50*id,50*d_id,NOTSRCERASE);
putimage(p_x,p_y,50,50,&AC1,50*id,50*d_id,SRCINVERT);
//绘制人物2图片
putimage(p2_x,p2_y,50,50,&BC2,50*id2,50*d_id2,NOTSRCERASE);
putimage(p2_x,p2_y,50,50,&BC1,50*id2,50*d_id2,SRCINVERT);
Sleep(30);
FlushBatchDraw();
}
EndBatchDraw();
//关闭窗口
closegraph();
return 0;
}
可以给两个人物加上冲突判断,是的人物不会叠加显示。
这些就交给大家增加了