순 C 언어 로 뱀 먹 기 게임 을 하 겠 습 니 다.

15361 단어 C 언어탐식 사
본 논문 의 사례 는 C 언어 가 뱀 을 게 걸 스 럽 게 먹 는 게임 을 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
대학생 1 개,노동절 기간 4 일 동안 작은 게임 을 쓴다.
본 탐식 뱀 게임 코드 는 모두 531 줄 이 고 개발 환경 은 VS 이 며 게임 은 두 가지 모델 로 나 뉘 는데 표준 모델 과 초대형 지도 모델 로 다음 과 같은 소스 코드 를 자세히 보고 게임 에 들 어가 구체 적 인 내용 을 체험 한다.

//   
 
#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
//    
#define FrameX 5
#define FrameY 5
#define Frame_width 20
#define Frame_height 20
#define FrameX2 3
#define FrameY2 3
#define Frame_width2 55
#define Frame_height2 47
//    
int i, j, p, q, m;int a[2];
//     
struct Snack{ 
int x[200];
//  X  ; 
int y[200];
//  Y  ; 
int count;
//       ; 
int length;
//      ; 
int speed;
//      ;
};
 
//     
struct Food{ 
 int x;
//   X  ;
    int y;
//   Y  ;
};
 
//     
void gotoxy(HANDLE hOut, int x, int y){ COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(hOut, pos);}
 
//    
void menu(){
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
void over_game();
void start_game();
void start_game2(); 
int n; 
gotoxy(hOut, 50, 14);
printf("      "); 
gotoxy(hOut, 50, 16); 
printf("      【1】");
gotoxy(hOut, 50, 17);
printf("      【2】");
gotoxy(hOut, 50, 18);
printf("     【3】");
scanf_s("%d", &n); 
switch(n) 
{ 
case 1: 
 system("cls"); 
 start_game(); 
 over_game(); 
 break; 
case 2: 
 system("cls"); 
 start_game2(); 
 over_game();
 break; 
case 3: 
 system("cls"); 
 over_game();
 break; 
default: 
 printf("      ,     !"); 
 menu(); 
 break;
}}
//      2
void make_frame2()
{
 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
 //          
 gotoxy(hOut, 24, 13);
 //     
 printf("#");
 gotoxy(hOut, 14, 23);
 printf("#");
 gotoxy(hOut, 35, 20);
 //      
 printf("#");
 gotoxy(hOut, 40, 35);
 printf("#");
 gotoxy(hOut, 70, 6);
 //      
 printf("#");
 gotoxy(hOut, 80, 16);
 printf("#");
 gotoxy(hOut, 60, 45);
 //     
 printf("#");
 gotoxy(hOut, 28, 8);
 printf("#");
for (i = 0; i < 2*Frame_width2+1;i++) 
{ gotoxy(hOut, FrameX2 + i, FrameY2);
//   
printf("-"); gotoxy(hOut, FrameX2 + i, Frame_height2+FrameY2);
//    
printf("-"); } 
for (i = 0; i < Frame_height2-1; i++) 
{ gotoxy(hOut, FrameX2, FrameY2 + i+1);
//    
printf("|"); 
gotoxy(hOut, FrameX2+2*Frame_width2, FrameY2 + i+1);
//   
printf("|"); }}
//      
void make_frame()
{
 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
 //         
 gotoxy(hOut, 24, 13); 
 //     
 printf("#"); 
 gotoxy(hOut, 14, 23); 
 printf("#"); 
 for (i = 0; i < 2 * Frame_width + 1; i++)
 { gotoxy(hOut, FrameX + i, FrameY);
 //    
 printf("-");
 gotoxy(hOut, FrameX + i, Frame_height + FrameY);
 //   
 printf("-"); } 
 for (i = 0; i < Frame_height - 1; i++)
 { gotoxy(hOut, FrameX, FrameY + i + 1);
 //   
 printf("|");
 gotoxy(hOut, FrameX + 2 * Frame_width, FrameY + i + 1);
 //    
 printf("|"); }}
 
//    
void intt_snack(struct Snack* snack)
{ 
 snack->x[0] = FrameX + 5;
 //     ;
 snack->y[0] = FrameY + 6;
 //     ; 
 snack->speed = 200;
 //    
 snack->length = 3;
 //    ; 
 snack->count = 0; 
 for (i = 1; i < snack->length; i++) 
    { 
 snack->x[i] = snack->x[i - 1]+2; 
    snack->y[i] = snack->y[i - 1];
    }
}
 
//    2
void intt_snack2(struct Snack* snack)
{ snack->x[0] = FrameX2 + 5;
//     ; 
snack->y[0] = FrameY2 + 6;
//     ;
snack->speed = 200;
//    ; 
snack->length = 3;
//    ; 
snack->count = 0;
for (i = 1; i < snack->length; i++) 
{ snack->x[i] = snack->x[i - 1] + 2;
snack->y[i] = snack->y[i - 1]; }}
 
