Native/C

Matrix Logo

aucd29 2013. 10. 2. 18:47
[code]
/***************************************************************************
*
*        Date        : Matrix Logo
*        Copyright    : http://www.winapi.co.kr
*    
***************************************************************************/

#include <Turboc.h>

// 아스키 코드 처리
#define MAX        1024
#define LEFT    75
#define RIGHT    77
#define UP        72
#define DOWN    80
#define ESC        27

struct Signal
{
    BOOL exist;        // 신호의 존재 여부
    char ch;        // 출력할 문자
    int x,y;        // 현재 좌표
    int distance;    // 이동할 거리
    int nFrame;        // 속도
    int nStay;        // 속도에 대한 카운트
};

Signal S[MAX];

void main()
{
    int i;
    int ch;
    int freq=15;    // 기본 빈도
    int frame=200;    // 기본 속도

    clrscr();
    gotoxy(0,23);
    printf("매트릭스 흉내내기. 상하:속도 증감, 좌우:빈도 증감, ESC:종료");

    randomize();
    for (;;)
    {
        gotoxy(60,23);
        printf("속도:%d, 빈도:%d    ",frame,freq);

        // 키 입력 처리
        if (kbhit()) {
            ch=getch();
            if (ch == 0xE0) {
                ch=getch();
                switch (ch) {
                case UP:
                    frame=min(frame+10,1000);
                    break;
                case DOWN:
                    frame=max(frame-10,10);
                    break;
                case RIGHT:
                    freq=max(freq-1,1);
                    break;
                case LEFT:
                    freq=min(freq+1,30);
                    break;
                }
            } else {
                if (ch == ESC) {
                    return;
                }
            }
        }

        // 새로운 신호 생성

        if (random(freq) == 0) {        // 빈도의 숫자가 작으면 작을수로 0이될 확률이 높아진다.
            for (i=0;i<MAX;i++) {
                if (S[i].exist == FALSE) {
                    S[i].exist=TRUE;
                    S[i].ch=random('Z'-'A'+1)+'A';
                    S[i].x=random(80);                // 1~80 까지의 x값
                    S[i].y=0;
                    S[i].distance=random(14)+9;
                    S[i].nFrame=S[i].nStay=random(15)+5;
                    break;
                }
            }
        }

        // 주기가 다 된 신호 이동 및 제거 처리

        for (i=0;i<MAX;i++) {
            if (S[i].exist == FALSE) continue;
            if (--S[i].nStay == 0) {
                S[i].nStay=S[i].nFrame;
                gotoxy(S[i].x,S[i].y);putch(' ');
                if (++S[i].y < S[i].distance) {
                    gotoxy(S[i].x,S[i].y);putch(S[i].ch);
                } else {
                    S[i].exist=FALSE;
                }
            }
        }
        delay(1000/frame);
    }
}
[/code]