大一c语言刚入门求指导贪吃蛇编程

怎么样判断贪吃蛇的死亡游戏失败 应该使用什么方法进行编写 我已经找到了一个源代码 需要讲解一下原理 用到哪些知识点

这个有详细的注释说明


#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
/*大一上的时候C语言入门学的一个小游戏。还是挺有意思的,有兴趣的同学可以继续优化下:比如蛇头碰到蛇身就判定为输 /给蛇身加点颜色等。
*/
//1.2食物结构体
#define MAPHEIGHT 25
#define MAPWIDTH 60
#define SNAKESIZE 50  //蛇的最大节数
struct FOOD
{
    int x;
    int y;
}food;

#define SNAKESIZE 50  //蛇的最大节数
struct
{ //每一节蛇的坐标
    int x[SNAKESIZE];
    int y[SNAKESIZE];
    int len; //蛇长
    int speed; //
}snake;
///全局变量
int key='w';//初始化移动方向
int changFlag = 0;//蛇变化标记
//模块化设计
//
//1.画地图
void gotoxy(int x,int y); //实现的函数声明
void drawMap();
void createFood();
int snakeStatus();
void drawMap()

{
    srand((unsigned)time(NULL)); //随机函数种子
    //1.圈地
    //1.1 左右边框
    for(int i=0;i<=MAPHEIGHT;i++)
    {
        gotoxy(0,i);
        printf("■");
        gotoxy(MAPWIDTH,i);
        printf("■");
    }
    for(int i=0;i<=MAPWIDTH;i+=2) //上下  ■占用两个字符
    {
        gotoxy(i,0);
        printf("■");
        gotoxy(i,MAPHEIGHT);
        printf("■");
    }
    //2.画蛇  ■
    //2.1确定蛇的属性
    snake.len=3;
    snake.speed =100;
    snake.x[0]=MAPWIDTH/2;  //开始蛇头放屏幕中间
    snake.y[0]=MAPHEIGHT/2;
    //2.2画蛇头
    gotoxy(snake.x[0],snake.y[0]);
    printf("■"); //一节 x=2
    //画身体
   for(int k =1;k<snake.len;k++)
    {
        snake.x[k]=snake.x[k-1]+2;
        snake.y[k]=snake.y[k-1];
        gotoxy(snake.x[k],snake.y[k]);
        printf("■");
    }
    //3.画食物  ● //3.1确定坐标
    food.x=rand()%(MAPWIDTH-4)+2;  //边框的宽度是2 占用两个字符 两边边框就是4
    food.y=rand()%(MAPHEIGHT-2)+1;  //上下各占一个
      if(food.x%2!=0)
         {
          food.x=food.x+1;
         }
     //3.2 画出来就可以
     gotoxy(food.x,food.y);
     printf("●");



}
//2.food
void createFood()
{
 //蛇头坐标等于食物坐标,就是吃了
 if(snake.x[0]==food.x && snake.y[0]==food.y)
 {
     srand((unsigned int)time(NULL));
     //产生的食物不能在蛇的身上,并且坐标要是偶数 因为蛇头的宽度是偶数
     while(1)
     {
         int flag=1;
         food.x=rand()%(MAPWIDTH-4)+2;
         food.y=rand()%(MAPHEIGHT-2)+1;
         //产生的食物不能在蛇身上
         for(int k=0;k<snake.len;k++)
         {
             if(snake.x[k]==food.x&&snake.y[k]==food.y)
             {
                 flag = 0; //不合适的标志
                 break;
             }
         }
         if(flag&&food.x%2==0)
         {

             break;
         }
     }
       gotoxy(food.x,food.y);//产生新的食物
  printf("●");
      snake.len++;
   changFlag =1;//蛇的标记是1
 }
//gotoxy(food.x,food.y);//产生新的食物
  // printf("●");

}
void keyDown() //3.按键操作
{
//无按键的处理 原方向
if(_kbhit())
{//有按键
    fflush(stdin);
    key=_getch();
}
//擦除
if(!changFlag)
{
    gotoxy(snake.x[snake.len-1],snake.y[snake.len-1]);
    printf("  ");//两个空格擦掉尾巴。
}
//后面的蛇身
for(int i =snake.len-1;i>0;i--)
{
    snake.x[i]=snake.x[i-1];
    snake.y[i]=snake.y[i-1];

}
//移动方向的处理
switch(key)
{
case 'W': //往上走 y--
case 'w':
       snake.y[0]--;
       break;
case 'S':
case 's':
       snake.y[0]++;
       break;
case 'A':
case 'a':
       snake.x[0]-=2;
       break;
case 'd':
case 'D':
       snake.x[0]+=2;
       break;
    }
    //画蛇头
    gotoxy(snake.x[0],snake.y[0]);
    printf("■");
    changFlag=0;
    gotoxy(MAPHEIGHT+2,0); //移动不能一直看着光标
}
int snakeStatus()  //4.蛇的状态:判断是否结束游戏
{
if (snake.x[0]==0||snake.x[0]==MAPWIDTH||snake.y[0]==0||snake.y[0]==MAPHEIGHT)
  {
      return 0;
      //蛇头不能撞自己
      for(int k=1;k<snake.len;k++)
      {
          if(snake.x[0]== snake.x[k]&& snake.y[k]==snake.y[0])
            return 0;
      }

  }
return 1;
}
void gotoxy(int x,int y) //5.光标移动
{
    //调用win32 API去设置控制台的光标位置
     //1.找到控制台的这个窗口
     HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
     //2.光标的结构体
     COORD coord;
     //3.设置光标
     coord.X =x;
     coord.Y =y;
     //4.同步到控制台  Set Console Cursor Position
     SetConsoleCursorPosition(handle,coord);
}

