C 언어 는 간단 한 뱀 먹 기 게임 을 실현 한다.

40198 단어 C 언어탐식 사
본 논문 의 사례 는 여러분 에 게 C 언어 가 간단 한 탐식 뱀 게임 을 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
\#훈훈 한 알림:다음 디자인 은 VS 2019 에서 완벽 하 게 실행 되 고 전체 코드 는 끝 에 첨부 되 어 있 으 니 참고 하 시기 바 랍 니 다.
(1)게임 접속 인터페이스(동적 읽 기 및 따뜻 한 알림)

void loading()//     
{
 int j;
 gotoxy(43, 11);
 printf("Loading...");
 gotoxy(39, 13);
 printf("□□□□□□□□");
 gotoxy(39, 13);
 for (j = 8; j >= 0; j--)
 {
  HideCursor();
  Sleep(200);
  printf("\033[40;35m■\033[0m");
 }
 gotoxy(43, 11);
 printf("          ");
 gotoxy(39, 13);
 printf("                          ");
 gotoxy(39, 9);
 printf("         !");
 gotoxy(22, 14);
 printf("                              ");
 gotoxy(22, 16);
 printf("                              ");
 Sleep(1500);
}

(2)메뉴 인터페이스

void menu(int a)
{
 HideCursor();
 gotoxy(11, 3);
 printf("\033[40;37m                         _________________________  ||                                   \033[0m");
 gotoxy(11, 4);
 printf("\033[40;33m                       //=====================      ||   //                              \033[0m");
 gotoxy(11, 5);
 printf("\033[40;37m                      //                            ||  //                               \033[0m");
 gotoxy(11, 6);
 printf("\033[40;37m                      \\\\                  __        || //                              \033[0m");
 gotoxy(11, 7);
 printf("\033[40;33m                       \\\\                //\\\\       ||//                             \033[0m");
 gotoxy(11, 8);
 printf("\033[40;33m                       //               //  \\\\      ||\\\\        =====                \033[0m");
 gotoxy(11, 9);
 printf("\033[40;37m                      //    ==         //    \\\\     || \\\\     //      \\\\           \033[0m");
 gotoxy(11, 10);
 printf("\033[40;37m _ _ _ ____          //   //  \\\\      //======\\\\    ||  \\\\   ||=========           \033[0m");
 gotoxy(11, 11);
 printf("\033[40;33m- - - -----\\\\_______//   //    \\\\    //        \\\\   ||   \\\\  ||                  \033[0m");
 gotoxy(11, 12);
 printf("\033[40;35m            \\-------/   //      \\\\  //          \\\\  ||    \\\\  \\\\________________\033[0m");
 gotoxy(39, 6);
 printf("\033[40;36m②○②○\033[0m");
 gotoxy(2, 15);
 printf("\033[40;33m―――――――――――――――――――――――――――――――――――――――――――――――――\033[1m");

 gotoxy(2, 18);
 printf("                      1.                                  2.    ");
 gotoxy(2, 20);
 printf("                      3.                                  4.    ");

 switch (a) {
 case 1: gotoxy(22, 18); break;
 case 2: gotoxy(62, 18); break;
 case 3: gotoxy(22, 20); break;
 case 4: gotoxy(62, 20); break;
 }
 printf("\033[40;36m◇\033[0m");
 return;
}

(3)메뉴 선택

void chose()//    
{
 ii = 1;
 fflush(stdin);
 frame(2);
 menu(ii);
 while (ii != 5)
 {
  (void)_getch();
  if (GetAsyncKeyState(VK_DOWN) & 0x8000)// 
  {
   if (ii == 1)
    ii = 3;
   else if (ii == 2)
    ii = 4;
  }
  if (GetAsyncKeyState(VK_UP) & 0x8000)// 
  {
   if (ii == 3)
    ii = 1;
   else if (ii == 4)
    ii = 2;
  }
  if (GetAsyncKeyState(VK_LEFT) & 0x8000)// 
  {
   if (ii == 2)
    ii = 1;
   else if (ii == 4)
    ii = 3;
  }
  if (GetAsyncKeyState(VK_RIGHT) & 0x8000)// 
  {
   if (ii == 1)
    ii = 2;
   else if (ii == 3)
    ii = 4;
  }
  menu(ii);
  Sleep(150);//  
  if (GetAsyncKeyState(VK_RETURN) & 0x0D)//  
  {
   fflush(stdin);
   return;
  }
 }
}
(4)장애물 과 지도

