回复 楼主 九天冥盟
#include<iostream>
#include<graphics.h>
#include<conio.h>
const short W=480;const short H=500;
class Character
{
private://坐标及大小
short x;
short y;
short height;
short width;
char ch[2];
public:
Character()
{
x=0;y=0;height=30;width=35;strcpy(ch," ");
}
Character(short input_x,short input_y,short input_h,short input_w,char letter)
{
x=input_x;y=input_y;
if(input_x<0) x=0;
if(input_y<0) y=0;
if(input_x>W-input_w) x=W-input_w;
if(input_y>H-input_h) x=H-input_h;
height=input_h;width=input_w;
ch[0]=letter;ch[1]='\0';
}
void Draw()
{
setbkcolor(RED);
settextstyle(width,height,_T("Courier"));
outtextxy(x,y,ch);
}
void Move()
{
char k;
while(true)
{
k=getch();
if(k==' ') break;//按空格退出函数
else
{
if(k=='A'||k=='a')
{
setbkcolor(BLACK);outtextxy(x,y," ");x-=10;
Draw();
}
if(k=='D'||k=='d')
{
setbkcolor(BLACK);outtextxy(x,y," ");x+=10;
Draw();
}
if(k=='W'||k=='w')
{
setbkcolor(BLACK);outtextxy(x,y," ");y-=10;
Draw();
}
if(k=='S'||k=='s')
{
setbkcolor(BLACK);outtextxy(x,y," ");y+=10;
Draw();
}
}
}
}
};
int main()
{
initgraph(W,H);
Character c(100,70,25,20,'D');
c.Draw();
c.Move();
closegraph();
return 0;
}