적의 총알이 발사가 안됩니다.


include

include

include

include

define ESC 27

define EXTENDASCII -32

define MAXBULLET 3

define MAXENEMY 15

define MAXENEMYBULLET 20

define HAVE_STDINT_H 1

void SetColor(int color);

void LoadingStage(); void HeightLine(); void EnemyBulletAction(); void DrawInfo(); void PlayerMoveAction(); void PlayerBulletAction(); void PlayerMissileAction(); void DrawEnemy(); void EnemyMoveAction(); void MoveEnemyBullet(); void EnemyCrash(); void PlayerCrash(); void DrawEnemyBullet(); enum { BLACK, DARK_BLUE, DARK_GREEN, DARK_SKYBLUE, DARK_RED, DARK_VOILET, DARK_YELLOW, GRAY, DARK_GRAY, BLUE, GREEN, SKYBLUE, RED, VIOLET, YELLOW, WHITE, };

int px = 30; int vx = -1, by; int mx = -1, my; int Score = 0;

struct { BOOL exist; int x, y; } Bullet[MAXBULLET];

struct { BOOL exist; int type; int x, y; int dest; int nFrame; int nStay; } Enemy[MAXENEMY];

char *arEnemy[] = {" ◐#※#◑ ", " <-=-> ", " nNWNn ", " ▩====▩ "};

struct { BOOL exist; char ch; int x, y; int nFrame; int nStay; } EnemyBullet[MAXENEMYBULLET];

void SetColor(int color) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color); }

void LoadingStage() { int i;

for(i = 20; i<50; i+=2)
{
    SetColor(WHITE);
    gotoxy(i, 4); printf("─");
    delay(30);
}

gotoxy(50, 4); printf("┐");
for(i = 5; i < 10; i++)
{
    SetColor(WHITE);
    gotoxy(50, i); printf("│");
    delay(50);
}

gotoxy(50, 10); printf("┘");
for(i = 48; i > 20; i -= 2)
{
    SetColor(WHITE);
    gotoxy(i, 10); printf("─");
    delay(30);
}

gotoxy(20, 10); printf("└");
for(i = 9; i > 4; i--)
{
    SetColor(WHITE);
    gotoxy(20, i); printf("│");
    delay(50);
}

gotoxy(20, 4); printf("┌");
delay(1500);

gotoxy(23, 7); printf("S"); delay(500);
gotoxy(27, 7); printf("H"); delay(500);
gotoxy(31, 7); printf("H"); delay(500);
gotoxy(35, 7); printf("S"); delay(500);
gotoxy(39, 7); printf("C"); delay(500);
gotoxy(43, 7); printf("P"); delay(500);
gotoxy(47, 7); printf("L"); delay(500);

gotoxy(44,13); printf("아무 키나 누르시면 게임 시작\n");
getch();

}

int main(void) { setcursortype(NOCURSOR); LoadingStage(); system("mode con: lines=40"); HeightLine(); randomize();

while(1)
{
    HeightLine();
    DrawInfo();

    PlayerMoveAction();
    PlayerBulletAction();
    PlayerMissileAction();

    DrawEnemy();
    EnemyMoveAction();
    MoveEnemyBullet();

    EnemyCrash();
    PlayerCrash();

    delay(10);
}

getch();
return 0;

}

void HeightLine() { int i;

for(i = 0; i < 40; i++)
{
    SetColor(WHITE);
    gotoxy(55, i); printf("│");
}

}

void DrawPlayer() { SetColor(GREEN); gotoxy(px, 39); printf("<<★>>"); }

void ErasePlayer() { gotoxy(px, 39); printf(" "); }

void PlayerMoveAction() { char ch; int i; if(GetAsyncKeyState(VK_LEFT) < 0 && px > 1) { ErasePlayer(); px--; DrawPlayer(); } else if(GetAsyncKeyState(VK_RIGHT) < 0 && px < 47 ) { ErasePlayer(); px++; DrawPlayer(); }

if(kbhit())
{
    ch = getch();
    switch(ch)
    {
    case ' ':
        for(i = 0; i < MAXBULLET && Bullet[i].exist == TRUE; i++) {;}
        if(i != MAXBULLET)
        {
            Bullet[i].x = px + 3; // 플레이어의 가운데에서 쏘기 위해 +  3
            Bullet[i].y = 38;
            Bullet[i].exist = TRUE;
        }
        break;
    case 'D':
        if(mx == -1)
        {
            mx = px + 3;
            my = 38;
        }
    case 'd':
        if(mx == -1)
        {
            mx = px + 3;
            my = 38; 
        }
        break;
    case ESC:
        exit(0);
    }
}

}

