#include <stdio.h>
#include <windows.h>
#include <conio.h>
void gotoxy(HANDLE hout,int x,int y);
int main()
{
char input;
int i,j,x=2,y=1;
HANDLE hout;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
gotoxy(hout,2,2);
SetConsoleTextAttribute(hout,BACKGROUND_RED | BACKGROUND_INTENSITY);
printf("
");
while (1)
{
SetConsoleTextAttribute(hout,FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoxy(hout,0,0);
printf("亲,你瞧,有个红方块,用wasd可以控制移动哦!\n");
SetConsoleTextAttribute(hout,BACKGROUND_GREEN | BACKGROUND_INTENSITY);
for (i=1,j=0;j<=16;j++)
{
gotoxy(hout,j*2,i);
printf("
");
}
for (i=10,j=0;j<=16;j++)
{
gotoxy(hout,j*2,i);
printf("
");
}
for (i=2,j=0;i<=10;i++)
{
gotoxy(hout,j,i);
printf("
");
}
for (i=2,j=32;i<=10;i++)
{
gotoxy(hout,j,i);
printf("
");
}
input=getch();
switch (input)
{
case 'w' :{if (x>2)
x--;};break;
case 'a' :{if (y>1)
y--;};break;
case 's' :{if (x<9)
x++;};break;
case 'd' :{if (y<15) y++;};break;
default: break;
}
SetConsoleTextAttribute(hout,0);
system("cls");
gotoxy(hout,y*2,x);
SetConsoleTextAttribute(hout,BACKGROUND_RED | BACKGROUND_INTENSITY);
printf("
");
}
return 0;
}
void gotoxy(HANDLE hout,int x,int y)
{
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(hout,pos);
}
你运行一下试一试