키입력 숙제(미완성;;)

2007.06.05 15:40

전민재 Views:6756

잠시 올려놓겠습니다..ㅠ 아 어려워
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <windows.h>

#define X 10
#define Y 10
#define UP 0x1048
#define DOWN 0x1050
#define LEFT 0x104b
#define RIGHT 0x104d

void gotoxy(int x, int y);
void sleep(double sec);

void start();
void wall();
void block(int b);
int input();
int move(int m);

int x1, y1, mov=0;


void main()
{
        start();
        wall();
        block(0);

        while(input())
                sleep(0.01);
        gotoxy(0, Y);
}

void gotoxy(int x, int y)
{
        COORD Cur={x,y};
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Cur);
}

void sleep(double sec)
{
        clock_t goal;
        goal=(long)(sec*CLOCKS_PER_SEC)+clock();

        while(goal>clock());
}

int input()
{
        int a;
        a=getch();
        
        if(a==0xE0){
                a=getch()|0x1000;
        }
        
        switch(a){
        case UP:
        case DOWN:
        case LEFT:
        case RIGHT:
                {
                        move(a);
                }

        case 0x1B:
                {
                        //turn();
                        break;
                }
        }
        return 1;
}

void start()
{
        x1=X/2;
        y1=Y/2;
}

void wall()
{
        int a;
        for(a=0; a<X+2; a++)
                printf("#");
        for(a=0; a<Y+2; a++){
                gotoxy(0, a+1);
                printf("#");
                gotoxy(X+1, a+1);
                printf("#");
        }
        gotoxy(0, Y+2);
        for(a=0; a<X+2; a++)
                printf("#");
        gotoxy(0, Y+3);
        printf("방향키로 블럭을 움직인다. esc키를 누르면 되돌아간다.n");

}

void block(int b)
{
        gotoxy(x1, y1);
        printf(!b?"*":" ");
}

int move(int m)
{
        block(1);
        switch(m){
        case UP: y1--;
                break;
        case DOWN: y1++;
                break;
        case LEFT : x1--;
                break;
        case RIGHT : x1++;
                break;
        }
        block(0);
        
        return 0;
}

Comment 2