void DrawBullet(int i) { SetColor(GREEN); gotoxy(Bullet[i].x, Bullet[i].y); printf("*"); }

void EraseBullet(int i) { gotoxy(Bullet[i].x, Bullet[i].y); printf(" "); }

void PlayerBulletAction() { int i;

for(i = 0; i < MAXBULLET;  i++)
{
    if(Bullet[i].exist == TRUE)
    {
        EraseBullet(i);
        if(Bullet[i].y == 0)
        {
            Bullet[i].exist = FALSE;
        }
        else
        {
            Bullet[i].y--;
            DrawBullet(i);
        }
    }
}

}

void DrawMissile( ) { SetColor(YELLOW); gotoxy(mx, my - 2); printf("▲▲"); gotoxy(mx, my - 1); printf("■■"); gotoxy(mx, my); printf("■■"); }

void EraseMissile( ) { SetColor(YELLOW); gotoxy(mx, my - 2); printf(" "); gotoxy(mx, my - 1); printf(" "); gotoxy(mx, my); printf(" "); }

void PlayerMissileAction( ) { if(mx != -1) { EraseMissile( ); if(my - 2 == 0) { mx = -1; } else { my--; DrawMissile( ); } } }

void DrawEnemy() { BOOL Found; int i, j;

if(random(100) == 0)
{
    for(i = 0; i < MAXENEMY && Enemy[i].exist == TRUE; i++) {;}

    if(i != MAXENEMY)
    {
        if (random(2) == 0)
        {
            Enemy[i].x = 5;
            Enemy[i].dest = 1;
        }
        else
        {
            Enemy[i].x = 45;
            Enemy[i].dest = -1;
        }

        while(1)
        {
            Enemy[i].y = random(10) + 1;
            for(Found = FALSE, j = 0; j < MAXENEMY; j++)
            {
                if(Enemy[i].exist == TRUE && Enemy[j].y == Enemy[i].y)
                {
                    Found = TRUE;
                    break;
                }
            }

            if(Found == FALSE)
                break;
        }
        Enemy[i].nFrame = Enemy[i].nStay = random(6) + 1;
        Enemy[i].type = random(sizeof(arEnemy) / sizeof(arEnemy[0]));
        Enemy[i].exist = TRUE;
    }
}

}

void EnemyMoveAction() { int i;

for(i = 0; i < MAXENEMY; i++)
{
    if(Enemy[i].exist == FALSE)
        continue;

    if(--Enemy[i].nStay == 0)
    {
        if(Enemy[i].type == -1)
        {
            gotoxy(Enemy[i].x - 3, Enemy[i].y);         printf("                ");
            gotoxy(Enemy[i].x - 3, Enemy[i].y - 1);     printf("                ");
            gotoxy(Enemy[i].x - 3, Enemy[i].y - 2);     printf("                ");
            Enemy[i].exist = FALSE;
            continue;
        }

        Enemy[i].nStay = Enemy[i].nFrame;

        if(Enemy[i].x > 47 || Enemy[i].x < 4)
        {
            Enemy[i].exist = FALSE;
            gotoxy(Enemy[i].x - 3, Enemy[i].y);
            printf("        ");
        }
        else
        {
            Enemy[i].x += Enemy[i].dest;
            SetColor(RED);
            gotoxy(Enemy[i].x - 3, Enemy[i].y);
            printf(arEnemy[Enemy[i].type]);

            EnemyBulletAction(i);
        }
    }
}

}