void obstacle() //   
{   //IOT  
 int sup_mapx1[68] = { 16,18,20,22,24,20,20,20,20,20,20,16,18,20,22,24,36,38,40,42,44,36,44,36,44,36,44,36,44,36,44,36,44,36,38,40,42,44,56,58,60,62,64,60,60,60,60,60,60,60 };
 int sup_mapy1[68] = {  8, 8, 8, 8, 8, 9,10,11,12,13,14,15,15,15,15,15, 8, 8, 8, 8, 8, 9, 9,10,10,11,11,12,12,13,13,14,14,15,15,15,15,15, 8, 8, 8, 8, 8, 9,10,11,12,13,14,15 };
     //ZHKU  
 int sup_mapx2[77] = { 10,12,14,16,18,20,20,18,16,14,12,10,10,12,14,16,18,20,28,28,28,28,28,28,28,28,30,32,34,36,36,36,36,36,36,36,36,44,44,44,44,44,44,44,44,46,48,48,50,52,50,52,60,60,60,60,60,60,60,60,62,64,66,68,70,70,70,70,70,70,70,70,18,12,46,46,48 };
    int sup_mapy2[77] = {  9, 9, 9, 9, 9, 9,10,11,12,13,14,15,16,16,16,16,16,16, 9,10,11,12,13,14,15,16,12,12,12, 9,10,11,12,13,14,15,16, 9,10,11,12,13,14,15,16,12,11,13,10, 9,14,15, 9,10,11,12,13,14,15,16,16,16,16,16, 9,10,11,12,13,14,15,16,10,15,11,13,12 };
 if (map == 2)//  
  j = 5;
 if (map == 3)//  
  j = 15;
 if (map == 4)//  
  j = 30;
 if(map>1)
 for (i = 0; i < j; i++)//     
 {
  obs.x[i] = rand() % 70 + 6;
  obs.y[i] = rand() % 20 + 3;
  if (obs.x[i] % 2 != 0)//       
   obs.x[i]--;
  if (obs.x[i] == food.x && obs.y[i] == food.y)//             
  continue;
  if (obs.y[i] == 20)//           
   i--;
 }
 else if (map == 0)
 {                 
  j = 68;
  for (i = 0; i < j; i++)//     1  
  {
   obs.x[i] = sup_mapx1[i];
   obs.y[i] = sup_mapy1[i];
  }
 }
 else if (map == -1)
 {
  j = 77;
  for (i = 0; i < j; i++)//     2  
  {
   obs.x[i] = sup_mapx2[i];
   obs.y[i] = sup_mapy2[i];
  }
 }
 if (map >1)
  for (i = 0; i < j; i++)//     
  {
   gotoxy(obs.x[i], obs.y[i]);
   printf("\033[40;31m▲\033[0m");
  }
 else if(map==0)
  for (i = 0; i < 50; i++)//      1
  {
   gotoxy(obs.x[i], obs.y[i]);
   if(i<16)
   printf("\033[40;31m■\033[0m");
   else if(i>37)
        printf("\033[40;34m■\033[0m");
          else printf("\033[40;32m■\033[0m");
  }
 else if(map==-1)
  for (i = 0; i < 72; i++)//      2
  {
   gotoxy(obs.x[i], obs.y[i]);
   if(i<18)printf("\033[40;32m■\033[0m");
   if (i < 37 && i >= 18) printf("\033[40;33m■\033[0m");
   if(i>=37&&i<52) printf("\033[40;36m■\033[0m");
   if (i >=52)printf("\033[40;35m■\033[0m");
  }
}

(5)뱀의 제어

void control()
{
 fflush(stdin);
 if (foodFlag == 0)//             
 {
  gotoxy(snake.x[snake.length - 1], snake.y[snake.length - 1]);
  switch (tail) //        
  {
  case 1:printf("  "); break;
  case 2:printf("~ "); break;
  case 3:printf("・"); break;
  }
 }
 for (i = snake.length - 1; i > 0; i--)//              (    )
 {
  snake.x[i] = snake.x[i - 1];
  snake.y[i] = snake.y[i - 1];
 }
 while (1)
 {
  if ((GetAsyncKeyState(VK_DOWN)) && vkflag != 2)// 
  {
   vkflag = 4; break;
  }
  if ((GetAsyncKeyState(VK_UP)) && vkflag != 4)// 
  {
   vkflag = 2; break;
  }
  if ((GetAsyncKeyState(VK_LEFT)) && vkflag != 3)// 
  {
   vkflag = 1; break;
  }
  if ((GetAsyncKeyState(VK_RIGHT)) && vkflag != 1)// 
  {
   vkflag = 3; break;
  }
  if (GetAsyncKeyState(VK_SPACE) && 0x20)//    
   while (1)
   {
    gotoxy(84, 16);
    printf("\033[40;36m      \033[0m");
    Sleep(200);
    gotoxy(84, 16);
    printf("            "); //  
    Sleep(150);
    if (GetAsyncKeyState(VK_SPACE) && 0x20)
     break;
   }
  break;
 }
 switch (vkflag)//  
 {
 case 1: snake.x[0] -= 2; break;// 
 case 2: snake.y[0]--; break;// 
 case 3: snake.x[0] += 2; break;// 
 case 4: snake.y[0]++; break;// 
 }
 gotoxy(snake.x[0], snake.y[0]);//    
 printf("◆");
 gotoxy(snake.x[1], snake.y[1]);//    
 printf("■");
 foodFlag = 0;
 fflush(stdin);
 return;
}
(6)음식 생 성

