[Small Game]Gluttonous Snake-V2.0

本文详细介绍贪吃蛇游戏V2.0版本的更新内容,包括新增功能如场景选择、随机道具、积分等级系统、速度调节、帮助界面及功能键,以及修复的大量bug。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        今天上午又改了V2.0版本。

        加了好多功能:

        1、场景选择:有障碍的场景和无障碍的场景。

        2、随机道具:增加了随机道具。

        3、积分等级:增加了积分制度和等级系统。

        4、速度调节:可以调节速度。

        5、帮助界面:增设了帮助界面。

        6、功能键:增设了暂停、退出、选择场景等功能键。

        7、改正了V1.0版本中的一大堆bug。。。

        直接上码:

#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
/*=========================================================================================================*/
#define LEN 30
#define WID 24
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define WAIT_TIME 30000
/*=========================================================================================================*/
int snake_direction = LEFT;
int snake_length = 3;
int Score = 0;
int Lv = 1;
int Scene = 1;
int num = 0;
int Speed = 100;
int Now_time;
int flag1 = 1;

bool step_c = false;
bool alive = true;
bool eatup = false;
bool ispause = false;
bool ismagic = false;
/*=========================================================================================================*/
//光标相关
	void Set_Cursor_To(int x, int y);
//蛇相关
	void direction_snake();
	void flag_snake();
	void draw_snake();
	void eraser_snake();
//食物相关
	void product_food();
	void product_barriers(int i);
	void eraser_barriers();
	void draw_barriers();
	void draw_magic();
//环境相关
	void draw_boundary();
	void draw_info();
//控制相关
	void Game_Over();
	void initialize();
	void Lv_system();
	void Help();
	void Chose_Scene();
/*=========================================================================================================*/
//光标相关 
	HANDLE handle_console = GetStdHandle(STD_OUTPUT_HANDLE);
	//设置光标位置
	void Set_Cursor_To(int x, int y)
	{
		COORD position = { x, y };
		SetConsoleCursorPosition(handle_console, position);
	}
/*=========================================================================================================*/
//蛇相关
	int snake[LEN][WID] = { 0 };											//全地图标识数组
	//检测移动方向
	void direction_snake()
	{
		if (kbhit())
		{
			int ch = getch();
			if(ch == -32||ch == 0)
			{
				ch = getch();
			}
			switch (ch)
			{
				case 'p':	ispause =  true;break;
				case 'q':	exit(0);break;
				case 'h':	Help();break;
				case 'c':	Chose_Scene();step_c = true;break;
				case ',':	Speed-=10;break;
				case '.':	Speed+=10;break;
				case UP:	snake_direction = UP;	break; 
				case DOWN:	snake_direction = DOWN;	break;
				case LEFT:	snake_direction = LEFT;	break;
				case RIGHT:	snake_direction = RIGHT;break;
				default: break;
			}
			ch = 0;
		}
		if(ispause == false)
		{
			eraser_snake();
			flag_snake();
			draw_snake();
		}
	}
	//标记蛇
	void flag_snake()
	{
		int Snake_ix, Snake_iy;
		
		for (int x = 0; x < LEN; x++)
		for (int y = 0; y < WID; y++)
		{
			if (snake[x][y] == 1)//记录蛇头Snake_ix/iy
			{
				switch (snake_direction)
				{
					case UP:Snake_ix = x; Snake_iy = y - 1; break;
					case DOWN:Snake_ix = x; Snake_iy = y + 1; break;
					case LEFT:Snake_ix = x-1; Snake_iy = y; break;
					case RIGHT:Snake_ix = x+1; Snake_iy = y; break;
					default: break;

				}
				if ((snake[Snake_ix][Snake_iy]  == -3)||(snake[Snake_ix][Snake_iy]  == -2))//检查移动后头坐标是否满足规则
				{
					alive = false;
				}
				if (snake[Snake_ix][Snake_iy] > 0)//检查移动后头位置是否满足规则
				{
					alive = false;
				}
				if (snake[Snake_ix][Snake_iy] == -1)
				{
					snake_length++;
					Score += 10 * (1 + 0.5*Lv);
					Lv_system();
					eatup = true;
				}
				if (snake[Snake_ix][Snake_iy] == -10)
				{
					Lv++;
					Set_Cursor_To(25,12);
					printf("Lv Up! Lv:%d",Lv);
					Sleep(1000);
					system("cls");
					draw_boundary();
					draw_barriers();
					draw_magic();
					ismagic = false;
				}
				if (snake[Snake_ix][Snake_iy] == -11)
				{
					Score += 20;
					Lv_system();
					ismagic = false;
				}
				if (snake[Snake_ix][Snake_iy] == -12)
				{
					Score += 40;
					Lv_system();
					ismagic = false;
				}
				if (snake[Snake_ix][Snake_iy] == -13)
				{
					Score += 80;
					Lv_system();
					ismagic = false;
				}
				if (snake[Snake_ix][Snake_iy] == -14)
				{
					Score += 100;
					Lv_system();
					ismagic = false;
				}
			}
			if (snake[x][y] == snake_length)//必要时清除蛇尾
			{
				snake[x][y] = 0;
			}
			if (snake[x][y] > 0)//对蛇身进行编号
			{
				snake[x][y] += 1;
			}
		}
		snake[Snake_ix][Snake_iy] = 1;
	}
	//画蛇
	void draw_snake()
	{
		for (int i = 0; i < LEN;i++)
		for (int j = 0; j < WID;j++)
		{
			if (snake[i][j]>0 &&snake[i][j]<800)
			{
				Set_Cursor_To(2 * i, j);
				printf("■");
			}
			if(snake[i][j] == -1)
			{
				Set_Cursor_To(2 * i, j);
				printf("★");
			}
		}
	}
	//除尾
	void eraser_snake()
	{
		for (int i = 0; i < LEN;i++)
		for (int j = 0; j < WID; j++)
		{
			if (snake[i][j] == snake_length)
			{
				Set_Cursor_To(2 * i, j);
				printf(" ");
			}
		}
	}
/*=========================================================================================================*/
//食物相关
	//生产食物
	void product_food()
	{
		int food_ix, food_iy;

		food_ix = rand() % LEN;
		food_iy = rand() % WID;
		while (snake[food_ix][food_iy] != 0)
		{
			food_ix = rand() % LEN;
			food_iy = rand() % WID;
		}
		snake[food_ix][food_iy] = -1;
		Set_Cursor_To(2 * food_ix, food_iy);
		printf("★");
		
		eatup = false;
	}
	//生成障碍
	void product_barriers(int i)
	{
		int barrier_ix,barrier_iy;
		
		for(;i>0;i--)
		{
			do
			{
				barrier_ix = rand()%LEN;
				barrier_iy = rand()%WID;
			}while(snake[barrier_ix][barrier_iy]!=0);
			snake[barrier_ix][barrier_iy] = 10000;
			Set_Cursor_To(2*barrier_ix,barrier_iy);
			printf("χ");
		}
	} 
	//清除障碍
	void eraser_barriers()
	{
		for (int i = 0; i < LEN;i++)
		for (int j = 0; j < WID; j++)
		{
			if (snake[i][j] == 10000)
			{
				Set_Cursor_To(2 * i, j);
				printf(" ");
			}
		}
	} 
	//画障碍
	void draw_barriers()
	{
		for (int i = 0; i < LEN;i++)
		for (int j = 0; j < WID; j++)
		{
			if (snake[i][j] >= 10000)
			{
				Set_Cursor_To(2 * i, j);
				printf("χ");
			}
		}
	} 
	//生产道具
	void product_magic()
	{
		int magic_ix,magic_iy;
		
		if(Score*11%6 == 3)
		{
			do
			{
				magic_ix = rand()%LEN;
				magic_iy = rand()%WID;
			}while(snake[magic_ix][magic_iy]!=0);
			snake[magic_ix][magic_iy] = -rand()%4 - 10;
			ismagic = true;
		}
	} 
	//画道具
	void draw_magic()
	{
		for (int i = 0; i < LEN;i++)
		for (int j = 0; j < WID; j++)
		{
			if (snake[i][j] == -10)
			{
				Set_Cursor_To(2 * i, j);
				printf("№"); 
			}
			if (snake[i][j] == -11)
			{
				Set_Cursor_To(2 * i, j);
				printf("Ⅰ"); 
			}
			if (snake[i][j] == -12)
			{
				Set_Cursor_To(2 * i, j);
				printf("Ⅱ"); 
			}
			if (snake[i][j] == -13)
			{
				Set_Cursor_To(2 * i, j);
				printf("Ⅲ"); 
			}
			if (snake[i][j] == -14)
			{
				Set_Cursor_To(2 * i, j);
				printf("Ⅳ"); 
			}
		}
	} 
/*=========================================================================================================*/
//环境相关
	//边界
	void draw_boundary()
	{
		for (int i = 0; i < LEN; i++)//上下边界
		{
			Set_Cursor_To(2 * i, 0);
			printf("--");
			Set_Cursor_To(2 * i, WID - 1);
			printf("--");
			snake[i][0] = -2;
			snake[i][WID - 1] = -2;
		}
		for (int i = 0; i < WID; i++)
		{
			Set_Cursor_To(0, i);
			printf("|");
			Set_Cursor_To(2*LEN - 1, i);
			printf("|");
			snake[0][i] = -3;
			snake[LEN-1][i] = -3;
		}
	}
	//信息
	void draw_info()
	{
		Set_Cursor_To(63,1);
		printf("'H':Help.");
		Set_Cursor_To(63,3);
		printf("'P':Pause.");
		Set_Cursor_To(63,5);
		printf("'Q':Quit.");
		Set_Cursor_To(63,7);
		printf("'C':Chose Scenes.");
		Set_Cursor_To(63,9);
		printf("',':Speed Up.");
		Set_Cursor_To(63,11);
		printf("'.':Speed Down.");
		Set_Cursor_To(63,19);
		printf("Speed:\t0.%03ds",Speed); 
		Set_Cursor_To(63,20);
		printf("Score:\t%d",Score); 
		Set_Cursor_To(63,21);
		printf("Lv:\t%d",Lv);
		Set_Cursor_To(63,22);
		printf("Length:\t%d",snake_length);
		Set_Cursor_To(63,23);
		printf("Barriers:%d",num);
	} 
/*=========================================================================================================*/
//控制相关
	//判定游戏结束
	void Game_Over()
	{
		Set_Cursor_To(25, 12);
		printf("~~傻屌,你死了~~");
		Set_Cursor_To(21, 13);
		printf("Press any key to replay.");
		system("pause > nul");
	}
	//初始化
	void initialize()
	{
		Score = 0;
		Lv = 1;
		snake_length = 3;
		for (int x = 0; x < LEN; x++)
		for (int y = 0; y < WID; y++)
		{
			snake[x][y] = 0;
		}		
		draw_boundary();
		draw_info();
		for (int i = 0; i < snake_length; i++)                  //初始化蛇
		{
			snake[i + 12][12] = i + 1;
		}
		draw_snake();
		product_food();
		alive = true;
		ispause = false;
		snake_direction = LEFT;
	}
	//等级系统
	void Lv_system()
	{
		if(Score > (Lv+1)*(Lv)*20)
		{
			Lv++;
			Set_Cursor_To(25,12);
			printf("Lv Up! Lv:%d",Lv);
			Sleep(1000);
			system("cls");
			draw_boundary();
			draw_barriers();
			draw_magic();
		}
	} 
	//帮助界面
	void Help()
	{
		system("cls");
		Set_Cursor_To(10,2);
		printf("It's a game produced by Bit[CS2013]_Percy.");
		Set_Cursor_To(10,4);
		printf("Game's name: Gluttonous Snake.");
		Set_Cursor_To(10,6);
		printf("Game's version: V2.0.");
		Set_Cursor_To(10,8);
		printf("Game's operation: you can press'Up'、'Down'、'Left'、\n\t\t'Right' to control the snake to eat the star.");
		Set_Cursor_To(10,11);
		printf("When the snake eatup the star,the score will increase.\n\t\tAnd your level will increase,too.");
		Set_Cursor_To(10,14);
		printf("Try to eat more STAR ~ ");
		Set_Cursor_To(10,15);
		printf("Oh,I nearly foget to say that you will be much longer\n\t\t when you eat the star.");
		Set_Cursor_To(10,18);
		printf("■:The body of the snake.");
		Set_Cursor_To(25,18);
		printf("№:Lv UP.");
		Set_Cursor_To(25,18);
		printf("χ:The barriers.");
		Set_Cursor_To(10,20);
		printf("Ⅰ:20 scores.");
		Set_Cursor_To(25,20);
		printf("Ⅱ:40 scores.");
		Set_Cursor_To(40,20);
		printf("Ⅲ:80 scores.");
		Set_Cursor_To(55,20);
		printf("Ⅳ:100 scores.");
		Set_Cursor_To(10,22);
		printf("★:The star which can increase your length and scores.");
		
		system("pause > nul");
		system("cls");
		draw_boundary();
		draw_barriers();
		draw_magic();
	} 
	//场景选择
	void Chose_Scene()
	{
		system("cls");
		Set_Cursor_To(10,4);
		printf("Please chose a scene you like.");
		Set_Cursor_To(10,6);
		printf("1、No barriers.");
		Set_Cursor_To(10,8);
		printf("2、Barriers.");
		
		switch(getch())
		{
			case '1': Scene = 1;break;
			case '2': Scene = 2;break;
			default : break;
		}
		if(Scene == 2)
		{
			Set_Cursor_To(10,10);
			printf("Please input the number of barriers:");
			scanf("%d",&num);
		}
		system("cls");
		draw_boundary();
	} 
/*=========================================================================================================*/
int main()
{
	system("title Bit[CS2013]_Percy_贪吃蛇V2.0");
	system("mode con:cols=100 lines= 30");
	Chose_Scene();
	srand((unsigned)time(NULL));
	Now_time = clock();
	while(1)
	{
		if(Scene == 1)
			{
				flag1 = 0;
				while (1)
				{
					system("cls");
					initialize();
					while (1)
					{
						if (alive == false)
						{
							Game_Over();
							break;
						}
						else
						{
							if (eatup == true)
							{
								product_food();
							}
							if(ispause == true)
							{	
								system("pause > nul");
								ispause = false;
							}
							if(ismagic == false)
							{
								product_magic();
								draw_magic();
								ismagic == true;
							}
							direction_snake();
							if(Scene != 1)
							{
								break;
							}
							draw_info();
							Sleep(Speed);
						}
					}
					if(Scene != 1)
							{
								break;
							}
				}
		}
		else
		{
			for(int times = 0;;times++)
				{
					system("cls");
					initialize();
					if(times != 0 || flag1 == 1)
						product_barriers(num);
					while (1)
					{
						if (alive == false)
						{
							Game_Over();
							break;
						}
						else
						{
							if (eatup == true)
							{
								product_food();
							}
							if(ispause == true)
							{	
								system("pause > nul");
								ispause = false;
							}
							if(ismagic == false)
							{
								product_magic();
								draw_magic();
								ismagic == true;
							}
							direction_snake();
							if(Scene != 2)
							{
								break;
							}
							else
							{
								if(step_c == true)
								{
									eraser_barriers();
									product_barriers(num);
									step_c = false;
								}
							}
							draw_info();
							Sleep(Speed);
						}
					}
					if(Scene != 2)
					{
						break;
					}
				}
		}
	}
	return 0;
}
/*=========================================================================================================*/


1. 用户与权限管理模块 角色管理: 学生:查看实验室信息、预约设备、提交耗材申请、参与安全考核 教师:管理课题组预约、审批学生耗材申请、查看本课题组使用记录 管理员:设备全生命周期管理、审核预约、耗材采购与分发、安全检查 用户操作: 登录认证:统一身份认证(对接学号 / 工号系统,模拟实现),支持密码重置 信息管理:学生 / 教师维护个人信息(联系方式、所属院系),管理员管理所有用户 权限控制:不同角色仅可见对应功能(如学生不可删除设备信息) 2. 实验室与设备管理模块 实验室信息管理: 基础信息:实验室编号、名称、位置、容纳人数、开放时间、负责人 功能分类:按学科(计算机实验室 / 电子实验室 / 化学实验室)标记,关联可开展实验类型 状态展示:实时显示当前使用人数、设备运行状态(正常 / 故障) 设备管理: 设备档案:名称、型号、规格、购置日期、单价、生产厂家、存放位置、责任人 全生命周期管理: 入库登记:管理员录入新设备信息,生成唯一资产编号 维护记录:记录维修、校准、保养信息(时间、内容、执行人) 报废处理:登记报废原因、时间,更新设备状态为 "已报废" 设备查询:支持按名称、型号、状态多条件检索,显示设备当前可用情况 3. 预约与使用模块 预约管理: 预约规则:学生可预约未来 7 天内的设备 / 实验室,单次最长 4 小时(可设置) 预约流程:选择实验室→选择设备→选择时间段→提交申请(需填写实验目的) 审核机制:普通实验自动通过,高危实验(如化学实验)需教师审核 使用记录: 签到 / 签退:到达实验室后扫码签到,离开时签退,系统自动记录实际使用时长 使用登记:填写实验内容、设备运行情况(正常 / 异常),异常情况需详细描述 违规管理:迟到 15 分钟自动取消预约,多次违规限制预约权限 4. 耗材与安全管理模块 耗材管理: 耗材档案:名称、规格、数量、存放位置、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值