最近在学C语言,今天做了一个小案例,觉得蛮有意思的,新手做着也许会很有成就感,分享下
主要实现了:打印一个笑脸,通过上下左右控制,使用的环境是VS2015。
中间也碰到了一些问题,就是传统的 gotoxy(),cls()等函数只能在turbo c 中使用,在网上查了下,gotoxy()可以使用windows的接口实现,cls()一样可以用system("cls")实现。
**很多经典的东西真的可以很优秀!**下面是代码
#include "stdafx.h"
#include <Windows.h>
#include <conio.h>
#include <dos.h>
#define UP '8'
#define DOWN '2'
#define LEFT '4'
#define RIGHT '6'
void key_listen(int x, int y);
void facePrint(int x, int y);
void gotoxy(short x, short y)
{
COORD c = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
int main()
{
int x = 10, y = 10;
gotoxy(x,y);
printf("%c", 2);
key_listen(x, y);
_getch();
return 0;
}
void facePrint(int x, int y)
{
system("cls");
gotoxy(x, y);
printf("%c", 2);
}
void key_listen(int x,int y)
{
char c;
while (( c = _getch()) != EOF)
{
switch (c)
{
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
default:
break;
}
facePrint(x, y);
}
}