int main(){

 drawMap();
 while(1)
 {
     createFood();
     Sleep(snake.speed);//延时
   keyDown();
   if(!snakeStatus())
   {
       break;
   }
   }
   gotoxy(MAPWIDTH/2,MAPHEIGHT/2);
   printf("You lose!");

   system("pause"); //

  return 0;
}

就是判断贪吃蛇的头部与身体重叠或者头部移出地图边界就是死亡
参考

/*
@Author: Joke-Lin
@Time: 2020-03-19
@Encoding: GB 2312
@IDE: VS2019
*/
 
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
 
#define up 'w'
#define down 's'
#define left 'a'
#define right 'd'
 
void welcome();               // 开始界面
void Finish();                // 结束界面
void creatgraph();            // 围墙打印
void gotoxy(int x, int y);    // 光标跳转,横为X 0,1,2..
void gotoprint(int x, int y); // 跳转打印
void gotodelete(int x, int y);// 跳转删除
void creatfood();             // 食物产生
int ClickControl();           // 获取键盘信号
int Judge();                  // 游戏结束判断
void MovingBody();            // 蛇的移动 
void Eating();                // 蛇吃到东西后的操作(伸长)
void ChangeBody(int x, int y); // 蛇的坐标变换
 
/*全局变量 + 预处理:*/
typedef struct Snakes
{
    int x;
    int y;
    struct Snakes* next;
}snake;
 
snake* head;    // 声明蛇头指针 
 
// 申明并定义食物 
struct Food
{
    int x;
    int y;
}food;
 
char name[20];  // 保存用户名 有兴趣可以制作登录系统 
int score = 0;  // 分数 
char click = 1; // 记录敲下的键盘按键 
int speed;      // 速度 其实是延迟的毫秒数 
 
/************************************************************/
 
int main()
{
    system("color 0B"); // 设置控制台字体颜色 
    welcome();          // 欢迎界面 
    creatgraph();       // 创建地图 
    creatfood();        // 新建食物 
    // 捕获鼠标按键 ClickControl
    if (ClickControl() == 0) return 0;
    return 0;
}
 
/**********************************************************/
void welcome()
{
    gotoxy(15, 10);
    printf("/**********************************************/");
    gotoxy(15, 20);
    printf("/**********************************************/");
    gotoxy(20, 13);
    printf("WELCOME TO THE GAME OF RETRO SNAKE");
    gotoxy(14, 16);
    printf("请在英文输入法中操作,反向移动等同于吃到自己,wasd控制p暂停");
    gotoxy(20, 18);
    printf("PLEASE ENTER YOUR NAME:");
    scanf_s("%s", &name, 20);
    system("cls");
}
 
/**********************************************************/
void creatgraph() {
    int i;
    /*
    注意这里横坐标是每次+2 因为控制台字符宽高比为1:2
    */
    for (i = 0; i < 58; i += 2)
    {
        gotoprint(i, 0);
        gotoprint(i, 26);
    }
    for (i = 1; i < 26; i++)
    {
        gotoprint(0, i);
        gotoprint(56, i);
    }
    gotoxy(63, 10);
    printf("hello %s,Welcome To Play", name);
    gotoxy(63, 15);
    printf("Your Score Is:%d    = ̄ω ̄= ", score);
    gotoxy(63, 20);
    printf("This Game Is Created By JOKER");
    head = (snake*)malloc(sizeof(snake));
    snake* p = (snake*)malloc(sizeof(snake));
    snake* q = (snake*)malloc(sizeof(snake));
    head->x = 16;
    head->y = 15;
    p->x = 16;
    p->y = 16;
    q->x = 16;
    q->y = 17;
    head->next = p;
    p->next = q;
    q->next = NULL;
}
/**********************************************************/
void gotoxy(int x, int y)
{
    // 更新光标位置 
    COORD pos;
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(hOutput, pos);
    // 隐藏光标 
    CONSOLE_CURSOR_INFO cursor;
    cursor.bVisible = FALSE;
    cursor.dwSize = sizeof(cursor);
    SetConsoleCursorInfo(hOutput, &cursor);
}
 
/**********************************************************/
void gotoprint(int x, int y)
{
    gotoxy(x, y);
    printf("■");
}
 
/**********************************************************/
void gotodelete(int x, int y)
{
    gotoxy(x, y);
    printf("  ");
}
 
/**********************************************************/
void creatfood()
{
    // 随机产生一个食物 
    bool flag = false;
    while (!flag)
    {
        flag = true;
        srand((int)time(NULL));
        food.y = rand() % (25 - 1 + 1) + 1;
        food.x = rand() % (54 - 2 + 1) + 2;
        if (food.x % 2 != 0)
        {
            food.x = food.x + 1;
        }
        snake* judge = head;
        while (1)  //遍历排除蛇身重复
        {
            if (judge->next == NULL) break;
            if (food.x == judge->x && food.y == judge->y)
            {
                flag = false;
            }
            judge = judge->next;
        }
    }
    gotoxy(food.x, food.y);
    printf("⊙");
}
 
/**********************************************************/
// 捕获鼠标 游戏主循环 
int ClickControl()
{
    char c;
    while (1)
    {
        if (Judge() == 0) return 0;
        if (_kbhit())
        {
            click = _getch();
        }
        MovingBody();
        Eating();
    }
    return 1;
}
 
/**********************************************************/
void MovingBody() {
    int x = head->x, y = head->y;
    snake* p = head;
    // 通过先清空后打印实现动画效果
    while (p->next != NULL) {
        p = p->next;
    }
    gotodelete(p->x, p->y); // 消除尾节点
    switch (click)
    {
    case up:
        y -= 1;
        break;
    case down:
        y += 1;
        break;
    case left:
        x -= 2;
        break;
    case right:
        x += 2;
        break;
    default:
        break;
    }
    if (x != head->x || y != head->y) {
        // 改变坐标时更新 暂停游戏停止更新蛇 
        ChangeBody(x, y);
    }
    p = head;
    // 打印蛇头
    gotoprint(p->x, p->y);
    // 蛇速度控制 
    int count = score / 10;
    if (count <= 10) speed = 150;
    else if (count > 10 && count <= 20) speed = 100;
    else if (count > 20 && count <= 40) speed = 50;
    else speed = 10;
    Sleep(speed);
}
 
/**********************************************************/
// 吃到食物处理 添加一个尾巴 
void Eating()
{
    if (head->x == food.x && head->y == food.y)
    {
        creatfood();
        snake* _new = (snake*)malloc(sizeof(snake));
        snake* p;
        p = head;
        while (1)
        {
            if (p->next == NULL) break;
            p = p->next;
        }
        p->next = _new;
        _new->next = NULL;
        score += 10;
        gotoxy(77, 15);
        printf("%d", score);
    }
}
 
/**********************************************************/
// 更新蛇体坐标 只需要消除尾结点 然后把新坐标结点置为头结点即可 
void ChangeBody(int x, int y)
{
    snake* p = head;
    while (p->next->next != NULL) {
        p = p->next;
    }
    free(p->next);
    p->next = NULL;
    snake* new_head = (snake*)malloc(sizeof(snake));
    new_head->x = x;
    new_head->y = y;
    new_head->next = head;
    head = new_head;
}
 
/**********************************************************/
// 判断是否游戏结束 
int Judge()
{
    if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26)
    {
        Finish();
        return 0;
    }
    snake* p = head->next;
    while (1)
    {
        if (p == NULL) break;
        if (head->x == p->x && head->y == p->y)
        {
            Finish();
            return 0;
        }
        p = p->next;
    }
    return 1;
}
 
/**********************************************************/
void Finish()
{
    system("cls");
    gotoxy(15, 10);
    printf("/**********************************************/");
    gotoxy(15, 20);
    printf("/**********************************************/");
    gotoxy(18, 14);
    printf("GAME   OVER      o(* ̄▽ ̄*)o");
    gotoxy(20, 16);
    printf("Your Score is %d    hiahiahia", score);
    gotoxy(18, 18);
    printf("还不错哦,     继续努力O(∩_∩)O");
    gotoxy(0, 27);
    // 释放空间 
    snake* p = head, * q;
    while (p != NULL) {
        q = p->next;
        free(p);
        p = q;
    }
    system("pause");
}


如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

请问你做贪吃蛇游戏是要用ege图形库,还是其他图形库呢?