//   
void move_snack(HANDLE hOut,struct Snack* snack)
{ gotoxy(hOut, snack->x[0], snack->y[0]); printf(" "); 
//    ;
for (i = 1; i < snack->length; i++) 
{ 
  snack->x[i - 1] = snack->x[i];
  snack->y[i - 1] = snack->y[i]; 
}}
 
//   
void print_snack(HANDLE hOut, struct Snack* snack)
{ 
 for (i = 0; i < snack->length; i++) 
{ 
  gotoxy(hOut, snack->x[i], snack->y[i]); 
if (i == 0) 
{ 
 printf("*");
} 
else if 
(i < snack->length - 1) 
{
 printf("0"); 
} 
else 
{ 
 printf("@"); 
} 
 }
}
 
//    
void print_food(HANDLE hOut, struct Snack*snack,struct Food* food){ srand((unsigned)time(0));
while (1) 
{ 
food->x = rand() % 2*Frame_width+1; food->y = rand() % Frame_height;
if (food->x == 0 || food->y == 0) { continue; }
food->x = food->x+FrameX;
food->y = food->y + FrameY;
for (i = 0; i < snack->length; i++) 
{ 
 if (food->x == snack->x[i] && food->y == snack->y[i]) 
{ 
  break;
 } 
} 
if (i == snack->length) 
{ 
 gotoxy(hOut, food->x, food->y);
printf("$"); 
break;
} 
}
}
 
//    2
void print_food2(HANDLE hOut, struct Snack* snack, struct Food* food)
{ srand((unsigned)time(0));
while (1) 
{ 
 food->x = (rand() % (3*Frame_width2 + 1))+FrameX2;
food->y = (rand() % Frame_height2)+FrameY2; 
if (food->x == 0 || food->y == 0) 
{ 
 continue; 
} 
for (i = 0; i < snack->length; i++)
{ 
 if (food->x == snack->x[i] && food->y == snack->y[i])
{
  break; 
 } 
} 
if (i == snack->length)
{ 
 gotoxy(hOut, food->x, food->y); printf("$");
break; 
}
}
}
 
//   
void eat_food(HANDLE hOut, struct Snack *snack, struct Food *food)
{ if (snack->x[snack->length-1] == food->x && snack->y[snack->length-1] == food->y)
{ snack->length++; 
for (i = snack->length; i >= 1; i--) 
{ snack->x[i] = snack->x[i - 1]; 
snack->y[i] =snack->y[i - 1]; } snack->x[0] = a[0]; 
snack->y[0] = a[1]; print_food(hOut,snack,food); 
snack->count++; if (snack->count % 5 == 0) snack->speed -= 20; }}
 
//  
void through_wall(HANDLE hOut, struct Snack* snack, char ch)
{ if (ch == 72 && snack->y[snack->length - 1] == FrameY) 
{ snack->y[snack->length - 1] =Frame_height+4; 
//           ,   
} if (ch == 80 && snack->y[snack->length - 1] == FrameY + Frame_height)
{ snack->y[snack->length - 1] = FrameY+1; 
//           ,  
} if (ch == 75 && snack->x[snack->length - 1] == FrameX-1) 
{ snack->x[snack->length - 1] =2*Frame_width + FrameX-1;
//           ,   
} if (ch == 77 && snack->x[snack->length - 1] == 2*Frame_width+FrameX+1)
{ snack->x[snack->length - 1] = FrameX+1; 
//           ,  
}}
 
//  2
void through_wall2(HANDLE hOut, struct Snack* snack, char ch)
{ if (ch == 72 && snack->y[snack->length - 1] == FrameY2) 
{ snack->y[snack->length - 1] = Frame_height2 + 4;
//           ,   
} if (ch == 80 && snack->y[snack->length - 1] == FrameY2 + Frame_height2)
{ snack->y[snack->length - 1] = FrameY2 + 1; 
//           ,   
} if (ch == 75 && snack->x[snack->length - 1] == FrameX2 - 1) 
{ snack->x[snack->length - 1] = 2 * Frame_width2 + FrameX2 - 1; 
//           ,   
} if (ch == 77 && snack->x[snack->length - 1] == 2 * Frame_width2 + FrameX2 + 1) 
{ snack->x[snack->length - 1] = FrameX2 + 1;
//           ,   
}}
 
//       
int snack_die(struct Snack *snack){ 
 for (i = 0; i < snack->length - 1; i++) 
 { if (snack->x[snack->length - 1] == snack->x[i] && snack->y[snack->length - 1] == snack->y[i]) 
  return 0; 
 if (snack->x[snack->length - 1] == 24 && snack->y[snack->length - 1] == 13) 
  return 0;
 if (snack->x[snack->length - 1] == 14 && snack->y[snack->length - 1] == 23)
  return 0; } 
 return 1;}
 
