贪吃蛇

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. .NET中的迭代器(Iterator)

    更新记录 本文迁移自Panda666原博客,原发布时间:2021年6月30日. 一.迭代器介绍 C#2.0开始,我们可以使用迭代器(iterator).编译器自动把我们定义的迭代器生成 可枚举类型 或 ...

  2. VMware 虚拟机安装CentOS镜像详细步骤

    CentOS目前官网提供的下载版本有6.7.8,最新的版本为8,不过个人推荐CentOS 7 的版本,因为相比较于最新版本,版本7更加地稳定.而相比于版本6,版本7新增了很多的功能.CentOS 7 ...

  3. 自己实现一个自定义React项目脚手架「ReactCli」

    前言 首先为什么想到自己实现一个React脚手架呢?是因为之前刚接触create-react-app时,觉得不太灵活.虽然文件目录很清晰,但是还是觉得不如VueCLI的可以自定义配置更加灵活.当然Re ...

  4. 《ECMAScript 6 入门》【二、变量的解构赋值】(持续更新中……)

    前言: 让我们看下es6的新语法解构,跟模式匹配类似.一.数组的解构赋值 举个例子给多个变量赋值的写法: var a =1;var b =2;var c =3; 需要写多个变量特别麻烦,我们先使用以前 ...

  5. SAP SD-Invoice 销售发票

    针对销售订单的发票流程: 1. 事务码:VF01(个别生成系统发票) 创建开票凭证(发票)/  VF04 开具系统发票(可把多个item 合并成一张系统发票) 2. 事务码:VF02 修改发票, 释放 ...

  6. Linux安装MySQL,数据库工具连接Linux的MySQL

    1.centOS中默认安装了MariaDB,需要先进行卸载 rpm -qa | grep -i mariadb rpm -e --nodeps 上面查出来的mariadb 2.下载MySQL仓库并安装 ...

  7. Elasticsearch 在地理信息空间索引的探索和演进

    vivo 互联网服务器团队- Shuai Guangying 本文梳理了Elasticsearch对于数值索引实现方案的升级和优化思考,从2015年至今数值索引的方案经历了多个版本的迭代,实现思路从最 ...

  8. zabbix监控mysql主从同步

    获取主从复制sql线程和I/O线程状态是否为yes #!/bin/bash HOSTNAME="数据库IP" PORT="端口" USERNAME=" ...

  9. UiPath条件判断活动Flow Decision的介绍与使用

    一.Flow Decision介绍 FlowDecision节点是一个条件节点,它根据指定条件是否成立来控制流程的两个分支. 当条件为True时,流程执行一个分支 当条件为False时,流程执行另外一 ...

  10. ERROR: manifest for elasticsearch:latest not found: manifest unknown: manife

    当我们用docker下载 elasticsearch 的时候出现如下错误: 这里错误的原因是没有发现最新版,需要我们指定版本. docker pull elasticsearch:7.12.0 那我们 ...