贪吃蛇

gitee:贪吃蛇C语言版: Snake

蛇的结构

typedef struct Snake
{
int x;
int y;
struct Snake *next;
};

游戏开始欢迎界面

//游戏开始欢迎界面
void meun()
{
printf(" \n");
printf(" __________ ___ \n");
printf(" / \\ / \\ \\ |____ __\\__ \n");
printf(" / ________ \\ / ___ \\ _/ __ | | / \n");
printf(" | | |__| _/_ |_| / [|] |/ \n");
printf(" | | | | | / _|_ \\__/ \n");
printf(" \\ \\_______ / \\ |___/ ____ \n");
printf(" \\ \\ ____ ____ ____ __ | | ___ ______ \n");
printf(" \\_______ \\ | |/ \\ / \\_/ / | | / / / \\ \n");
printf(" \\ \\ | ___ \\ / ____ / | |/ / / ____ \\ \n");
printf(" __ | | | / \\ \\ | | | / | / | /____\\ | \n");
printf(" \\ \\_______| | | | | | | |__| | | \\ | ________/ \n");
printf(" \\ / | | | | \\ \\ | |\\ \\ \\ \\____ \n");
printf(" \\__________/ |__| |__| \\___/\\__\\ |__| \\__\\ \\______/ \n");
printf("按回车键开始游戏");
}

初始化蛇身

//初始化蛇身
void init()
{
pSnake tmp = (pSnake)malloc(sizeof(Snake *));
tmp->next = NULL;
head = tmp;
tmp->x = 30;
tmp->y = 10;
for (int i = 1; i < size; i++)
{
pSnake temp = (pSnake)malloc(sizeof(Snake *));
temp->next = NULL;
tmp->next = temp;
temp->x = tmp->x + 2;
temp->y = tmp->y;
tmp = tmp->next;
}
}

游戏开始欢迎界面

//游戏开始欢迎界面
void meun()
{
printf(" \n");
printf(" __________ ___ \n");
printf(" / \\ / \\ \\ |____ __\\__ \n");
printf(" / ________ \\ / ___ \\ _/ __ | | / \n");
printf(" | | |__| _/_ |_| / [|] |/ \n");
printf(" | | | | | / _|_ \\__/ \n");
printf(" \\ \\_______ / \\ |___/ ____ \n");
printf(" \\ \\ ____ ____ ____ __ | | ___ ______ \n");
printf(" \\_______ \\ | |/ \\ / \\_/ / | | / / / \\ \n");
printf(" \\ \\ | ___ \\ / ____ / | |/ / / ____ \\ \n");
printf(" __ | | | / \\ \\ | | | / | / | /____\\ | \n");
printf(" \\ \\_______| | | | | | | |__| | | \\ | ________/ \n");
printf(" \\ / | | | | \\ \\ | |\\ \\ \\ \\____ \n");
printf(" \\__________/ |__| |__| \\___/\\__\\ |__| \\__\\ \\______/ \n");
printf("按回车键开始游戏");
}

画墙

//画墙
void printWall()
{
for (int i = 0; i <= 80; i += 2)
{
pos(i, 0);
printf("■");
pos(i, 26);
printf("■");
} for (int i = 0; i <= 26; i++)
{
pos(0, i);
printf("■");
pos(80, i);
printf("■");
} }

初始化蛇身

//初始化蛇身
void init()
{
pSnake tmp = (pSnake)malloc(sizeof(Snake *));
tmp->next = NULL;
head = tmp;
tmp->x = 30;
tmp->y = 10;
for (int i = 1; i < size; i++)
{
pSnake temp = (pSnake)malloc(sizeof(Snake *));
temp->next = NULL;
tmp->next = temp;
temp->x = tmp->x + 2;
temp->y = tmp->y;
tmp = tmp->next;
}
}

随机函数

//随机函数
void random(int *x, int *y)
{
srand((unsigned)time(NULL));
// 2~78 1~39
int a = rand() % 39 + 1;
int b = rand() % 25 + 1;
*x = a;
*y = b;
}

坐标函数

//坐标函数
void pos(int x, int y)
{
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}

打印蛇身

//打印蛇身
void print()
{
pSnake tmp = head;
while (tmp != NULL)
{
pos(tmp->x, tmp->y);
printf("■");
tmp = tmp->next;
}
}

蛇动

void move()
{
if (flag == 77 || flag == 72 || flag == 80 || flag == 75)
{ }
else
{
return;
}
//增加头
pSnake temp = (pSnake)malloc(sizeof(pSnake));
temp->next = head;
head = temp; if (flag == 77) //右
{
temp->x = temp->next->x + 2;
temp->y = temp->next->y;
}
else if (flag == 72) //上
{
temp->x = temp->next->x;
temp->y = temp->next->y - 1;
}
else if (flag == 80)
{
temp->x = temp->next->x;
temp->y = temp->next->y + 1;
}
else if (flag == 75)
{
temp->x = temp->next->x - 2;
temp->y = temp->next->y;
} //删尾巴
pSnake tmp = head;
while (tmp->next->next != NULL)
{
tmp = tmp->next;
}
pos(tmp->next->x, tmp->next->y);
free(tmp->next);
printf(" "); tmp->next = NULL; print();
}

清除光标

//隐藏光标
void HideCursor()
{
CONSOLE_CURSOR_INFO curInfo; //定义光标信息的结构体变量
curInfo.dwSize = 1; //如果没赋值的话,光标隐藏无效
curInfo.bVisible = FALSE; //将光标设置为不可见
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
SetConsoleCursorInfo(handle, &curInfo); //设置光标信息
}

产生食物

void Cfood()
{
int x, y;
random(&x, &y);
if ((x >= 2 && x <= 78) && (y <= 25 && y >= 1)) //在墙里面
{
if (isIn(x, y) == 0) //不在蛇内
{
fx = x;
fy = y;
pos(x, y);
printf("★");
return;
}
}
Cfood();
}

判断是否在蛇身上

int isIn(int x, int y)
{
pSnake tmp = head;
while (tmp != NULL)
{
if (tmp->x == x && tmp->y == y)
{
return 1;
}
tmp = tmp->next;
}
return 0;
}

随机函数

//随机函数
void random(int *x, int *y)
{
srand((unsigned)time(NULL));
// 2~78 1~39
int a = rand() % 39 + 1;
int b = rand() % 25 + 1;
*x = a * 2;
*y = b;
}

判断食物是否被吃

int isFood()
{
pSnake tmp = head;
if (tmp->x == fx && tmp->y == fy)
{
Cfood();
add();
return 1;
}
return 0;
}

蛇的增长

void addSnake()
{
pSnake tmp = head;
while (tmp->next != NULL)
{
tmp = tmp->next;
}
pSnake temp = (pSnake)malloc(sizeof(Snake));
tmp->next = temp;
temp->next = NULL;
}

蛇的死亡

int isDe()
{
pSnake tmp = head;
if (tmp->x == 0 || tmp->x == 80 || tmp->y == 0 || tmp->y == 26)
{
return 1;
}
tmp = tmp->next;
while (tmp != NULL)
{
if (head->x == tmp->x && head->y == tmp->y)
{
return 1;
}
tmp = tmp->next;
}
return 0;
}

增加积分

//增加积分
void add()
{
score += 5;
pos(90, 15);
printf("您现在的积分是:%d", score);
}

颜色

int color(int num)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), num);
return 0; }

已经死了

void death()
{
system("cls");
pos(40, 15);
printf("您获得的分数是:%d", score);
pos(40, 16);
if (score <= 20)
{
printf("你已经死亡,真是个垃圾");
}
else if (score <= 30)
{
printf("呦呵小伙子有点东西");
}
else if (score <= 40)
{
printf("传说中的训蛇大师");
}
else if (score > 40)
{
printf("你就是神!!!!");
}
else if (score > 100)
{
printf("超越神啦!!!!!!!!!!");
}
getchar();
}

贪吃蛇(C语言版)链表实现的更多相关文章

  1. 贪吃蛇(简易版)Leslie5205912著

    # include <stdio.h># include <string.h># include <windows.h># include <stdlib.h ...

  2. 如何用python制作贪吃蛇以及AI版贪吃蛇

    用python制作普通贪吃蛇 哈喽,大家不知道是上午好还是中午好还是下午好还是晚上好! 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很 ...

  3. 人生就像一条加速奔向死亡的贪吃蛇【winform版】

    群里聊天的时候,一个学妹说她在做贪吃蛇的小作业,于是昨晚(5.20无聊只好撸代码/(ㄒoㄒ)/~~)花了2个小时撸了一个出来,早上又花了些时间完善功能,就有了这个还算比较完善的版本,当然代码结构比较混 ...

  4. 用最少的JS代码写出贪吃蛇游戏---迷你版

    游戏进行页面展示 GAME  OVER 页面展示  代码如下: <!doctype html> <html>   <body>   <canvas id=&q ...

  5. C语言 贪吃蛇

    贪吃蛇(单人版): 本人先来介绍一个函数 -- bioskey函数: int bioskey (int cmd) 参数 (cmd) 基本功能 0 返回下一个从键盘键入的值(若不键入任何值,则将等下一个 ...

  6. [C语言]链表实现贪吃蛇及部分模块优化

    在继上篇[C语言]贪吃蛇_结构数组实现大半年后,链表实现的版本也终于出炉了.两篇隔了这么久除了是懒癌晚期的原因外,对整个游戏流程的改进,模块的精简也花了一些时间(都是借口). 优化模块的前沿链接: · ...

  7. 小项目特供 贪吃蛇游戏(基于C语言)

    C语言写贪吃蛇本来是打算去年暑假写的,结果因为ACM集训给耽搁了,因此借寒假的两天功夫写了这个贪吃蛇小项目,顺带把C语言重温了一次. 是发表博客的前一天开始写的,一共写了三个版本,第一天写了第一版,第 ...

  8. OC版贪吃蛇

    昨天写了一个js版贪吃蛇,今天突然想写一个OC版的,来对比一下两种语言的区别 oc版功能,适配所有尺寸iphone,可暂停,可设置地图和蛇的比例,可加速 对比一下会发现js版的相对OC版的会简单一些, ...

  9. c语言贪吃蛇详解3.让蛇动起来

    c语言贪吃蛇详解3.让蛇动起来 前几天的实验室培训课后作业我布置了贪吃蛇,今天有时间就来写一下题解.我将分几步来教大家写一个贪吃蛇小游戏.由于大家c语言未学完,这个教程只涉及数组和函数等知识点. 上次 ...

随机推荐

  1. WinForms获得已打开窗体的引用

    更新记录 本文迁移自Panda666原博客,原发布时间:2021年7月6日. 对于已经打开的窗口,可以通过Application.OpenForms属性进行获得.该属性是一个FormCollectio ...

  2. MAUI模板项目闪退问题

    MAUI模板项目闪退问题 在MAUI最初发布的时候就曾创建过几个模板项目进行体验过,没遇到什么坑.由于最近需要开发针对餐饮行业的收银机(安卓系统)开发一款应用,这种收银机一般配置不咋滴,系统版本和性能 ...

  3. DBPack 赋能 python 微服务协调分布式事务

    作者:朱晗 中国电子云 什么是分布式事务 事务处理几乎在每一个信息系统中都会涉及,它存在的意义是为了保证系统数据符合期望的,且相互关联的数据之间不会产生矛盾,即数据状态的一致性. 按照数据库的经典理论 ...

  4. 原生实现.NET5.0+ 自定义日志

    一.定义一个静态类 声明一个 ReaderWriterLockSlim 对象 用于并发控制 1 /// <summary> 2 /// IO锁 3 /// </summary> ...

  5. IDEA快速创建maven项目

    遇到问题不要急,不要怕. 一.  二. 三.  四.Finish进来之后,项目会加载一会,之后会是下面这样子.  五.继续往下面配置,建立java和resorces文件夹  六.下面配置tomcat服 ...

  6. JS中通过id或者class获取文本内容

    一.JS通过id获取文本内容 二.JS通过class获取文本内容

  7. 扩展新的WCV到标准的WC后,不能在业务角色里面看见视图解决方法

    by zyi 感谢群主红枣的分享 1.把你的WCVIEW扩展进WC中 2.使用UI Designer打开你的WCVIEW 3.更改你的WCVIEW名字

  8. 还在因为部署 Kubernetes 时,无法拉取 k8s.gcr.io/*** 镜像而头疼吗

    拉取外网 Kubernetes 镜像 还在因为部署 Kubernetes 时,无法拉取 k8s.gcr.io/*** 镜像而头疼吗? 传送门 https://github.com/liamhao/pu ...

  9. 使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星

    最新博客链接 最近想学习一下 CE,刚好看见游戏库里装了 Kingdom Rush 就拿它来研究吧.这里写的东西,需要一些 Cheat Engine 的基础,可以看看教程. 这里主要是看写的注释,来理 ...

  10. 强化学习-学习笔记4 | Actor-Critic

    Actor-Critic 是价值学习和策略学习的结合.Actor 是策略网络,用来控制agent运动,可以看做是运动员.Critic 是价值网络,用来给动作打分,像是裁判. 4. Actor-Crit ...