//       2
int snack_die2(struct Snack* snack)
{ for (i = 0; i < snack->length - 1; i++) 
{ if (snack->x[snack->length - 1] == snack->x[i] && snack->y[snack->length - 1] == snack->y[i]) 
return 0;
if (snack->x[snack->length - 1] == 24 && snack->y[snack->length - 1] == 13) 
return 0;
if (snack->x[snack->length - 1] == 14 && snack->y[snack->length - 1] == 23) 
return 0; 
if (snack->x[snack->length - 1] == 35 && snack->y[snack->length - 1] == 20) 
return 0;
if (snack->x[snack->length - 1] == 40 && snack->y[snack->length - 1] == 35) 
return 0;
if (snack->x[snack->length - 1] == 70 && snack->y[snack->length - 1] == 6) 
return 0; 
if (snack->x[snack->length - 1] == 80 && snack->y[snack->length - 1] == 16) 
return 0; 
if (snack->x[snack->length - 1] == 60 && snack->y[snack->length - 1] == 45) 
return 0;
if (snack->x[snack->length - 1] == 28 && snack->y[snack->length - 1] == 8) 
return 0; } 
return 1;}
 
//    
void start_game(){ unsigned char ch = 77;
//               
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
//          
struct Snack s, * snack = &s;
struct Food f, * food = &f; make_frame(); intt_snack(snack);
print_food(hOut,snack, food);
while (1) 
{ a[0] = snack->x[0]; 
//        
a[1] = snack->y[0];
//        
 j = 0; 
if (_kbhit()) 
{
 /*         ,    ,ch       */ 
ch = _getch(); 
if (_kbhit()) 
{
/*       ,    */
 Sleep(20); 
j = 1; } }
 switch (ch) 
 { 
 case 72:
 {
  /*      */
  move_snack(hOut, snack); 
  //   
  snack->y[snack->length - 1] -= 1; 
  //         ,  1
 break;
 }
 case 80: 
 {
  /*      */
  move_snack(hOut, snack); 
  //    
  snack->y[snack->length - 1] += 1; 
  //         ,  1
 break; 
 } 
 case 75: {
  /*      */ 
  move_snack(hOut, snack); 
  //    
  snack->x[snack->length - 1] -= 2;
  //         ,  2 
 break;
 } 
 case 77: 
 {
  /*      */
  move_snack(hOut, snack); 
  //    
  snack->x[snack->length - 1] += 2;
  //         ,  2 
  break; } } 
 through_wall(hOut, snack, ch); 
 eat_food(hOut, snack, food); 
 //    
 print_snack(hOut, snack); 
 //   
 if (snack_die(snack) == 0) 
 {
  /*       :1.      2. Esc  3.   50ms */
  gotoxy(hOut, 15, 15); 
  printf("Game Over"); Sleep(1000); 
  break; 
 }
 if(j == 0) 
 { 
  Sleep(snack->speed); 
 //     
 } 
 else 
 {
  Sleep(10);
 } 
}
}
 
void start_game2(){ unsigned char ch = 77; 
//                
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//          
struct Snack s, * snack = &s; struct Food f, * food = &f; 
make_frame2();
intt_snack2(snack);
print_food(hOut, snack, food);
while (1) { a[0] = snack->x[0]; 
//         
a[1] = snack->y[0];
//         
j = 0; if (_kbhit()) 
{
 /*         ,    ,ch       */ 
 ch = _getch(); 
 if (_kbhit()) {
 /*       ,    */
 Sleep(20); j = 1; } } 
switch (ch) { 
case 72: {
 /*      */
 move_snack(hOut, snack);
 //   
 snack->y[snack->length - 1] -= 1; 
 //         ,  1
break;
} case 80: {
 /*      */
 move_snack(hOut, snack);
 //    
 snack->y[snack->length - 1] += 1; 
 //         ,  1 
break; } case 75: {
 /*      */ 
 move_snack(hOut, snack);
 //   
 snack->x[snack->length - 1] -= 2; 
 //         ,  2
break; } case 77: {
 /*      */ 
 move_snack(hOut, snack);
 //    
 snack->x[snack->length - 1] += 2;
 //         ,  2 
 break; } } 
through_wall2(hOut, snack, ch); 
eat_food(hOut, snack, food); 
//    
print_snack(hOut, snack); 
//    
if (snack_die2(snack) == 0) 
{
 /*       :1.      2. Esc  3.   50ms */ 
 gotoxy(hOut, 30, 18); 
 printf("Game Over"); 
 Sleep(1000);
 break; } 
if (j == 0) { Sleep(snack->speed); 
//     
}
else { Sleep(10); } }}
void choose()
{ HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
void start_game();
 void start_game2();
 void over_game();
 int n; 
gotoxy(hOut, 10, 20); 
printf("Continue [1]");
gotoxy(hOut, 30, 20);
 printf("Continue [2]");
gotoxy(hOut, 50, 20);
 printf("Exit [3]");
 gotoxy(hOut, 70, 20); 
printf("  :"); 
scanf_s("%d", &n); 
switch (n) 
{ case 1: system("cls"); 
start_game(); 
over_game();
break; 
case 2: 
system("cls"); 
start_game2(); 
over_game(); 
break; 
case 3:
exit(0); 
break;
default:
 printf("※※      ,     ※※");
 system("pause >nul"); over_game(); 
 choose(); break; }}
 
/******    *******************************************************************/
void over_game()
{
 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
 system("cls");
 printf("







\t\t\t\t


"); choose(); system("pause"); } void main(){ menu(); start_game(); // start_game2(); // over_game(); // }
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기