C 언어 탐식 뱀 놀이(명령 행)

7711 단어 C 언어탐식 사
이것 은 순수한 C 언어 로 쓴 뱀 탐식 게임 입 니 다.여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.

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

#define SNAKE_LENGTH 100//        
#define SCREEN_WIDETH 80
#define SCREEN_HEIGHT 30

//         
struct coor{
 int x;
 int y;
};

//    
enum CH {
 right = VK_RIGHT,
 left = VK_LEFT,
 up = VK_UP,
 down = VK_DOWN
};

//      
struct snake{
 int len;//      
 struct coor coord[SNAKE_LENGTH];//       
 enum CH CH;//      
 int SPEED;
 int flag;//        1      0    
}snake;

//      
void gotoxy(int x, int y)
{
 COORD pos;
 pos.X = x;
 pos.Y = y;
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

//       
void init_sence()
{
 //      
 for (int i = 0; i < SCREEN_WIDETH; i += 2)
 {
 gotoxy(i,0);
 printf("■");
 gotoxy(i, SCREEN_HEIGHT);
 printf("■");
 }
 //      
 for (int i = 0; i <=SCREEN_HEIGHT; i++)
 {
 gotoxy(0, i);
 printf("■");
 gotoxy(SCREEN_WIDETH,i);
 printf("■");
 }
 //      
 gotoxy(SCREEN_WIDETH + 5, 2);
 printf("\t\t   ");
 gotoxy(SCREEN_WIDETH + 5, 6);
 printf("2018//12//1");
 gotoxy(SCREEN_WIDETH + 5, 8);
 printf("  :   ");
 gotoxy(SCREEN_WIDETH + 5, 10);
 printf("F1:  \tF2:  ");
 gotoxy(SCREEN_WIDETH + 5, 12);
 printf("CTRL:  \t  :  ");
 gotoxy(SCREEN_WIDETH + 5, 14);
 printf("ESC:    ");
 gotoxy(SCREEN_WIDETH + 5, 28);
 printf("  :QQ:2862841130:::");
}

struct foodcoord {
 int x;
 int y;
 int flag;//       
}food;


//**  c  **


#include"snake.h"
//    
void move_snake();
//   
void draw_snake();
//    
void creatfood();
//         
void eatfood();
//       
void SnakeState();


int main()
{
 //      
 system("mode con cols=110 lines=31");
 //    
 SetConsoleTitleA("   ");


 //    
begin:
 snake.CH = VK_RIGHT;//     
 snake.len = 5; //     
 snake.SPEED = 300;//         
 snake.coord[1].x = SCREEN_WIDETH / 2;//        
 snake.coord[1].y = SCREEN_HEIGHT / 2;
 snake.coord[2].x = SCREEN_WIDETH / 2-2;//        
 snake.coord[2].y = SCREEN_HEIGHT / 2;
 snake.coord[3].x = SCREEN_WIDETH / 2-4;//        
 snake.coord[3].y = SCREEN_HEIGHT / 2;

 //       
 food.flag = 1;//1        0        

   //       
 snake.flag = 1;//1  0 

 init_sence();//       
 while (1)
 {
 draw_snake();//  
 Sleep(snake.SPEED);//      
 move_snake();//   
 if(food.flag)
 creatfood();//    
 eatfood();//        
 SnakeState();//       
 if (!snake.flag)break;
 }
 system("cls");
 gotoxy(SCREEN_WIDETH/2, SCREEN_HEIGHT/2-4);
 printf(" GAME OVER!!!");
 gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+2);
 printf("     :\t\t\t%d ",snake.len-1);
 gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+4);
 printf("     :\t\t\tCTRL ");
 gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+6);
 printf("          :\t\tESC");

 while (1)
 {
 if (GetAsyncKeyState(VK_CONTROL))
 {
  system("cls");
  goto begin;
 }
 else if (GetAsyncKeyState(VK_ESCAPE))
  return 0;
 }
}

//    
void move_snake()
{
 //         
 if (GetAsyncKeyState(up))
 {
 if(snake.CH!=down)snake.CH = up;
 }
 else if (GetAsyncKeyState(down))
 {
 if (snake.CH != up)snake.CH = down;
 }
 else if (GetAsyncKeyState(right))
 {
 if (snake.CH != left)snake.CH = right;
 }
 else if (GetAsyncKeyState(left))
 {
 if (snake.CH != right)snake.CH = left;
 }
 else if (GetAsyncKeyState(VK_F1))
 {
 if(snake.SPEED>=100)snake.SPEED -= 50;
 }
 else if (GetAsyncKeyState(VK_F2))
 {
 if (snake.SPEED <= 3000)snake.SPEED += 100;
 }
 //               
 switch (snake.CH)
 {
 case right:snake.coord[1].x += 2; break;
 case left:snake.coord[1].x -= 2; break;
 case up:snake.coord[1].y -= 1; break;
 case down:snake.coord[1].y += 1; break;
 }
 
 
}

//   
void draw_snake()
{
 //    
 gotoxy(snake.coord[1].x, snake.coord[1].y);
 printf("□");

 //    ,    for    
 for (int i = 2; i <= snake.len; i++)
 {
 gotoxy(snake.coord[i].x, snake.coord[i].y);
 printf("□");
 }
 //    
 gotoxy(snake.coord[snake.len].x, snake.coord[snake.len].y);
 printf(" ");

 //      
 for (int i = snake.len; i >1; i--)
 {
 snake.coord[i].x = snake.coord[i - 1].x;
 snake.coord[i].y = snake.coord[i - 1].y;
 }
 
 gotoxy(0, 0);
 printf("■");
 gotoxy(85, 25);
 printf("  :%d ", snake.len-1);
 
}

//    
void creatfood()
{
 //      
 srand((unsigned)time(NULL));
 if(food.flag)
 while (1)
 {
 food.x = rand() % 80;
 food.y = rand() % 30;
 if (food.x % 2 == 0 && food.x >= 2 && food.x <= 78 && food.y > 1 && food.y < 30)
 {
  int flag = 0;
  //                 
  for (int i = 1; i <= snake.len; i++)
  {
  if (snake.coord[i].x == food.x&&snake.coord[i].y == food.y)
  {
   flag = 1;
   break;
  }
  }
  if (flag)continue;
  //    
  else
  {
  gotoxy(food.x, food.y);
  printf("⊙");
  food.flag = 0;
  break;
  }
 }
 }
 food.flag = 0;
}

//         
void eatfood()
{
 //              
 if (food.x == snake.coord[1].x&&food.y == snake.coord[1].y)
 {
  snake.len+=1;
  food.flag = 1;
 }
}

//       
void SnakeState()
{
 if (snake.coord[1].x < 2 || snake.coord[1].x>78 || snake.coord[1].y < 1 || snake.coord[1].y>29)
 snake.flag = 0;

 for (int i = 2; i <= snake.len; i++)
 {
 if (snake.coord[1].x == snake.coord[i].x&&snake.coord[1].y == snake.coord[i].y)
  snake.flag = 0;
 }
 
}
더 많은 재 미 있 는 클래식 게임 을 통 해 주 제 를 실현 하고 여러분 에 게 공유 합 니 다.
C++클래식 게임 모음
python 클래식 게임 모음
python 러시아 블록 게임 집합
JavaScript 클래식 게임 을 계속 합 니 다.
자바 클래식 게임 모음
javascript 고전 게임 모음
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기