编写c语言程序,键盘输入向上键笑脸向上移动,向下它就向下,上下左...

编写c语言程序,键盘输入向上键笑脸向上移动,向下它就向下,上下左右都能控制
不要使用goto语句,让初学c语言的看懂。

第1个回答  2012-03-10
#include "stdio.h"
main()
{
int i=20; /* 横坐标 */
int j=15; /* 纵坐标 */
int c[4]={0};
char t=1;
int d[4]={0};
/* int e[4]={0}; */
int a;
int b;

while(t)
{
clrscr();
printf("Please input '0' is over\n");
printf("%c",2);
gotoxy(i,j); */
*c=getch();
if(*c==0x4b)
{
i=i==2?79:i-1;
gotoxy(i,j);
printf("%c",2);
gotoxy(i,j);

gotoxy(i+1,j);
printf("%c",2);
gotoxy(i,j);
gotoxy(i+2,j);
printf("%c",2);
gotoxy(i,j);
getch();

*d=gatch();
/* if(*d==0x48)
{
a=i+1;
b=j+1;
gotoxy(a,b);
printf("%c",2);
gotoxy(a-1,b-1);
getch();
} */
if(*d==0x50)
{
gotoxy(i+1,j);
printf("%c",2);
gotoxy(i,j+1);
getch();
}
/* if(*d==0x48)
{

gotoxy()

} */
}
else if(*c==0x4d)
{
i=i==79?2:i+1;
gotoxy(i,j);
printf("%c",2);
gotoxy(i,j);
a=i-1;
b=j;
gotoxy(a,b);
printf("%c",2);
gotoxy(a+1,b);
a=i-2;
b=j;
gotoxy(a,b);
printf("%c",2);
gotoxy(a+2,b);
getch();
}
else if(*c==0x48)
{
j=j==3?23:j-1;
gotoxy(i,j);
printf("%c",2);
gotoxy(i,j);
a=i;
b=j+1;
gotoxy(a,b);
printf("%c",2);
gotoxy(a,b-1);
a=i;
b=j+2;
gotoxy(a,b);
printf("%c",2);
gotoxy(a,b-2);
getch();
}

else if(*c==0x50)
{
j=j==23?3:j+1;
gotoxy(i,j);
printf("%c",2);
gotoxy(i,j);
a=i;
b=j-1;
gotoxy(a,b);
printf("%c",2);
gotoxy(a,b+1);
a=i;
b=j-2;
gotoxy(a,b);
printf("%c",2);
gotoxy(a,b+2);
getch();
}

else if(*c==0x20)
i--,j--;
else if(*c==0x30) clrscr(),t=0;

}

}
\\这个是开始学C的时候编写的,TC编译器上可以,不支持VC++
第2个回答  2012-03-10
#include<stdio.h>
#include<windows.h>
#include<conio.h>
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define ESC 27
int x,y;
void WhereXY(int *x,int *y);
void GoToXY(int x,int y);
void PrintPic(void);
void ShowPic(void);
void ClearPic(void);

void main()
{
PrintPic();
while(1)
{
ShowPic();
}
}

void PrintPic(void)
{
printf("^_^");
}
void ClearPic(void)
{
printf("\b\b\b");
printf(" ");
printf("\b\b\b");
}
void ShowPic(void)
{
char key;
key = getch();
ClearPic();
WhereXY(&x,&y);
switch(key)
{
case UP:
if(y > 1 )
GoToXY(x,--y);
break;
case DOWN:
GoToXY(x,++y);
break;
case LEFT:
if(x > 0)
GoToXY(--x,y);
break;
case RIGHT:
GoToXY(++x,y);
break;
case ESC:
exit(0);
default:
break;
}
PrintPic();
}
void WhereXY(int *x,int *y)
{
HANDLE h1;
CONSOLE_SCREEN_BUFFER_INFO scrInfo;
h1=GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(h1,&scrInfo);
*x=scrInfo.dwCursorPosition.X;
*y=scrInfo.dwCursorPosition.Y;
}
void GoToXY(int x,int y)
{
HANDLE h1;
COORD pos={x,y};
h1=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h1,pos);
}
//VC++下可直接运行
相似回答