程序笔记   发布时间:2022-06-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C语言贪吃蛇经典小游戏大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

一、贪吃蛇小游戏简介:

        用上下左右控制蛇的方向,寻找吃的东西,每吃一口就能得到一定的积分,而且蛇的身子会越吃越长,身子越长玩的难度就越大,不能碰墙,也不能咬到自己的身体,等到了一定的分数,就能过关。 

二、函数框架

C语言贪吃蛇经典小游戏

三、数据结构

typedef struct Snake 
{ 
  size_t x; //行 
  size_t y; //列 
  struct Snake* next; 
}Snake,*pSnake; 

定义蛇的结构体,利用单链表来表示蛇,每个结点为蛇身体的一部分。 

四、代码实现(vs2010  c语言)

1.Snake.h

#ifndef __SNAKE_H__ 
#define __SNAKE_H__ 
 
#include <stdio.h> 
#include <stdlib.h> 
#include <windows.h> 
#include <time.h> 
#include <malloc.h> 
#include <assert.h> 
//标识地图大小 
#define ROW_MAP 10  //地图的行 
#define Col_MAP 20  //地图的列 
#define succesS_score 10//通关分数 
enum Direction //蛇行走的方向 
{ 
  R,//右 
  L,//左 
  U,//上 
  D //下 
}Direction; 
 
enum State 
{ 
  ERROR_SELF,//咬到自己 
  ERROR_WALL,//撞到墙 
  norMAL,//正常状态 
  succesS   //通关 
}State; 
 
typedef struct Snake 
{ 
  size_t x; //行 
  size_t y; //列 
  struct Snake* next; 
}Snake,*pSnake; 
 
 
voID StartGame(); 
voID RunGame(); 
voID EndGame(); 
 
#endif 

2.Snake.c

#include "Snake.h" 
 
 
pSnake head = NulL; //定义蛇头指针 
pSnake Food = NulL; //定义食物指针 
 
int sleeptime = 500;//间隔时间,用来控制速度 
int score = 0; //总分 
int everyscore = 1; //每步得分 
 
//定义游戏中用到的符号 
const char food = '#'; 
const char snake = '*'; 
 
voID Pos(int x,int y)  //控制输出光标 
{ 
  COORD pos; //pos为结构体 
  pos.X = x; //控制列 
  pos.Y = y; //控制行 
  SetConsolecursorposition(GetStdHandle(STD_OUTPUT_HANDLE),pos);//读取标准输出句柄来控制光标为pos 
} 
 
voID Face() 
{ 
  system("color 0C"); 
  printf("*******************************************************\n"); 
  printf("*        Welcome to Snake Game!        *\n"); 
  printf("*                           *\n"); 
  printf("*       ->开始游戏请按 enter键         *\n"); 
  printf("*       ->退出游戏请按 esc键          *\n"); 
  printf("*       ->暂停游戏请按 space键         *\n"); 
  printf("*       ->通过上下左右键来控制蛇的移动     *\n"); 
  printf("*       ->通过F1键减速    F2键加速     *\n"); 
  printf("*******************************************************\n"); 
} 
voID Map()  //初始化地图 
{ 
  int i = 0; 
  for(i = 0; i<Col_MAP; i+=2)  //打印上下边框(每个■占用两列) 
  { 
    Pos(i,0); 
    printf("■"); 
    Pos(i,ROW_MAP-1); 
    printf("■"); 
  } 
  for(i = 0; i<ROW_MAP; i++)  //打印左右边框 
  { 
    Pos(0,i); 
    printf("■"); 
    Pos(Col_MAP-2,i); 
    printf("■"); 
  } 
} 
voID PrintSnake() //打印蛇 
{ 
  pSnake cur = head; 
  while(cur) 
  { 
    Pos(cur->y,cur->X); 
    printf("%c",snakE); 
    cur = cur->next; 
  } 
} 
voID InitSnake()  //初始化蛇身 
{ 
  int initNum = 3; 
  int i = 0; 
  pSnake cur; 
  head = (pSnakE)malloc(sizeof(SnakE)); 
  head->x = 5; 
  head->y = 10; 
  head->next = NulL; 
 
  cur = head; 
  for(i = 1; i < initNum; i++) 
  { 
    pSnake newNode = (pSnakE)malloc(sizeof(SnakE)); 
    newNode->x = 5+i; 
    newNode->y = 10; 
    newNode->next = NulL; 
    cur->next = newNode; 
    cur = cur->next; 
  } 
 
  PrintSnake(); 
} 
 