void foodcreate()
{
 n++;//    
 flag = 1;
    sorce += 10;//     10 
 if (sorce>=his_max )//   
 {
  his_max = sorce;
  gotoxy(82, 2);
  printf("                 ");
  gotoxy(82, 2);
  printf("★    :%d", his_max);
 }
 gotoxy(82, 3);//    
 printf("                 ");
 gotoxy(82, 3);
 printf("☆    :%d", sorce);

 while (flag==1)//    
 {  
   food.x = rand() % 74 + 4;//         
   food.y = rand() % 24 + 1;
   if (food.x % 2 != 0 && flag == 1)//      
   {
    food.x = food.x - 1;
    flag = 0;
   }
   if(map!=1)//      
   for (i = 0; i < snake.length; i++)
   {
    for(m=0;m<j;m++)
     if (obs.x[m] == food.x && obs.y[m] == food.y)
     {
      flag = 1;
      break;
     }
   }
   for (i = 0; i < snake.length; i++)//            
   {
    if (snake.x[i] == food.x && snake.y[i] == food.y)
    {
     flag = 1;
     break;
    }
   }
 }
    gotoxy(food.x, food.y); //    
 printf("●");
 snake.length++;//       1
 foodFlag = 1;//         
 return;
}
(7)사망 판단

int  dead()
{
 if (snake.y[0] == 0 || snake.y[0] == map_y-1)//      
  return 0;
 if (snake.x[0] == 0 || snake.x[0] == map_x-2)//      
  return 0;
 if(map!=1)//       
 for (m = 0; m < j; m++)
  if (obs.x[m] == snake.x[0] && obs.y[m] == snake.y[0])
  {
   return 0;
  }
 for (i = 1; i < snake.length; i++)//      
 {
 if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0])
  return 0;
 }
 if (GetAsyncKeyState(VK_ESCAPE)&0x1B)//ESC     
 {
  ii = 5;
  return 0;
 }
 return 1;
}

(8)게임 초기 화

void game()//     
{
 int i ;
 n = 0;//    
 vkflag = 1;
 sorce = 0;
 foodFlag = 0;
 system("cls");//    
 frame(1);//      (  )
 gotoxy(82, 2);
 printf("★    :%d", his_max);
 gotoxy(82, 3);
 printf("☆    :%d", sorce);
 gotoxy(84, 8);
 printf("      ");
 gotoxy(85, 9);
 printf("\033[40;33m    ↑      \033[0m");
 gotoxy(85, 10);
 printf("\033[40;33m  ←  →    \033[0m");
 gotoxy(85, 11);
 printf("\033[40;33m    ↓      \033[0m");
 gotoxy(83, 12);
 printf("       ");
 gotoxy(82, 18);
 printf("      /  ");
 gotoxy(83, 20);
 printf(" Shift    "); 
 gotoxy(82, 22);
 printf(" ESC     ");

 snake.length = org_len;//    (    )
 snake.x[0] = 50 ;
 snake.y[0] = 20;
 gotoxy(snake.x[0], snake.y[0]);
 printf("◆");
 for (i = 1; i < snake.length; i++)//   
 {
  Sleep(25);
  snake.x[i] = snake.x[i - 1] + 2;
  snake.y[i] = snake.y[0];
  gotoxy(snake.x[i], snake.y[i]);
  printf("■");
 }
 if (map >=1)
 {
  food.x = rand() % 44 + 4;//     (    )
  food.y = rand() % 24 + 1;
  if (food.x % 2 != 0)
   food.x = food.x - 1;
 }
 else {
  food.x = 14;//     (    )
  food.y = 3;
 }
 gotoxy(food.x, food.y);//       
 printf("\033[40;37m●\033[0m");
 Sleep(250);
 gotoxy(food.x, food.y);
 printf("  ");
 Sleep(250);
 gotoxy(food.x, food.y);
 printf("\033[40;37m●\033[0m");
 Sleep(250);
 obstacle();
 while(ii==1) //  
 {
  if (GetAsyncKeyState(VK_SHIFT))//    
  {
   gotoxy(93, 20);
   printf("\033[40;31m  \033[0m");//  
  }
  else { Sleep(100);
  gotoxy(93, 20);
  printf("  ");
  }
  Sleep(speed - (changespeed * n));//    
  control();//  
  if (snake.x[0] == food.x && snake.y[0] == food.y)//    
  foodcreate();
  if (dead() != 1) //        
  {
   gotoxy(36,12);
   printf("Game over");//    
   gotoxy(36, 14);
   printf("  T _ T  ");
   Sleep(500);
   while (1)
   {
    gotoxy(32, 16);
    printf("  Enter      ");
    Sleep(150);

    if (GetAsyncKeyState(VK_RETURN) & 0x0D)//    
    {
     fflush(stdin);
     ii = 5;
     return;
    }
    
   }
  } 
 }
}
(9)게임 설정

