欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C语言实现贪吃蛇游戏演示

程序员文章站 2022-06-24 20:51:59
本文实例为大家分享了c语言实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下ide用的是 vs2019先看效果代码全览game.h#pragma once#define _crt_secure_no...

本文实例为大家分享了c语言实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下

ide用的是 vs2019

先看效果

C语言实现贪吃蛇游戏演示 

代码全览

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;
  }
 }
 
 
}

相关思路有空再写。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: C语言 贪吃蛇