C語言實現貪吃蛇遊戲演示

本文實例為大傢分享瞭C語言實現貪吃蛇遊戲的具體代碼,供大傢參考,具體內容如下

IDE用的是 VS2019

先看效果

 

代碼全覽

game.h

#pragma once
#define  _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>

#define PLATFORM 1 //運行的系統 1為win 0為linux
 
#define MAPWIDTH 15 //地圖寬度,包括墻
#define MAPHEIGHT 15  //地圖高度,包括墻
#define SNAKELENGTH (MAPHEIGHT - 2) * (MAPWIDTH - 2)
 
//結構體聲明
struct Body
{
 int isExist;
 int x;
 int y;
};
 
 
struct Food {
 int x;
 int y;
};
 
 
void game();
 
void initWall(char wall[MAPHEIGHT][MAPWIDTH], int mapWidth, int mapHeight);
void displayMap(int mapWidth, int mapHeight, struct Body snake[], int snakelength, struct Food food);
void clearScreen();
void inputProcess(char* pinput);
void initSnake(struct Body snake[SNAKELENGTH], int length);
void generateFood(struct Food* food, struct Body snake[]);
int isWall(int x, int y);
int isSnake(int x, int y, struct Body snake[], int lengh);
void control(char input, struct Body snake[]);
void generateFood(struct Food* food, struct Body snake[]);
int isFood(int x, int y, struct Food* food);
int isEat(struct Body snake[], struct Food* pfood);
void bodyMove(struct Body snake[], int* bodyLength);
int isInBody(struct Body snake[], int lengh);

GameStart.c

#include "game.h"
 
void displayMenu() {
 
 printf("########################\n");
 printf("###### 貪吃蛇遊戲 #######\n");
 printf("########################\n");
 printf("------------------------\n");
 printf("       1.開始遊戲        \n");
 printf("       0.退出遊戲        \n");
 printf("------------------------\n");
 printf("請輸入選項:>");
 
 char ch;
 scanf("%c", &ch);
 getchar();
 switch (ch)
 {
 case '1': {
  game();
  break;
 }
 case '0': {
  exit(0);
  break;
 }
 default:
  printf("輸入錯誤,請重新輸入:>");
  break;
 }
  
 
}
 
int main(void) {
 while (1) {
  clearScreen();
  displayMenu();
 
  clearScreen();
  
 }
 
 return 0;
}

game.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

//遊戲邏輯
void game() {
 
 //分數
 int score = 0;
 
 //遊戲狀態 0為勝利 1為咬到蛇身 2為撞到墻上 
 int gameState = 0;
 
 //輸入狀態
 char input = 0;
 //墻
 char wall[MAPHEIGHT][MAPWIDTH];
 //創建蛇結構體數組
 struct Body snake[SNAKELENGTH];
 
 //創建食物結構體
 struct Food food = { 5,5 };
 
 
 //初始化蛇
 initSnake(snake, SNAKELENGTH);
 
 //初始化墻
 initWall(wall, MAPWIDTH, MAPHEIGHT);
 
 //生成食物
 generateFood(&food, snake);
 

 while (1)
 {
 
  //清屏
  clearScreen();
 
 
  control(input, snake);
  //顯示地圖
  displayMap(MAPWIDTH, MAPHEIGHT, snake, SNAKELENGTH, food);
  printf("得分:%d\n", score);
  //printf("food:%d %d\n", food.x, food.y);
  //printf("snake:%d %d", snake[0].x, snake[0].y);
  //處理輸入
  inputProcess(&input);

 
  //撞到蛇身,遊戲失敗
  if (isInBody(snake, SNAKELENGTH)) {
   gameState = 1;
   break;
  }
  //撞到墻上,遊戲失敗
  if (isWall(snake[0].x, snake[0].y)) {
   gameState = 2;
   break;
  }
 
  //吃到食物加分,蛇身加一
  if (isEat(snake, &food)) {
   score++;
   snake[score].isExist = 1;
   snake[score].x = snake[score - 1].x;
   snake[score].y = snake[score - 1].y;
 
   if (score == SNAKELENGTH - 1) {
    //遊戲勝利
    gameState = 0;
    break;
   }
 
   generateFood(&food, snake);
  }
 
  //蛇身移動
  bodyMove(snake, &score);
 
 }
 
 //勝負顯示
 switch (gameState)
 {
 
 case 1: {
  printf("咬到蛇身,遊戲結束!\n");
 
  break;
 }
 case 2: {
  printf("撞到墻上,遊戲結束!\n");
  break;
 }
 case 0: {
  printf("遊戲勝利!\n");
  break;
 }
 default:
 
  break;
 }
 printf("按回車鍵退出");
 getchar();
 
}
 

//清除屏幕
void clearScreen() {
 if (PLATFORM) {
  system("cls");
 }
 else {
  system("clear");
 }
 
 
 printf("\033c");
}
 