voID CreateFood() //在地图上随机产生一个食物 
{ 
  pSnake cur = head; 
  Food = (pSnakE)malloc(sizeof(SnakE)); 
 
  //产生x~y的随机数 k=rand()%(Y-X+1)+X; 
  srand((unsigned)time(NulL)); 
  Food->x = rand()%(ROW_MAP-2 - 1 + 1)+1; 
  Food->y = rand()%(Col_MAP-3 - 2 + 1)+2; 
  Food->next = NulL; 
  while(cur)  //检查食物是否与蛇身重合 
  { 
    if(cur->x == Food->x && cur->y == Food->y) 
    { 
      free(Food); 
      Food = NulL; 
      CreateFood(); 
      return; 
    } 
    cur = cur->next; 
  } 
  Pos(Food->y,Food->X); 
  printf("%c",food); 
} 
 
voID StartGame() //游戏开始的所有设置 
{ 
  Face(); 
  system("pause"); 
 
  if(GetAsyncKeyState(VK_RETURN)) 
  { 
    system("cls"); 
    Pos(Col_MAP+5,1); 
    printf("当前分数/通关分数:"); 
    Pos(Col_MAP+20,1); 
    printf("%d/%d",score,succesS_score); 
    Pos(Col_MAP+5,2); 
    printf("当前分每步得分:"); 
    Pos(Col_MAP+20,2); 
    printf("%d",everyscore); 
    Pos(Col_MAP+5,3); 
    printf("\n"); 
    Pos(Col_MAP+5,4); 
    printf("速度越快 得分越高哦!!\n"); 
 
    Map(); 
    InitSnake(); 
    CreateFood(); 
  } 
  else if(GetAsyncKeyState(VK_ESCAPE)) 
  { 
    exit(0); 
  } 
} 
 
int IsCrossWall()       //判断是否碰到墙 
{ 
  if(head->x <= 0 || head->x >= ROW_MAP-1 
    ||head->y <= 1 || head->y >= Col_MAP-2) 
  { 
    State = ERROR_WALL; 
    return 0; 
  } 
  return 1; 
} 
 
int IsEatSelf(pSnake newhead) //判断是否咬到自己 
{ 
  pSnake cur = head; 
  assert(newhead); 
  while(cur) 
  { 
    if(cur->x == newhead->x && cur->y == newhead->y) 
    { 
      State = ERROR_SELF; 
      return 0; 
    } 
    cur = cur->next; 
  } 
  return 1; 
} 
 
int IsFood(pSnake pos) //判断该位置是不是食物 
{ 
  assert(pos); 
  if(pos->x == Food->x && pos->y == Food->y) 
  { 
    return 1; 
  } 
  return 0; 
} 
 
voID SnakeMove()  //蛇移动一次 
{ 
  pSnake newhead = NulL; 
  newhead = (pSnakE)malloc(sizeof(SnakE)); 
 
  if(Direction == R) 
  { 
    newhead->x = head->x; 
    newhead->y = head->y+1; 
    newhead->next = head; 
  } 
  else if(Direction == L) 
  { 
    newhead->x = head->x; 
    newhead->y = head->y-1; 
    newhead->next = head; 
  } 
  else if(Direction == U) 
  { 
    newhead->x = head->x-1; 
    newhead->y = head->y; 
    newhead->next = head; 
   
  } 
  else if(Direction == D) 
  { 
    newhead->x = head->x+1; 
    newhead->y = head->y; 
    newhead->next = head; 
  } 
 
  if(IsFood(newhead)) 
  { 
    head = newhead; 
    PrintSnake(); 
    CreateFood(); 
    score += everyscore; 
    Pos(Col_MAP+20,succesS_score); 
    if(score >= succesS_score) 
    { 
      State = succesS; 
    } 
  } 
  else  
    { 
      if(IsCrossWall() && IsEatSelf(newhead)) 
      { 
        pSnake cur = NulL; 
        head = newhead; 
        cur = head; 
        //删除蛇尾并打印 
        while(cur->next->next != NulL) 
        { 
          Pos(cur->y,cur->X); 
          printf("%c",snakE); 
          cur = cur->next; 
        } 
         
        Pos(cur->y,cur->X); 
        printf("%c",snakE); 
        Pos(cur->next->y,cur->next->X); 
        printf(" "); //打印空格来覆盖频幕上的蛇尾 
        free(cur->next); 
        cur->next = NulL; 
      } 
      else 
      { 
        free(newhead); 
        newhead = NulL; 
      } 
  } 
} 
 
