2011. 9. 3. 14:55


//2011.06.28
//daun

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define ON 1
#define OFF 0
using namespace std;

//전체 크기 80*25
//0번은 배경 1번은 뱀의몸통 2번은 먹이
//3이 되면 count가 1이 증가하고 ... 이런식으로해야겟군.. 아아아아아아아아 복잡하다 일단은 해봅시다

void headWay(int, int *, int *);

struct room{
 int time;
 int type;
};


int main()
{
 srand(time(NULL));
 int count=1 ; //먹이를 먹은 횟수이자 뱀의 몸통길이를 나타냄
 room arr[70][23]={0,0};
 int headx=0, heady=0;
 int type=117;
 int foodx =0, foody = 0;
 int level;
 int leveltime = 0;
 cout << "1~10 Level중 몇 Level을 할거인지 입력해주세요 ." << endl;
 cout << "1 : 가장 쉬운 난이도 , 10 : 가장 어려운 난이도 " << endl;
 cin >> level;
 leveltime = 25*(11-level);

 headx = rand()%70;
 heady = rand()%23;
 foodx = rand()%70;
 foody = rand()%23;
 arr[foodx][foody].type = 3;

 while(1)
 {
  Sleep(leveltime);
  system("cls");

  if(_kbhit())
  {
   type = _getch();
   headWay(type, &headx, &heady); 
  } //방향키를 누른경우
  else
  {
   headWay(type, &headx, &heady);
  } //방향키를 누르지 않은 경우

  arr[headx][heady].type += 1;
  arr[headx][heady].time = count;

  //먹이는 숫자 3임
  cout << "위 : U, 아래 : J, 왼쪽 : H, 오른쪽 : K  <난이도" << level << ">"<<endl;

  for(int b = 0 ; b <23; b++)
  {
   for(int a=0; a<70; a++)
   {
    if(arr[a][b].type == 0)
     cout << " "; //그냥배경
    if(arr[a][b].type == 1)
    {
     cout << "o";
     arr[a][b].time --;
     if(arr[a][b].time == 0)
      arr[a][b].type = 0;
    } //그냥 뱀몸통
    if(arr[a][b].type == 2)
    {
     system("cls");
     cout << "Your Score is " << count -1 << endl;
     cout << "END GAME " <<endl;
     Sleep(10000);
     return 0;
    } //뱀이 자신의 몸에 닿아 게임이 끝난 경우
    if(arr[a][b].type == 3)
    {
     cout <<"X";
    } //그냥 먹이가 놓여진경우
    if(arr[a][b].type == 4)
    {
     arr[a][b].type = 1;
     count++;
     foodx = rand()%20;
     foody = rand()%20;
     arr[foodx][foody].type = 3;
    } //뱀이 먹이를 먹은경우
   }
   cout << endl;
  }
 }
 return 0;
}

void headWay(int type, int *x, int *y)
{   
 switch(type)
 {
 case 117:
  if(*y == 0)
   *y = 22;
  else
   *y = *y - 1;
  break;//위로
 case 106:
  if(*y == 22)
   *y = 0;
  else
   *y = *y + 1;
  break; // 아래로
 case 104:
  if(*x == 0)
   *x = 69;
  else
   *x = *x - 1;
  break; //왼쪽으로
 case 107:
  if(*x == 69)
   *x = 0;
  else
   *x = *x + 1;
  break; //오른쪽으로
 }
} //방향키를 눌럿을때 뱀이 움직이는 방향 조절하는 함수

'Code > c/c++' 카테고리의 다른 글

[C++]연산자오버로딩  (0) 2012.02.11
[C++]Double Linked List 기본기능만  (0) 2011.12.23
[C++]poker  (0) 2011.09.03
[Code] 유클리드 알고리즘(Euclid Algorism)  (0) 2011.08.29
[C++ Code]Swap  (0) 2011.07.28
Posted by I_co