//輸入處理
void inputProcess(char* pinput) {
 int t = (int)time(NULL);
 while (1) {
  if (_kbhit()) {
   switch (getch())
   {
   case 'w': {
    if (*pinput != 's') {
     *pinput = 'w';
    }
 
    break;
   }
   case 's':
   {
    if (*pinput != 'w') {
     *pinput = 's';
 
    }
 
    break;
   }
   case 'a': {
    if (*pinput != 'd') {
     *pinput = 'a';
    }
 
    break;
   }
   case 'd': {
    if (*pinput != 'a') {
     *pinput = 'd';
    }
    break;
   }
     /* case ' ': {
       *pinput = ' ';
       break;
      }*/
   default:
    break;
   }
  }
 
  if ((int)time(NULL) - t == 1) {
   //printf("%c\n", *pinput);
   //一秒一幀
 
   break;
  }
  /*if (*pinput == ' ') {
   continue;
  }*/
 
 }
 
}
 
//初始化墻
//'#'墻
//' '空
void initWall(char wall[MAPHEIGHT][MAPWIDTH], int mapWidth, int mapHeight) {
 
 
 for (int i = 0; i < mapHeight; i++) {
 
  for (int j = 0; j < mapWidth; j++) {
   if (i == 0 || i == mapHeight - 1) {
    wall[i][j] = '#';
   }
   else if (j == 0 || j == MAPWIDTH - 1) {
    wall[i][j] = '#';
   }
   else {
    wall[i][j] = ' ';
   }
 
  }
 }
}
 
 
//初始化蛇狀態,位置
void initSnake(struct Body snake[SNAKELENGTH], int length) {
 
 for (int i = 0; i < length; i++) {
 
  if (i == 0)
  {
 
   snake[i].x = MAPWIDTH / 2;
   snake[i].y = MAPHEIGHT / 2;//蛇出生位置,即蛇頭初始位置
   snake[i].isExist = 1;
 
  }
  else {
   snake[i].isExist = 0;
   snake[i].x = 0;
   snake[i].y = 0;
  }
 
 
 }
 
 
}
 
//生成食物
void generateFood(struct Food* food, struct Body snake[]) {
 int x;
 int y;
 srand((unsigned int)time(NULL));
 do {
 
  x = (rand() % MAPHEIGHT) + 1;
  y = (rand() % MAPWIDTH) + 1;
 } while (isSnake(x, y, snake, SNAKELENGTH) || isWall(x, y));
 
 (*food).y = y;
 (*food).x = x;
}
 
//判斷是否是墻
int isWall(int x, int y) {
 if (y <= 1 || y >= MAPHEIGHT || x <= 1 || x >= MAPWIDTH) {
  return 1;
 }
 return 0;
}
 
 
//判斷是否是蛇
int isSnake(int x, int y, struct Body snake[], int lengh) {
 for (int i = 0; i < lengh; i++) {
  if (snake[i].isExist == 1 && snake[i].x == x && snake[i].y == y) {
   return 1;
  }
 
 }
 return 0;
}
 
//判斷是否撞到蛇身
int isInBody(struct Body snake[], int lengh) {
 for (int i = 1; i < lengh; i++) {
  if (snake[i].isExist == 1 && snake[i].x == snake[0].x && snake[i].y == snake[0].y) {
   return 1;
  }
 }
 return 0;
}
 
//判斷是否是食物
int isFood(int x, int y, struct Food* food) {
 if ((*food).x == x && (*food).y == y) {
  return 1;
 }
 return 0;
}
 
//顯示遊戲地圖
void displayMap(int mapWidth, int mapHeight, struct Body snake[], int snakelength, struct Food food) {
 int x;
 int y;
 
 
 for (int i = 0; i < mapHeight; i++) {
  y = i + 1;
  for (int j = 0; j < mapWidth; j++) {
   x = j + 1;
   if (isWall(x, y)) {
    printf("# ");
   }
   else if (isSnake(x, y, snake, snakelength)) {
    if (snake[0].x == x && snake[0].y == y) {
     printf("@ ");//蛇頭
    }
    else {
     printf("* ");//蛇身
    }
 
   }
   else if (isFood(x, y, &food)) {
    printf("+ ");
   }
   else {
    printf("  ");
   }
 
 
  }
  printf("\n");
 }
 
 
}
 
//方向控制
void control(char input, struct Body snake[]) {
 switch (input) {
 case 'w': {
  snake[0].y -= 1;
  break;
 }
 case 'a': {
  snake[0].x -= 1;
  break;
 }
 case 's': {
  snake[0].y += 1;
  break;
 }
 case 'd': {
  snake[0].x += 1;
  break;
 }
 }
}
 
//判斷是否吃到食物
int isEat(struct Body snake[], struct Food* pfood) {
 if (isFood(snake[0].x, snake[0].y, pfood)) {
  return 1;
 }
 return 0;
}
 
//移動蛇身
void bodyMove(struct Body snake[], int* bodyLength) {
 if (*bodyLength) {
 
  for (int i = *bodyLength; i >= 1; i--) {
 
   snake[i].x = snake[i - 1].x;
   snake[i].y = snake[i - 1].y;
  }
 }
 
 
}

相關思路有空再寫。

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: