跳到正文

个人记录

自制贪吃蛇游戏~

code_12342约 29 分钟
原文
文章信息与版本

文章信息

发布
2024年7月22日
收录
2025年12月12日
更新
2026年11月2日
篇幅
约 29 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h> 
#include <stdlib.h>
using namespace std;
int a, mod = 0, movment = 0,hang = 5, lie = 5, len = 3, speed = 300, hx = 1, hy = 1, score = -1, b = 1, ans = 0, developermod = 1, zhangai = 0, modx = 1, endx = -10086, endy = -10086, autolen = 3, bestscore = 0;
bool seta = false, gameOver = false, debugmod = false, check = false, newa = false;
int x, y;
string mody = "经典模式";
int cross[105][105]; //棋盘,0为空,1为蛇头,2为蛇身,3为水果
int visx[100005];
int visy[100005]; 
string name = "经典";
string DIR = "R";
string endn = "Game-Over!";
bool validFruit = false;
void initialization() {
	memset(cross, 0, sizeof(cross));
	newa = false;
	gameOver = false;
	DIR = "R";
	len = autolen;
	bestscore = max(bestscore, score);
	if (bestscore == -1) bestscore = 0;
	score = -1;
	movment = 0;
	check = false;
}
void fruit()
{
    while (!validFruit)
    {
        x = rand() % hang + 1;
        y = rand() % lie + 1;
        if (cross[x][y] == 0 && x != 0 && y != 0)
        {
            cross[x][y] = 3;
            validFruit = true;
        }
    }
}
void Input()
{
    if (_kbhit())
    {
        switch (_getch())
        {
        case 'a':
            DIR = "L";
            break;
        case 'd':
            DIR = "R";
            break;
        case 'w':
            DIR = "U";
            break;
        case 's':
            DIR = "D";
            break;
        case ' ':
        	b = 3;
        	break;
        case 'x':
            gameOver = true;
            break;
        }
        
    }
}
void draw()
{
	check = false;
	for (int i = 1;i <= hang + 2;i++)
	{
		cout << "□";
	}
	cout << "              score:" << score;
	if (newa) cout << "         新纪录!";
	cout << endl;
	Input();
	for (int i = 1;i <= lie;i++) 
	{
		Input();
		cout << "□";
		for (int j = 1; j <= hang;j++)
		{
			if (cross[j][i] == 0) cout << "  "; //空气 
			if (cross[j][i] == 1) cout << " O"; //头 
			if (cross[j][i] == 2) cout << " o"; //身体 
			if (cross[j][i] == 3) cout << " *"; //水果 
			if (cross[j][i] == 4) cout << " X";
			if (cross[j][i] == 5) cout << "□";
			if (cross[j][i] == 3) check = true;
		}
		cout << "□";
		if (i == 2) cout << "              bestscore:" << bestscore;
		cout << endl;
	}
	for (int i = 1;i <= hang + 2;i++)
	{
		cout << "□";
	}
	if (lie < 2 && developermod == 0) cout << "              bestscore:" << bestscore << endl;
	if (lie < 2 && developermod == 1) cout << "              bestscore:" << bestscore;
	if (developermod == 1)
	{
		cout << endl << endl;
		cout << "     x:" << hx << "     y:" << hy << endl << endl;
		cout << "     当前指令:" << DIR << endl << endl;
		cout << "     蛇身长度:" << len << endl << endl;
		cout << "     总行数:" << hang << "     总列数:" << lie << endl << endl; 
		cout << "     移动次数:" << movment << endl << endl;
		cout << "     果子x:" << x << "     y:" << y << endl << endl;
		cout << "     果子是否生成:" << check << endl;
	} 
	
} 

int main()
{
	system("color 2");
	developermod = 0;
	srand(time(0));
	while(1)
	{
		initialization();
		if (!debugmod) system("color 2");
		else system("color 6");  
		cout << "C++ 贪吃蛇    made by 豌豆射手" << endl << endl;
		cout << ">>> 开始游戏" << endl;
		cout << "    游戏设置" << endl;
		cout << "    游戏预设" << endl;
		while(1)
		{
			if (_kbhit() && !developermod)
			{
				if (_getch() == ' ') developermod = 1;
			} 
			if (_kbhit() && developermod && !debugmod) {
				if (_getch() == 'R' && developermod) debugmod = true;
			}
			if (developermod) cout << endl << "开发者模式 developermod"; 
			if (debugmod)
			{
				system("color 6");
				cout << " + 调试模式(无敌模式)";
			}
			if (!seta) a = _getch();
			seta = false;
			system("cls");
			cout << "C++ 贪吃蛇    made by 豌豆射手" << endl << endl;
			if (a == 115 && mod == 1) mod = 2;
			if (a == 115 && mod == 0) mod = 1;
			if (a == 119 && mod == 1) mod = 0;
			if (a == 119 && mod == 2) mod = 1;
			if (mod == 0) 
			{
				cout << ">>> 开始游戏" << endl;
				cout << "    游戏设置" << endl;
				cout << "    游戏预设" << endl; 
			}
			else if (mod == 1) 
			{
				cout << "    开始游戏" << endl;
				cout << ">>> 游戏设置" << endl;
				cout << "    游戏预设" << endl; 
			}
			else
			{
				cout << "    开始游戏" << endl;
				cout << "    游戏设置" << endl;
				cout << ">>> 游戏预设" << endl; 
			} 
			if (a == 100 && mod == 0)
			{
				system("cls");
				break;
			} 
			if (a == 100 && mod == 1)
			{
				system("cls");
				cout << "设置 Settings" << endl;
				cout << ">>> 行 => " << hang << endl;
				cout << "    列 => " << lie << endl;
				cout << "    速度(毫秒)=> " << speed << endl;
				cout << "    初始长度 => " << len << endl;
				cout << "    退出设置 " << endl;
				while(1)
				{
					a = _getch();
					system("cls");
					if (a == 115 && mod != 5) mod += 1;
					if (a == 119 && mod != 1) mod -= 1;
					if (a == 97)
					{
						mody = "自定义";
						if (mod == 1) hang -= 1;
						if (mod == 2) lie -= 1;
						if (mod == 3) speed -= 25;
						if (mod == 4) len -= 1;
						if (mod == 5)
						{
							mod = 1;
							seta = true;
							break;
						}
					}
					if (a == 100)
					{
						mody = "自定义";
						if (mod == 1) hang += 1;
						if (mod == 2) lie += 1;
						if (mod == 3) speed += 25;
						if (mod == 4) len += 1;
						/**if (mod == 5)
						{
							mod = 1;
							seta = true;
							break;
						}**/
					}
					if (mod == 1)
					{
						cout << "设置 Settings" << endl;
						cout << ">>> 行 => " << hang << endl;
						cout << "    列 => " << lie << endl;
						cout << "    速度(毫秒)=> " << speed << endl;
						cout << "    初始长度 => " << len << endl;
						cout << "    退出设置 " << endl;
					}
					else if (mod == 2)
					{
						cout << "设置 Settings" << endl;
						cout << "    行 => " << hang << endl;
						cout << ">>> 列 => " << lie << endl;
						cout << "    速度(毫秒)=> " << speed << endl;
						cout << "    初始长度 => " << len << endl;
						cout << "    退出设置 " << endl;
					}
					else if (mod == 3)
					{
						cout << "设置 Settings" << endl;
						cout << "    行 => " << hang << endl;
						cout << "    列 => " << lie << endl;
						cout << ">>> 速度(毫秒)=> " << speed << endl;
						cout << "    初始长度 => " << len << endl;
						cout << "    退出设置 " << endl;
					}
					else if (mod == 4)
					{
						cout << "设置 Settings" << endl;
						cout << "    行 => " << hang << endl;
						cout << "    列 => " << lie << endl;
						cout << "    速度(毫秒)=> " << speed << endl;
						cout << ">>> 初始长度 => " << len << endl;
						cout << "    退出设置 " << endl;
					}
					else
					{
						cout << "设置 Settings" << endl;
						cout << "    行 => " << hang << endl;
						cout << "    列 => " << lie << endl;
						cout << "    速度(毫秒)=> " << speed << endl;
						cout << "    初始长度 => " << len << endl;
						cout << ">>> 退出设置 " << endl;
					}
				}
			} 
			if (a == 100 && mod == 2)
			{
				system("cls");
				cout << "游戏预设 Game presets" << endl;
				cout << ">>> 经典模式" << endl;
				cout << "    宽阔场地" << endl;
				cout << "    急速惊魂" << endl;
				cout << "    狭窄走廊" << endl;
				cout << "    一望无际" << endl;
				cout << "    退出预设" << endl << endl;
				cout << "详细信息 Detailed information" << endl << endl;
				cout << "行 -- 5   列 -- 5   速度 -- 300   初始长度 -- 3" << endl;
				cout << "难度 -- ★★★" << endl;
				cout << endl << "当前模式:" << mody << endl;
				while (1)
				{
					a = _getch();
					system("cls");
					if (a == 115 && modx != 6) modx += 1;
					if (a == 119 && modx != 1) modx -= 1;
					if (a == 97 && modx == 6)
					{
						cout << "C++ 贪吃蛇    made by 豌豆射手" << endl << endl;
						cout << "    开始游戏" << endl;
						cout << "    游戏设置" << endl;
						cout << ">>> 游戏预设" << endl;
						break;
					} 
					if (a == 100)
					{
						if (modx == 1)
						{
							hang = 5;
							lie = 5;
							len = 3;
							speed = 300;
							mody = "经典模式";
						}
						else if (modx == 2)
						{
							hang = 10;
							lie = 10;
							len = 8;
							speed = 225;
							mody = "宽阔场地";
						}
						else if (modx == 3)
						{
							hang = 8;
							lie = 8;
							len = 5;
							speed = 100;	
							mody = "急速惊魂";
						}
						else if (modx == 4)
						{
							hang = 18;
							lie = 4;
							len = 8;
							speed = 150;
							mody = "狭窄走廊";
						}
						else if (modx == 5) 
						{
							hang = 12;
							lie = 12;
							len = 12;
							speed = 250;
							mody = "一望无际";
						}
					}
					if (modx == 1)
					{
						//system("cls");
						cout << "游戏预设 Game presets" << endl;
						cout << ">>> 经典模式" << endl;
						cout << "    宽阔场地" << endl;
						cout << "    急速惊魂" << endl;
						cout << "    狭窄走廊" << endl;
						cout << "    一望无际" << endl;
						cout << "    退出预设" << endl << endl;
						cout << "详细信息 Detailed information" << endl << endl;
						cout << "行 -- 5   列 -- 5   速度 -- 300   初始长度 -- 3" << endl;
						cout << "难度 -- ★★★" << endl;
					}
					else if (modx == 2)
					{
						//system("cls");
						cout << "游戏预设 Game presets" << endl;
						cout << "    经典模式" << endl;
						cout << ">>> 宽阔场地" << endl;
						cout << "    急速惊魂" << endl;
						cout << "    狭窄走廊" << endl;
						cout << "    一望无际" << endl;
						cout << "    退出预设" << endl << endl;
						cout << "详细信息 Detailed information" << endl << endl;
						cout << "行 -- 10   列 -- 10   速度 -- 225   初始长度 -- 8" << endl;
						cout << "难度 -- ★★" << endl;
					}
					else if (modx == 3)
					{
						//system("cls");
						cout << "游戏预设 Game presets" << endl;
						cout << "    经典模式" << endl;
						cout << "    宽阔场地" << endl;
						cout << ">>> 急速惊魂" << endl;
						cout << "    狭窄走廊" << endl;
						cout << "    一望无际" << endl;
						cout << "    退出预设" << endl << endl;
						cout << "详细信息 Detailed information" << endl << endl;
						cout << "行 -- 8   列 -- 8   速度 -- 100   初始长度 -- 5" << endl;
						cout << "难度 -- ★★★★★" << endl;
					}
					else if (modx == 4)
					{
						//system("cls");
						cout << "游戏预设 Game presets" << endl;
						cout << "    经典模式" << endl;
						cout << "    宽阔场地" << endl;
						cout << "    急速惊魂" << endl;
						cout << ">>> 狭窄走廊" << endl;
						cout << "    一望无际" << endl;
						cout << "    退出预设" << endl << endl;
						cout << "详细信息 Detailed information" << endl << endl;
						cout << "行 -- 18   列 -- 4   速度 -- 150   初始长度 -- 8" << endl;
						cout << "难度 -- ★★★★" << endl;
					}
					else if (modx == 5)
					{
						//system("cls");
						cout << "游戏预设 Game presets" << endl;
						cout << "    经典模式" << endl;
						cout << "    宽阔场地" << endl;
						cout << "    急速惊魂" << endl;
						cout << "    狭窄走廊" << endl;
						cout << ">>> 一望无际" << endl;
						cout << "    退出预设" << endl << endl;
						cout << "详细信息 Detailed information" << endl << endl;
						cout << "行 -- 12   列 -- 12   速度 -- 250   初始长度 -- 12" << endl;
						cout << "难度 -- ★★" << endl;
					}
					else
					{
						//system("cls");
						cout << "游戏预设 Game presets" << endl;
						cout << "    经典模式" << endl;
						cout << "    宽阔场地" << endl;
						cout << "    急速惊魂" << endl;
						cout << "    狭窄走廊" << endl;
						cout << "    一望无际" << endl;
						cout << ">>> 退出预设" << endl;
					}
					cout << endl << "当前模式:" << mody << endl; 
				}
				
			}
		}
		autolen = len;
		hx = hang / 2;
		hy = lie / 2;
		int cur = 1;
		for (int i = 1;i <= len;i++)
		{
			if (gameOver) break;
			if (cross[hx][hy] == 3)
			{
				len++;
				score += len * 2;
				validFruit = false;
			} 
			if (!validFruit) fruit();
			if ((cross[hx][hy] == 1 || cross[hx][hy] == 2 || hx > hang || hy > lie || hx < 1 || hy < 1) && !debugmod) 
			{
				system("cls");
				cross[hx][hy] = 4;
				draw();
				gameOver = true;
				break;
			}
			movment++;
			cross[hx][hy] = 1;
			visx[cur] = hx;
			visy[cur] = hy;
			cur++;
			system("cls");
			score++;
			if (b == 3) score--;
			if (score > bestscore) newa = true;
			bestscore = max(bestscore, score);
			b = 1;
			draw();
			if (check == false)
			{
				while (check == false)
				{
					validFruit = false;
					fruit();
					system("cls");
					draw();
				} 
			}
			cross[hx][hy] = 2;
			Input();
			if (DIR == "L") hx -= 1;
			else if (DIR == "R") hx += 1;
			else if (DIR == "U") hy -= 1;
			else if (DIR == "D") hy += 1;
			ans = speed / b;
			Sleep(ans);
		}
		if (gameOver)
		{
			system("color 4");
			//developermod = 0;
			system("cls");
			draw();
			//cout << endl << endl << "        Game Ower!" << endl << endl;
			cout << endl << endl << "        ";
			for (int i = 0;i < endn.size();i++) {
				cout << endn[i];
				Sleep(100);
			}
			cout << endl << endl;
			Sleep(400);
			cout << "        你的分数为:" << score << endl << endl;
			Sleep(400);
			cout << "        蛇的长度为:" << len << endl << endl;
			Sleep(400);
			cout << "        感谢您的游玩!" << endl << endl; 
			Sleep(400);
			system("pause");
			system("cls");
		} 
		while(1)
		{
			if (gameOver) break;
			if (cross[hx][hy] == 3)
			{
				len++;
				score += len * 2;
				validFruit = false;
			} 
			if (!validFruit) fruit();
			if ((cross[hx][hy] == 1 || cross[hx][hy] == 2 || hx > hang || hy > lie || hx < 1 || hy < 1) && !debugmod && !(hx == endx && hy == endy && cross[hx][hy] == 2))
			{
				system("cls");
				cross[hx][hy] = 4;
				draw();
				gameOver = true;
				break;
			}
			movment++;
			visx[cur] = hx;
			visy[cur] = hy;
			cur++;
			cross[visx[cur - len - 1]][visy[cur - len - 1]] = 0;
			endx = visx[cur - len];
			endy = visy[cur - len];
			cross[hx][hy] = 1;
			system("cls");
			score++;
			if (b == 3) score--;
			if (score > bestscore) newa = true;
			bestscore = max(bestscore, score);
			b = 1;
			draw();
			if (check == false)
			{
				while (check == false)
				{
					validFruit = false;
					fruit();
					system("cls");
					draw();
				} 
			}
			cross[hx][hy] = 2;
			Input();
			if (DIR == "L") hx -= 1;
			else if (DIR == "R") hx += 1;
			else if (DIR == "U") hy -= 1;
			else if (DIR == "D") hy += 1;
			ans = speed / b;
			Sleep(ans);
		}
		if (gameOver)
		{
			system("color 4");
			//developermod = 0;
			system("cls");
			draw();
			//cout << endl << endl << "        Game Ower!" << endl << endl;
			cout << endl << endl << "        ";
			for (int i = 0;i < endn.size();i++) {
				cout << endn[i];
				Sleep(100);
			}
			cout << endl << endl;
			Sleep(400);
			cout << "        你的分数为:" << score << endl << endl;
			Sleep(400);
			cout << "        蛇的长度为:" << len << endl << endl;
			Sleep(400);
			cout << "        感谢您的游玩!" << endl << endl; 
			Sleep(400);
			system("pause"); 
			system("cls");
		} 
	}
}
原文