首页 > 其他分享 >贪吃蛇(C语言版)链表实现

贪吃蛇(C语言版)链表实现

时间:2022-08-25 23:36:28浏览次数:89  
标签:tmp __ temp int next 链表 贪吃蛇 printf C语言

贪吃蛇

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

标签:tmp,__,temp,int,next,链表,贪吃蛇,printf,C语言
From: https://www.cnblogs.com/superFw/p/16626175.html

相关文章

  • 数据结构与算法分析--C语言描述 pdf
    高清扫描版下载链接:https://pan.baidu.com/s/1BGsOOAOqXE9j509OFtkjXA点击这里获取提取码书中详细介绍了当前流行的论题和新的变化,讨论了算法设计技巧,并在研究算法的性能......
  • leetcode148:排序链表
    packagecom.mxnet;publicclassSolution148{publicstaticvoidmain(String[]args){}/***给你链表的头结点head,请将其按升序排列并......
  • P3201 [HNOI2009] 梦幻布丁 将颜色x变成颜色y 问总共有多少种颜色 启发式合并+链表
    https://www.luogu.com.cn/problem/P3201题目描述nn 个布丁摆成一行,进行 mm 次操作。每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色。......
  • 什么是双向链表?双向链表的操作封装实现(增删改查)?
    什么是双向链表?双向链表既可以从头遍历到尾,又可以从尾遍历到头也就是链表相连的过程是双向的.那么它的实现原理,你能猜到吗?一个节点既有向前连接的引用,也......
  • 合并两条有序链表
     用JavaScript实现:constlink1=newLinkedList()link1.append(1)link1.append(3)link1.append(4)link1.append(6)li......
  • 双链表和循环链表
    一、结构体定义1.双链表typedefstructDLNode{ intdata; structDLNode*prior,*next;}DLNode;2.循环链表//同双链表二、操作1.尾插法建立双链表voidcreat......
  • C语言飞机订票系统
    C语言飞机订票系统C语言课程设计任务书17题目:飞机订票系统设计功能:本飞机共有80个坐位,分20排,每排4个位子。编号为A,B,C,D。如10D表示10排D座。A和D靠窗,19到20排为吸烟......
  • LeetCode 重排链表算法题解 All In One
    LeetCode重排链表算法题解AllInOnejs/ts实现重排链表重排链表原理图解//快慢指针重排链表https://leetcode.com/problems/reorder-list/https://le......
  • LeetCode 21 合并两个有序链表
    /***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode():val(0),next(nullptr){}*ListN......
  • LeetCode 142. 环形链表 II
    思路:快慢指针法:当快指针与慢指针相遇时,分别从起点,相遇点开始走,相遇即为环入口/***Definitionforsingly-linkedlist.*structListNode{*intval;*......