void setup2()//    (      )
{
 gotoxy(34, 8);
 switch (org_len)//    
 {
 case 2: printf("               ◆■           >"); break;
 case 3: printf("           <   ◆■■         >"); break;
 case 4: printf("           <   ◆■■■       >"); break;
 case 5: printf("           <   ◆■■■■     >"); break;
 case 6: printf("           <   ◆■■■■■   >"); break;
 case 7: printf("           <   ◆■■■■■■  "); break;
 }
 gotoxy(34, 10);
 switch (speed)//    
 {
 case  50:printf("          <         "); break;
 case 100:printf("          <        >"); break;
 case 150:printf("                   >"); break;
 }
 gotoxy(34, 12);
 switch (changespeed)//    
 {
 case 0:printf("                   >"); break;
 case 5:printf("         <          "); break;
 }
 gotoxy(34, 14);
 switch (tail)//    
 {
 case 1:printf("                   >"); break;
 case 2:printf("         <    ~     >"); break;
 case 3:printf("         <   ・      "); break;
 }
 gotoxy(34, 16);
 switch (map)//    
 {
 case -1:printf("               2 >"); break;
 case 0:printf("         <     1 >"); break;
 case 1:printf("         <        >"); break;
 case 2:printf("         <        >"); break;
 case 3:printf("         <        >"); break;
 case 4:printf("         <         "); break;
 }
    gotoxy(70, 22);
    printf("       "); 

 for (i = 0; i < 7; i++)
 {
  gotoxy(31, 8 + 2 * i);
  printf("  ");
 }
 gotoxy(67, 22);
 printf("  ");
 switch (set_flag)
 {
 case 1:gotoxy(31,  8); break;
 case 2:gotoxy(31, 10); break;
 case 3:gotoxy(31, 12); break;
 case 4:gotoxy(31, 14); break;
 case 5:gotoxy(31, 16); break;
 case 6:gotoxy(67, 22); break;
 }
 printf("★");
 return;
}
void setup()//    (      )
{
 frame(2);
 set_flag=1;
 gotoxy(30, 3);
 printf("      ■■◆            ◆■■");
 gotoxy(26, 4);
 printf("■■■■■■                        ■■■■■■");
 setup2();
 fflush(stdin);
 while (1)
 {
  if (GetAsyncKeyState(VK_DOWN))// 
  {
   if (set_flag == 6)
    set_flag = 1;
   else set_flag++;
  }
  if (GetAsyncKeyState(VK_UP))// 
  {
   if (set_flag == 1)
    set_flag = 6;
   else set_flag--;
  }
  if (GetAsyncKeyState(VK_LEFT))// 
  {
   switch (set_flag)
   {
   case 1: if (org_len > 2)
    org_len--; break;
   case 2: if (speed < 150)
    speed += 50; break;
   case 3: if (changespeed == 5)
    changespeed = 0;break;
   case 4: if (tail > 1)
    tail--;break;
   case 5: if (map > -1)
    map--;break;
   case 6: break;
   }
  }
  if (GetAsyncKeyState(VK_RIGHT))// 
  {
   switch (set_flag)
   {
   case 1: if (org_len < 7)
    org_len++;break;
   case 2: if (speed > 50)
    speed -= 50;break;
   case 3: if (changespeed == 0)
    changespeed = 5;break;
   case 4: if (tail < 3)
    tail++;break;
   case 5: if (map < 4)
    map++;break;
   case 6:break;
   }
  }
  setup2();
  if ((GetAsyncKeyState(VK_RETURN)&0x0D)&&set_flag==6)
  {
   ii = 5;
   return;
  }
  Sleep(175);
 }
 return;
}

(10)게임 설명

void instructions()//    
{
 system("cls");
 frame(2);
 gotoxy(10, 3);
 printf("    :");
 gotoxy(18, 5);
 printf("         ,           ,         ");
 gotoxy(18, 7);
 printf(" ↑←↓→                     ,    ");
 gotoxy(18, 9);
 printf("  ,      ,   10 ,         ,     ");
 gotoxy(18, 11);
 printf("                ,    。");
 gotoxy(18, 15);
 printf("    :     ,  Shift   ,         , ");
 gotoxy(18, 17);
 printf("                     。                          ");
 gotoxy(60, 20);
 printf("         ");
 gotoxy(3, 23);
 printf("       V1.0");
 while (1)//         
 {
  Sleep(100);
  (void)_getch();
  if (GetAsyncKeyState(VK_RETURN))//  
  {
   ii = 5;
   break;
  }
 }
}
void end()//    
{
 frame(2);
 gotoxy(44, 12);
 printf("     ");
 gotoxy(0, 26);
}

(11)전체 코드 직접 붙 이기

#include<stdio.h>
#include <stdlib.h>
#include<Windows.h>
#include <time.h>
#include<conio.h>

#define   SNAKE 300//      
#define   KEYDOWN(vk_code)((GetAsyncKeyState(vk_code)&0x8000)?1:0)//vk       ,         
#define   KEYUP(vk_code)((GetAsyncKeyState(vk_code)&0x8000)?0:1)

int ii = 1, i, m, flag = 1, n = 0, set_flag = 1;//       (ii   ,n  ,set_flag  ,flag         )
int speed = 100, changespeed = 5, tail = 1, org_len = 5, map = 1;//  、    、    、    、    
int vkflag = 1;//    
int sorce = 0, his_max = 0;//          
int map_x = 80, map_y = 26;//  
int foodFlag = 0;//      
int j;//     

struct//   
{
 int length;//    
 int x[SNAKE];//            x   
 int y[SNAKE];//            y   
}snake;
struct //      
{
 int x, y;
} food;
struct //     
{
 int x[77];
 int y[77];
}obs;

