2010년 4월 5일 월요일

갤러그 Game

#include <stdio.h>
#include <conio.h>
#include <windows.h>

#define TRUE 1
#define FALSE 0

#define LEFT 0x4b // 방향키 왼쪽
#define RIGHT 0x4d // 방향키 오른쪽
#define KEY_F 0x66 // 키보드 f키
enum{
 BLACK,      /*  0 : 까망 */
  DARK_BLUE,    /*  1 : 어두운 파랑 */
  DARK_GREEN,    /*  2 : 어두운 초록 */
  DARK_SKY_BLUE,  /*  3 : 어두운 하늘 */
  DARK_RED,    /*  4 : 어두운 빨강 */
  DARK_VOILET,  /*  5 : 어두운 보라 */
  DARK_YELLOW,  /*  6 : 어두운 노랑 */
  GRAY,      /*  7 : 회색 */
  DARK_GRAY,    /*  8 : 어두운 회색 */
  BLUE,      /*  9 : 파랑 */
  GREEN,      /* 10 : 초록 */
  SKY_BLUE,    /* 11 : 하늘 */
  RED,      /* 12 : 빨강 */
  VOILET,      /* 13 : 보라 */
  YELLOW,      /* 14 : 노랑 */  
  WHITE,      /* 15 : 하양 */  
};

typedef enum {NOCURSOR, SOLIDCURSOR, NORMALCURSOR}CURSOR_TYPE; //커서 없앰, 커서 확대, 커서 원복

char Block[2][4] = {" * ", "***"};// 캐릭터

void setcursortype(CURSOR_TYPE); // 커서 변경
void BackGround(int (*Board)[20], int input); // 배경 태두리 생성
void SetFixedPlayer(int x, int y); //캐릭터 생성
void SetFlowPlayer(int x,int y); //캐릭터 생성
void SetColor(int n); // 색깔 생성
void gotoxy(int x, int y); // 커서이동 함수 부분
void erase_Block(int x, int y); //기존의 Block 공백
void Bullet(int x, int y); // 키보드 f 버튼 클릭
void GameEnd();

int main(void)
{
 int row = 20;
 char key_input;
 static x=8, y= 17;
 int Board[20][20] = {0,};
 
 setcursortype(NOCURSOR);
 BackGround(Board, row);
 SetFixedPlayer(8, 1);
 SetFlowPlayer(8, 17);
 while(1)
 {
  if(kbhit())
  {
   key_input = getch();
   switch(key_input)
   {
   case LEFT :
    erase_Block(x, y); //기존의 블록 공백으로 설정
    SetFlowPlayer(x-=1, y); //x좌표 왼쪽으로 한칸
    if(x == 0)//보드 Block 범위 제한
    {
     x = 1;
     SetFlowPlayer(x, y); //블록 생성
    }
   
    break;
   case RIGHT :
    erase_Block(x, y); //기존의 블록 공백으로 설정
    SetFlowPlayer(x+=1, y);//x좌표 오른쪽으로 한칸
    if(x == 16)
    {
     x = 15;
     SetFlowPlayer(x, y); //블록 생성
    }
    break;
   case KEY_F:
    Bullet(x+1, y-2); //총알 생성
    break;
   }
  }
 }
 SetColor(7);//원래 배경색으로 복귀
 
 return 0;
 
}
void setcursortype(CURSOR_TYPE c)
{
 CONSOLE_CURSOR_INFO Curinfo; // console cursor 구조체
 switch(c)
 {
 case NOCURSOR:
  Curinfo.dwSize = 1; //cursor size
  Curinfo.bVisible = FALSE; // cursor 사라짐
  break;
 case SOLIDCURSOR:
  Curinfo.dwSize = 100; // cursor 크기 확대
  Curinfo.bVisible = TRUE; //커서 생성
  break;
 case NORMALCURSOR:
  Curinfo.dwSize = 20; // cursor 크기 작게
  Curinfo.bVisible = TRUE; //커서 생성
  break;
}
 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &Curinfo);
}

void SetColor(int n)
{
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), n);
}
void BackGround(int (*Board)[20], int input)
{
 int i, j, count =0;
 int direct =1, Column = -1, Row = 0;
 while(count != 2) // 달팽이 알고리즘 사용으로 테두리 블록생성
 {
  for(i=0; i < input; i++)            // 가로 처리 반복문
  {
   Column+=direct;
   Board[Row][Column]='@';
  }
 
  input--;
 
  for(i=0;i< input;i++)           // 세로 처리 반복문
  {
   Row+=direct;
   Board[Row][Column]='@';
  }
  count++;
  direct*=-1;               // 방향을 결정짓는 중요한 부분
 }
 for(i = 0; i < 20;i++)
 {
  for(j = 0; j < 20; j++)
  {
   if(Board[i][j] == '@') // 보드판 블록 색깔 지정
   {
    SetColor(5);
   
    printf("%c",Board[i][j]);
   }
   else
   {
    printf(" ");
   }
  }
  puts("");
 }
}
void SetFixedPlayer(int x, int y)
{
 int i;
 SetColor(10);
 gotoxy(x, y);
 for(i = 0; i < 3; i++)
 {
  printf("%c", Block[1][i]);
 }
 
}
void gotoxy(int x, int y)             /* 커서이동 함수 부분 */
{
    COORD XY = {x, y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), XY);
}
void SetFlowPlayer(int x, int y)
{
// static a = 0; 블록의 움직임을 확인
 int i, j;
 SetColor(10);
 if(x == 0 || x == 16) //보드 Block 범위 제한
 {
  return 0;
 }
 gotoxy(x, y);
// printf("%d %d\n", x, y);
 for(i = 0; i < 2; i++)
 {
 // a++;
  if(i==1)
  {
   gotoxy(x, y+1);
  }
  for(j = 0; j < 4; j++)
  {
   if(Block[i][j] == '*')
   {
   
//     if(a >= 3)
//     {
//      SetColor(1);
//     }
//    Sleep(1000);
    printf("%c", Block[i][j]);
   }
   else
   {
    printf(" ");
   }
   
  }
  puts("");
 }
}
void erase_Block(int x, int y)
{
 // static a = 0; 블록의 움직임을 확인
 int i, j;
 SetColor(10);
 gotoxy(x, y);
// printf("%d %d\n", x, y);
 for(i = 0; i < 2; i++)
 {
 // a++;
  if(i==1)
  {
   gotoxy(x, y+1);
  }
  for(j = 0; j < 3; j++)
  {
   if(Block[i][j] == '*')
   {
    printf(" "); // 기존의 블록 공백
   }
   else
   {
    printf(" ");
   }
   
  }
  puts("");
 }
}
void Bullet(int x, int y) // 키보드 f 버튼 클릭
{
 char bullet = '^'; //총알 설정
 gotoxy(x, y);
 for(y; y > 0; y--)
 {
  if(y == 1&& (x==8 || x==9 || x==10))
  {
   GameEnd();
  }
  printf("%c", bullet);
  Sleep(200); // 총알의 움직임을 명시
  erase_Block(x, y); // 기존의 총알 공백
  gotoxy(x, y);
  if(y == 1) // 캐릭터를 재 생성
  {
   SetFixedPlayer(8, 1);
  }
 }

}
void GameEnd()
{
 SetColor(RED);
 gotoxy(15,8);
 printf("┏━━━━━━━━━━━━━┓");
 gotoxy(15,9);
 printf("┃**************************┃");
 gotoxy(15,10);
 printf("┃*        GAME OVER       *┃");
 gotoxy(15,11);
 printf("┃**************************┃");
 gotoxy(15,12);
 printf("┗━━━━━━━━━━━━━┛");
 fflush(stdin);
 Sleep(1000);
 system("cls");
 SetColor(7);
 exit(0);
}

댓글 없음:

댓글 쓰기