#include
#include
#include
#define WHITE_BLACK ChangeColor(FOREGROUND_RED & FOREGROUND_GREEN & FOREGROUND_GREEN | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
void pos(int x_pos,int y_pos){
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x_pos;
pos.Y=y_pos;
SetConsoleCursorPosition(hOut,pos);
}
void ChangeColor(int color){
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hOut,color);
};
int main(){
int x_pos=0,y_pos=0;
int x_box=40,y_box=20;
int x_desity=10,y_desity=10;
system("color F7");
pos(20,10);
WHITE_BLACK;
printf("使用WSAD来控制人物,按任意键继续");
while(1){
switch(getch()){
case 'w':
if(y_pos==(y_box+1) && x_pos==x_box){
y_box--;
y_pos--;
}else{
y_pos--;
}
break;
case 's':
if(y_pos==(y_box-1) && x_pos==x_box){
y_box++;
y_pos++;
}else{
y_pos++;
}
break;
case 'a':
if(x_pos==(x_box+2) && y_pos==y_box){
x_box-=2;
x_pos-=2;
}else{
x_pos-=2;
}
break;
case 'd':
if(x_pos==(x_box-2) && y_pos==y_box){
x_box+=2;
x_pos+=2;
}else{
x_pos+=2;
}
break;
}
system("cls");
pos(x_pos,y_pos);
ChangeColor(FOREGROUND_RED | BACKGROUND_RED);
printf("囧");
pos(x_box,y_box);
ChangeColor(FOREGROUND_BLUE | BACKGROUND_BLUE);
printf("目");
if(x_box==x_desity && y_box==y_desity){
pos(20,5);
WHITE_BLACK;
printf("YOU WIN!");
pos(x_desity,y_desity);
ChangeColor(BACKGROUND_BLUE | FOREGROUND_BLUE);
printf("□");
break;
}else{
pos(x_desity,y_desity);
WHITE_BLACK;
printf("□");
}
pos(60,22);
WHITE_BLACK;
printf("%d,%d",x_pos,y_pos);
}
WHITE_BLACK;
getch();
return 0;
}