voID Pause() 
{ 
  while(1) 
  { 
    Sleep(sleeptimE); 
    if(GetAsyncKeyState(VK_SPACE)) 
    { 
      break; 
    } 
  } 
} 
 
voID ControlSnake() //用键盘控制游戏 
{ 
  if(GetAsyncKeyState(VK_Up) && Direction!=D) 
  { 
    Direction = U; 
  } 
  else if(GetAsyncKeyState(VK_DOWN) && Direction!=U) 
  { 
    Direction = D; 
  } 
  else if(GetAsyncKeyState(VK_left) && Direction!=R) 
  { 
    Direction = L; 
  } 
  else if(GetAsyncKeyState(VK_RIGHT) && Direction!=L) 
  { 
    Direction = R; 
  } 
  else if(GetAsyncKeyState(VK_F1)) 
  { 
    if(sleeptime != 500) 
    { 
      sleeptime = 500; 
      everyscore = 1; 
      Pos(Col_MAP+20,2); 
      printf("%d",everyscore); 
    } 
  } 
  else if(GetAsyncKeyState(VK_F2)) 
  { 
    if(sleeptime != 300) 
    { 
      sleeptime = 300; 
      everyscore = 2; 
      Pos(Col_MAP+20,everyscore); 
    } 
  } 
  else if(GetAsyncKeyState(VK_SPACE)) 
  { 
    Pause(); 
  } 
  else if(GetAsyncKeyState(VK_ESCAPE)) 
  { 
    exit(0); 
  } 
} 
 
voID StateGame() //判断游戏失败或成功 
{ 
  if(State == ERROR_SELF) 
  { 
    system("cls"); 
    printf("很遗憾,蛇咬到自己,游戏失败!\n"); 
  } 
  else if(State == ERROR_WALL) 
  { 
    system("cls"); 
    printf("很遗憾,蛇碰到墙壁,游戏失败!\n"); 
  } 
  else if(State == succesS) 
  { 
    system("cls"); 
    printf("恭喜您,已通关!!!\n"); 
  } 
   
} 
voID RunGame() 
{ 
  Direction = R; //蛇初始行走方向为右 
  State = norMAL;//游戏初始为正常状态 
  while(1) 
  { 
    ControlSnake(); 
    SnakeMove(); 
    if(State != norMAL) 
    { 
      StateGame(); 
      break; 
    } 
    Sleep(sleeptimE); 
  } 
} 
 
voID EndGame() //释放链表并恢复默认值 
{ 
  pSnake cur = head; 
  while(cur) 
  { 
    pSnake del = cur; 
    cur = cur->next; 
    free(del); 
    del = NulL; 
  } 
  head = NulL; 
  if(Food != NulL) 
  { 
    free(Food); 
    Food = NulL; 
  } 
  score = 0; 
  everyscore = 1; 
  sleeptime = 500; 
} 

3.Test.c

#include "Snake.h" 
 
int main() 
{ 
  while(1) 
  { 
    StartGame(); 
    RunGame(); 
    EndGame(); 
  } 
  system("pause"); 
  return 0; 
} 

五、运行界面展示

1.欢迎界面

 

C语言贪吃蛇经典小游戏


2.游戏界面

C语言贪吃蛇经典小游戏


小小的c语言项目,用来练手,仅供参哦!!
谢谢阅读,如有问题,欢迎提出。

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

您可能感兴趣的文章:@H_301_85@
  • 贪吃蛇C语言代码实现(难度可选)
  • C语言手把手教你实现贪吃蛇AI(上)
  • C语言链表实现贪吃蛇游戏
  • C语言结构数组实现贪吃蛇小游戏
  • 基于C语言实现的贪吃蛇游戏完整实例代码
  • C语言手把手教你实现贪吃蛇AI(中)

大佬总结

以上是大佬教程为你收集整理的C语言贪吃蛇经典小游戏全部内容,希望文章能够帮你解决C语言贪吃蛇经典小游戏所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。