void EnemyBulletAction(int i) { int j;

if(random(20) == 0)
{
    for(j = 0; j < MAXENEMYBULLET && EnemyBullet[j].exist == TRUE; j++) {;}
    if(j != MAXENEMYBULLET)
    {
        EnemyBullet[j].exist = TRUE;
        EnemyBullet[j].ch = random('*');
        EnemyBullet[j].x = Enemy[i].x + 2;
        EnemyBullet[j].y = Enemy[i].y + 1;
        EnemyBullet[j].nFrame = Enemy[j].nStay = Enemy[i].nFrame * 6;
    }
}

}

void DrawEnemyBullet(int i) { SetColor(RED); gotoxy(EnemyBullet[i].x, EnemyBullet[i].y); printf("*"); }

void EraseEnemyBullet(int i) { gotoxy(EnemyBullet[i].x, EnemyBullet[i].y); printf(" "); }

void MoveEnemyBullet() { int i;

for(i = 0; i < MAXBULLET; i++)
{
    if (EnemyBullet[i].exist == FALSE)
        continue;

    if(--EnemyBullet[i].nStay == 0)
    {
        EnemyBullet[i].nStay = EnemyBullet[i].nFrame;
        EraseEnemyBullet(i);

        if(EnemyBullet[i].y > 38)
        {
            EnemyBullet[i].exist = FALSE;
        }
        else
        {
            if(random(3))
            {
                if(EnemyBullet[i].x < px)
                    EnemyBullet[i].x++;
                else
                    EnemyBullet[i].x--;
            }
            EnemyBullet[i].y++;
            DrawEnemyBullet(i);
        }
    }
}

}

void EnemyCrash() { int i;

for(i = 0; i < MAXENEMY; i++)
{
    if(Enemy[i].exist == FALSE || Enemy[i].type == -1)
        continue;
    if(Enemy[i].y == Bullet[i].y && abs(Enemy[i].x - Bullet[i].x) <= 7)
    {
        gotoxy(Bullet[i].x, Bullet[i].y); printf("  ");
        Bullet[i].exist = FALSE;
        Enemy[i].type = -1;

        SetColor(GREEN);
        gotoxy(Enemy[i].x - 3, Enemy[i].y);
        printf("+10점!!!");
        Score += 10;
        Enemy[i].nFrame = Enemy[i].nStay = 50;
        break;
    }

    if(Enemy[i].y == my && abs(Enemy[i].x - mx) <= 7)
    { 
        gotoxy(mx, my); printf("    ");
        mx = -1;
        Enemy[i].type = -1;

        SetColor(GREEN);
        gotoxy(Enemy[i].x - 3, Enemy[i].y);
        printf("+10점!!!");
        Score += 10;
        Enemy[i].nFrame = Enemy[i].nStay = 50;
        break;
    }
}

}

void PlayerCrash() { int i;

for(i = 0; i < MAXENEMYBULLET; i++)
{
    if(EnemyBullet[i].exist == FALSE)
        continue;

    if(EnemyBullet[i].y == 39 && abs(EnemyBullet[i].x - px) <= 5)
    {
        SetColor(RED);
        gotoxy(px - 3,37); printf("@Bang@");
        gotoxy(px - 3,38); printf("@@@@@");
        gotoxy(px - 3,39); printf("@Bang@");
        delay(2000);
        clrscr();
        gotoxy(35,12); printf("게임오버");
        delay(1000);
        exit(0);
    }
}

}

void DrawInfo() { SetColor(WHITE); gotoxy(58, 3); printf("┌────────┐"); gotoxy(58, 4); printf("│S H H H S C P L │"); gotoxy(58, 3); printf("└────────┘");

gotoxy(62, 10); printf("점수 : %d", Score);

gotoxy(60, 20); printf("LEFT ← → RIGHT");
gotoxy(60, 25); printf("총 : 스페이스바");
gotoxy(60, 20); printf("미사일 : D");

}

여기서 제 총알은 나가는데 적의 총알이 나가질 않네요... 방법이 없을 까요?

  • 2017년 08월 12일에 작성됨

조회수 85


Kakao codefestival banner

로그인이 필요한 기능입니다.

Hashcode는 개발자들을 위한 무료 QnA사이트 입니다. 작성한 답변에 다른 개발자들이 댓글을 작성하거나 좋아요/싫어요를 할 수 있기 때문에 계정을 필요로 합니다.
► 로그인
► 계정만들기
Close