void gotoxy();//      
void HideCurson();//    
void frame(int a);//  1/2
void loading();//( )  
void menu(int a);//  
void chose();//    
void obstacle();//   
void control();//    
void foodcreate();//    
int  dead();//    
void game();//    
void setup2();//     
void setup();//       
void instructions();//    
void end();//    

void gotoxy(int x, int y) //    
{
 COORD pos = { x,y };
 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//           
 SetConsoleCursorPosition(hOut, pos);//             ,    
}
void HideCursor()//    
{
 CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void frame(int a)//    
{
 int i, k;
 system("cls");
 gotoxy(78, 0);// 1
 for (k = 0; k <= 11; k++)
 {
  if (a == 1)
   Sleep(4);
  printf("\033[40;37m■\033[0m");
 }

 for (i = 0; i <= 25; i++)//  
 {
  if (a == 1)
   Sleep(4);
  gotoxy(100, i);
  printf("\033[40;37m■\033[0m");
 }

 for (k = 0; k <= 50; k++)//    
 {
  if (a == 1)
   Sleep(4);
  gotoxy(100 - 2 * k, 25);
  printf("\033[40;37m■\033[0m");
 }
 for (i = 25; i >= 0; i--)//    
 {
  if (a == 1)
   Sleep(4);
  gotoxy(0, i);
  printf("\033[40;37m■\033[0m");
 }
 gotoxy(0, 0);
 for (k = 0; k <= 38; k++)// 2
 {
  if (a == 1)
   Sleep(4);
  printf("\033[40;37m■\033[0m");
 }
 if (a == 1)
  for (i = 0; i <= 25; i++)//   
  {
   Sleep(6);
   gotoxy(78, i);
   printf("\033[40;37m■\033[0m");
  }
}
void loading()//     
{
 int j;
 gotoxy(43, 11);
 printf("Loading...");
 gotoxy(39, 13);
 printf("□□□□□□□□");
 gotoxy(39, 13);
 for (j = 8; j >= 0; j--)
 {
  HideCursor();
  Sleep(200);
  printf("\033[40;35m■\033[0m");
 }
 gotoxy(43, 11);
 printf("          ");
 gotoxy(39, 13);
 printf("                          ");
 gotoxy(39, 9);
 printf("         !");
 gotoxy(22, 14);
 printf("                              ");
 gotoxy(22, 16);
 printf("                              ");
 Sleep(1500);
}
void menu(int a)
{
 HideCursor();
 gotoxy(11, 3);
 printf("\033[40;37m                         _________________________  ||                                   \033[0m");
 gotoxy(11, 4);
 printf("\033[40;33m                       //=====================      ||   //                              \033[0m");
 gotoxy(11, 5);
 printf("\033[40;37m                      //                            ||  //                               \033[0m");
 gotoxy(11, 6);
 printf("\033[40;37m                      \\\\                  __        || //                              \033[0m");
 gotoxy(11, 7);
 printf("\033[40;33m                       \\\\                //\\\\       ||//                             \033[0m");
 gotoxy(11, 8);
 printf("\033[40;33m                       //               //  \\\\      ||\\\\        =====                \033[0m");
 gotoxy(11, 9);
 printf("\033[40;37m                      //    ==         //    \\\\     || \\\\     //      \\\\           \033[0m");
 gotoxy(11, 10);
 printf("\033[40;37m _ _ _ ____          //   //  \\\\      //======\\\\    ||  \\\\   ||=========           \033[0m");
 gotoxy(11, 11);
 printf("\033[40;33m- - - -----\\\\_______//   //    \\\\    //        \\\\   ||   \\\\  ||                  \033[0m");
 gotoxy(11, 12);
 printf("\033[40;35m            \\-------/   //      \\\\  //          \\\\  ||    \\\\  \\\\________________\033[0m");
 gotoxy(39, 6);
 printf("\033[40;36m②○②○\033[0m");
 gotoxy(2, 15);
 printf("\033[40;33m―――――――――――――――――――――――――――――――――――――――――――――――――\033[1m");

 gotoxy(2, 18);
 printf("                      1.                                  2.    ");
 gotoxy(2, 20);
 printf("                      3.                                  4.    ");

 switch (a) {
 case 1: gotoxy(22, 18); break;
 case 2: gotoxy(62, 18); break;
 case 3: gotoxy(22, 20); break;
 case 4: gotoxy(62, 20); break;
 }
 printf("\033[40;36m◇\033[0m");
 return;
}
void chose()//    
{
 ii = 1;
 fflush(stdin);
 frame(2);
 menu(ii);
 while (ii != 5)
 {
  (void)_getch();
  if (GetAsyncKeyState(VK_DOWN) & 0x8000)// 
  {
   if (ii == 1)
    ii = 3;
   else if (ii == 2)
    ii = 4;
  }
  if (GetAsyncKeyState(VK_UP) & 0x8000)// 
  {
   if (ii == 3)
    ii = 1;
   else if (ii == 4)
    ii = 2;
  }
  if (GetAsyncKeyState(VK_LEFT) & 0x8000)// 
  {
   if (ii == 2)
    ii = 1;
   else if (ii == 4)
    ii = 3;
  }
  if (GetAsyncKeyState(VK_RIGHT) & 0x8000)// 
  {
   if (ii == 1)
    ii = 2;
   else if (ii == 3)
    ii = 4;
  }
  menu(ii);
  Sleep(150);//  
  if (GetAsyncKeyState(VK_RETURN) & 0x0D)//  
  {
   fflush(stdin);
   return;
  }
 }
}
void obstacle() //   
{   //IOT  
 int sup_mapx1[68] = { 16,18,20,22,24,20,20,20,20,20,20,16,18,20,22,24,36,38,40,42,44,36,44,36,44,36,44,36,44,36,44,36,44,36,38,40,42,44,56,58,60,62,64,60,60,60,60,60,60,60 };
 int sup_mapy1[68] = {  8, 8, 8, 8, 8, 9,10,11,12,13,14,15,15,15,15,15, 8, 8, 8, 8, 8, 9, 9,10,10,11,11,12,12,13,13,14,14,15,15,15,15,15, 8, 8, 8, 8, 8, 9,10,11,12,13,14,15 };
     //ZHKU  
 int sup_mapx2[77] = { 10,12,14,16,18,20,20,18,16,14,12,10,10,12,14,16,18,20,28,28,28,28,28,28,28,28,30,32,34,36,36,36,36,36,36,36,36,44,44,44,44,44,44,44,44,46,48,48,50,52,50,52,60,60,60,60,60,60,60,60,62,64,66,68,70,70,70,70,70,70,70,70,18,12,46,46,48 };
    int sup_mapy2[77] = {  9, 9, 9, 9, 9, 9,10,11,12,13,14,15,16,16,16,16,16,16, 9,10,11,12,13,14,15,16,12,12,12, 9,10,11,12,13,14,15,16, 9,10,11,12,13,14,15,16,12,11,13,10, 9,14,15, 9,10,11,12,13,14,15,16,16,16,16,16, 9,10,11,12,13,14,15,16,10,15,11,13,12 };
 if (map == 2)//  
  j = 5;
 if (map == 3)//  
  j = 15;
 if (map == 4)//  
  j = 30;
 if(map>1)
 for (i = 0; i < j; i++)//     
 {
  obs.x[i] = rand() % 70 + 6;
  obs.y[i] = rand() % 20 + 3;
  if (obs.x[i] % 2 != 0)//       
   obs.x[i]--;
  if (obs.x[i] == food.x && obs.y[i] == food.y)//             
  continue;
  if (obs.y[i] == 20)//           
   i--;
 }
 else if (map == 0)
 {                 
  j = 68;
  for (i = 0; i < j; i++)//     1  
  {
   obs.x[i] = sup_mapx1[i];
   obs.y[i] = sup_mapy1[i];
  }
 }
 else if (map == -1)
 {
  j = 77;
  for (i = 0; i < j; i++)//     2  
  {
   obs.x[i] = sup_mapx2[i];
   obs.y[i] = sup_mapy2[i];
  }
 }
 if (map >1)
  for (i = 0; i < j; i++)//     
  {
   gotoxy(obs.x[i], obs.y[i]);
   printf("\033[40;31m▲\033[0m");
  }
 else if(map==0)
  for (i = 0; i < 50; i++)//      1
  {
   gotoxy(obs.x[i], obs.y[i]);
   if(i<16)
   printf("\033[40;31m■\033[0m");
   else if(i>37)
        printf("\033[40;34m■\033[0m");
          else printf("\033[40;32m■\033[0m");
  }
 else if(map==-1)
  for (i = 0; i < 72; i++)//      2
  {
   gotoxy(obs.x[i], obs.y[i]);
   if(i<18)printf("\033[40;32m■\033[0m");
   if (i < 37 && i >= 18) printf("\033[40;33m■\033[0m");
   if(i>=37&&i<52) printf("\033[40;36m■\033[0m");
   if (i >=52)printf("\033[40;35m■\033[0m");
  }
}
void control()
{
 fflush(stdin);
 if (foodFlag == 0)//             
 {
  gotoxy(snake.x[snake.length - 1], snake.y[snake.length - 1]);
  switch (tail) //        
  {
  case 1:printf("  "); break;
  case 2:printf("~ "); break;
  case 3:printf("・"); break;
  }
 }
 for (i = snake.length - 1; i > 0; i--)//              (    )
 {
  snake.x[i] = snake.x[i - 1];
  snake.y[i] = snake.y[i - 1];
 }
 while (1)
 {
  if ((GetAsyncKeyState(VK_DOWN)) && vkflag != 2)// 
  {
   vkflag = 4; break;
  }
  if ((GetAsyncKeyState(VK_UP)) && vkflag != 4)// 
  {
   vkflag = 2; break;
  }
  if ((GetAsyncKeyState(VK_LEFT)) && vkflag != 3)// 
  {
   vkflag = 1; break;
  }
  if ((GetAsyncKeyState(VK_RIGHT)) && vkflag != 1)// 
  {
   vkflag = 3; break;
  }
  if (GetAsyncKeyState(VK_SPACE) && 0x20)//    
   while (1)
   {
    gotoxy(84, 16);
    printf("\033[40;36m      \033[0m");
    Sleep(200);
    gotoxy(84, 16);
    printf("            "); //  
    Sleep(150);
    if (GetAsyncKeyState(VK_SPACE) && 0x20)
     break;
   }
  break;
 }
 switch (vkflag)//  
 {
 case 1: snake.x[0] -= 2; break;// 
 case 2: snake.y[0]--; break;// 
 case 3: snake.x[0] += 2; break;// 
 case 4: snake.y[0]++; break;// 
 }
 gotoxy(snake.x[0], snake.y[0]);//    
 printf("◆");
 gotoxy(snake.x[1], snake.y[1]);//    
 printf("■");
 foodFlag = 0;
 fflush(stdin);
 return;
}
void foodcreate()
{
 n++;//    
 flag = 1;
    sorce += 10;//     10 
 if (sorce>=his_max )//   
 {
  his_max = sorce;
  gotoxy(82, 2);
  printf("                 ");
  gotoxy(82, 2);
  printf("★    :%d", his_max);
 }
 gotoxy(82, 3);//    
 printf("                 ");
 gotoxy(82, 3);
 printf("☆    :%d", sorce);

 while (flag==1)//    
 {  
   food.x = rand() % 74 + 4;//         
   food.y = rand() % 24 + 1;
   if (food.x % 2 != 0 && flag == 1)//      
   {
    food.x = food.x - 1;
    flag = 0;
   }
   if(map!=1)//      
   for (i = 0; i < snake.length; i++)
   {
    for(m=0;m<j;m++)
     if (obs.x[m] == food.x && obs.y[m] == food.y)
     {
      flag = 1;
      break;
     }
   }
   for (i = 0; i < snake.length; i++)//            
   {
    if (snake.x[i] == food.x && snake.y[i] == food.y)
    {
     flag = 1;
     break;
    }
   }
 }
    gotoxy(food.x, food.y); //    
 printf("●");
 snake.length++;//       1
 foodFlag = 1;//         
 return;
}
int  dead()
{
 if (snake.y[0] == 0 || snake.y[0] == map_y-1)//      
  return 0;
 if (snake.x[0] == 0 || snake.x[0] == map_x-2)//      
  return 0;
 if(map!=1)//       
 for (m = 0; m < j; m++)
  if (obs.x[m] == snake.x[0] && obs.y[m] == snake.y[0])
  {
   return 0;
  }
 for (i = 1; i < snake.length; i++)//      
 {
 if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0])
  return 0;
 }
 if (GetAsyncKeyState(VK_ESCAPE)&0x1B)//ESC     
 {
  ii = 5;
  return 0;
 }
 return 1;
}
void game()//     
{
 int i ;
 n = 0;//    
 vkflag = 1;
 sorce = 0;
 foodFlag = 0;
 system("cls");//    
 frame(1);//      (  )
 gotoxy(82, 2);
 printf("★    :%d", his_max);
 gotoxy(82, 3);
 printf("☆    :%d", sorce);
 gotoxy(84, 8);
 printf("      ");
 gotoxy(85, 9);
 printf("\033[40;33m    ↑      \033[0m");
 gotoxy(85, 10);
 printf("\033[40;33m  ←  →    \033[0m");
 gotoxy(85, 11);
 printf("\033[40;33m    ↓      \033[0m");
 gotoxy(83, 12);
 printf("       ");
 gotoxy(82, 18);
 printf("      /  ");
 gotoxy(83, 20);
 printf(" Shift    "); 
 gotoxy(82, 22);
 printf(" ESC     ");

 snake.length = org_len;//    (    )
 snake.x[0] = 50 ;
 snake.y[0] = 20;
 gotoxy(snake.x[0], snake.y[0]);
 printf("◆");
 for (i = 1; i < snake.length; i++)//   
 {
  Sleep(25);
  snake.x[i] = snake.x[i - 1] + 2;
  snake.y[i] = snake.y[0];
  gotoxy(snake.x[i], snake.y[i]);
  printf("■");
 }
 if (map >=1)
 {
  food.x = rand() % 44 + 4;//     (    )
  food.y = rand() % 24 + 1;
  if (food.x % 2 != 0)
   food.x = food.x - 1;
 }
 else {
  food.x = 14;//     (    )
  food.y = 3;
 }
 gotoxy(food.x, food.y);//       
 printf("\033[40;37m●\033[0m");
 Sleep(250);
 gotoxy(food.x, food.y);
 printf("  ");
 Sleep(250);
 gotoxy(food.x, food.y);
 printf("\033[40;37m●\033[0m");
 Sleep(250);
 obstacle();
 while(ii==1) //  
 {
  if (GetAsyncKeyState(VK_SHIFT))//    
  {
   gotoxy(93, 20);
   printf("\033[40;31m  \033[0m");//  
  }
  else { Sleep(100);
  gotoxy(93, 20);
  printf("  ");
  }
  Sleep(speed - (changespeed * n));//    
  control();//  
  if (snake.x[0] == food.x && snake.y[0] == food.y)//    
  foodcreate();
  if (dead() != 1) //        
  {
   gotoxy(36,12);
   printf("Game over");//    
   gotoxy(36, 14);
   printf("  T _ T  ");
   Sleep(500);
   while (1)
   {
    gotoxy(32, 16);
    printf("  Enter      ");
    Sleep(150);

    if (GetAsyncKeyState(VK_RETURN) & 0x0D)//    
    {
     fflush(stdin);
     ii = 5;
     return;
    }
    
   }
  } 
 }
}
void setup2()//    (      )
{
 gotoxy(34, 8);
 switch (org_len)//    
 {
 case 2: printf("               ◆■           >"); break;
 case 3: printf("           <   ◆■■         >"); break;
 case 4: printf("           <   ◆■■■       >"); break;
 case 5: printf("           <   ◆■■■■     >"); break;
 case 6: printf("           <   ◆■■■■■   >"); break;
 case 7: printf("           <   ◆■■■■■■  "); break;
 }
 gotoxy(34, 10);
 switch (speed)//    
 {
 case  50:printf("          <         "); break;
 case 100:printf("          <        >"); break;
 case 150:printf("                   >"); break;
 }
 gotoxy(34, 12);
 switch (changespeed)//    
 {
 case 0:printf("                   >"); break;
 case 5:printf("         <          "); break;
 }
 gotoxy(34, 14);
 switch (tail)//    
 {
 case 1:printf("                   >"); break;
 case 2:printf("         <    ~     >"); break;
 case 3:printf("         <   ・      "); break;
 }
 gotoxy(34, 16);
 switch (map)//    
 {
 case -1:printf("               2 >"); break;
 case 0:printf("         <     1 >"); break;
 case 1:printf("         <        >"); break;
 case 2:printf("         <        >"); break;
 case 3:printf("         <        >"); break;
 case 4:printf("         <         "); break;
 }
    gotoxy(70, 22);
    printf("       "); 

 for (i = 0; i < 7; i++)
 {
  gotoxy(31, 8 + 2 * i);
  printf("  ");
 }
 gotoxy(67, 22);
 printf("  ");
 switch (set_flag)
 {
 case 1:gotoxy(31,  8); break;
 case 2:gotoxy(31, 10); break;
 case 3:gotoxy(31, 12); break;
 case 4:gotoxy(31, 14); break;
 case 5:gotoxy(31, 16); break;
 case 6:gotoxy(67, 22); break;
 }
 printf("★");
 return;
}
void setup()//    (      )
{
 frame(2);
 set_flag=1;
 gotoxy(30, 3);
 printf("      ■■◆            ◆■■");
 gotoxy(26, 4);
 printf("■■■■■■                        ■■■■■■");
 setup2();
 fflush(stdin);
 while (1)
 {
  if (GetAsyncKeyState(VK_DOWN))// 
  {
   if (set_flag == 6)
    set_flag = 1;
   else set_flag++;
  }
  if (GetAsyncKeyState(VK_UP))// 
  {
   if (set_flag == 1)
    set_flag = 6;
   else set_flag--;
  }
  if (GetAsyncKeyState(VK_LEFT))// 
  {
   switch (set_flag)
   {
   case 1: if (org_len > 2)
    org_len--; break;
   case 2: if (speed < 150)
    speed += 50; break;
   case 3: if (changespeed == 5)
    changespeed = 0;break;
   case 4: if (tail > 1)
    tail--;break;
   case 5: if (map > -1)
    map--;break;
   case 6: break;
   }
  }
  if (GetAsyncKeyState(VK_RIGHT))// 
  {
   switch (set_flag)
   {
   case 1: if (org_len < 7)
    org_len++;break;
   case 2: if (speed > 50)
    speed -= 50;break;
   case 3: if (changespeed == 0)
    changespeed = 5;break;
   case 4: if (tail < 3)
    tail++;break;
   case 5: if (map < 4)
    map++;break;
   case 6:break;
   }
  }
  setup2();
  if ((GetAsyncKeyState(VK_RETURN)&0x0D)&&set_flag==6)
  {
   ii = 5;
   return;
  }
  Sleep(175);
 }
 return;
}
void instructions()//    
{
 system("cls");
 frame(2);
 gotoxy(10, 3);
 printf("    :");
 gotoxy(18, 5);
 printf("         ,           ,         ");
 gotoxy(18, 7);
 printf(" ↑←↓→                     ,    ");
 gotoxy(18, 9);
 printf("  ,      ,   10 ,         ,     ");
 gotoxy(18, 11);
 printf("                ,    。");
 gotoxy(18, 15);
 printf("    :     ,  Shift   ,         , ");
 gotoxy(18, 17);
 printf("                     。                          ");
 gotoxy(60, 20);
 printf("         ");
 gotoxy(3, 23);
 printf("       V1.0");
 while (1)//         
 {
  Sleep(100);
  (void)_getch();
  if (GetAsyncKeyState(VK_RETURN))//  
  {
   ii = 5;
   break;
  }
 }
}
void end()//    
{
 frame(2);
 gotoxy(44, 12);
 printf("     ");
 gotoxy(0, 26);
}
main()//    
{
 frame(2);
 loading();
 while (ii == 1 || ii == 2 || ii == 3||ii==5)
 {
  system("cls");
  Sleep(200); 
  menu(ii);
  chose();
  switch (ii)
  {
  case 1:game(); break;
  case 2:setup(); break;
  case 3:instructions(); break;
  case 4:break;
  case 5:ii = 1; continue;
  }
 }
 end();
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기