跳到正文

科技·工程

贪吃蛇-Retro Snaker 3.0

Metaphysical约 13 分钟
原文
文章信息与版本

文章信息

发布
2025年5月13日
收录
2025年12月12日
更新
2026年10月27日
篇幅
约 13 分钟
获赞
1

版本

  1. v1当前

可以转载,但请标明原创。

复原:蓝星纪元年(原 Sile 工作室)

玩法:

  • 吃掉地图上生成的食物,使贪吃蛇变长。
  • 撞到墙壁或贪吃蛇的身体死亡。

源码:

C++
#include <iostream>
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <queue>

using namespace std;

const int N = 20, M = 2e5;
int map[N][N], len = 3, Jsq = 0, FX = 3, direction = 3, X = 8, Y = 9, T = 0;
struct node
{
	int x, y;
} Snake[M];

void Play ()
{
	bool flag = false;
	int gx = rand () % 16 + 1, gy = rand () % 16 + 1;
	while (map[gx][gy] != 0) gx = rand () % 16 + 1, gy = rand () % 16 + 1;
	map[gx][gy] = 1;
	while (true)
	{
		SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 8);
		printf ("\n  方向-direction:");
		SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 15);
		if (direction == 1) printf ("左-left ");
		if (direction == 2) printf ("上-Up   ");
		if (direction == 3) printf ("右-right");
		if (direction == 4) printf ("下-down ");
		SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 8);
		printf ("\n  长度-len:");
		SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 15);
		printf ("%03d\n  ", len);
		SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 8);
		printf ("坐标-coordinate:");
		SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 7);
		printf ("X:");
		SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 15);
		printf ("%02d ", X);
		SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 7);
		printf ("Y:");
		SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 15);
		printf ("%02d\n", Y);
		for (int i = 0; i <= 16; i ++)
		{
			printf ("  ");
			for (int j = 0; j <= 17; j ++)
			{
				SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 15);
				if ((j == 0 || j == 17) && i != 0)
				{
					SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 11);
					printf ("|");
				}
				else if (i == 0 && j != 0 && j != 17)
				{
					SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 11);
					printf ("__");
				}
				else if (i == 16 && j != 0 && j != 17 && map[i][j] == 0)
				{
					SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 11);
					printf ("__");
				}
				else if (i == 0 && (j == 0 || j == 17)) printf (" ");
				else if (map[i][j] == 0) printf ("  ");
				else if (map[i][j] == 2) printf ("━ ");
				else if (map[i][j] == 3) printf ("┃ ");
				else if (map[i][j] == 4) printf ("┏ ");
				else if (map[i][j] == 5) printf ("┓ ");
				else if (map[i][j] == 6) printf ("┗ ");
				else if (map[i][j] == 7) printf ("┛ ");
				else if (map[i][j] == 1)
				{
					SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 6);
					printf ("○");
				}
			}
			printf ("\n");
		}
		if (flag)
		{
			Sleep (1000);
			break;
		}
		int xX = 18, xY = 18;
		FX = (FX != direction ? FX : direction);
		if (GetAsyncKeyState (65) & 0x8000) FX = 1;
		else if (GetAsyncKeyState (87) & 0x8000) FX = 2;
		else if (GetAsyncKeyState (68) & 0x8000) FX = 3;
		else if (GetAsyncKeyState (83) & 0x8000) FX = 4;
		if (abs (direction - FX) == 2) FX = direction;
		if (FX == 1) xX = X, xY = Y - 1;
		else if (FX == 2) xX = X - 1, xY = Y;
		else if (FX == 3) xX = X, xY = Y + 1;
		else if (FX == 4) xX = X + 1, xY = Y;

		map[18][18] = 0;
		if (abs (direction - FX) != 2 && clock () - T >= 300)
		{
			if (map[xX][xY] == 1)
			{
				len ++;
				while (map[gx][gy] != 0) gx = rand () % 16 + 1, gy = rand () % 16 + 1;
				map[gx][gy] = 1;
			}
			T = clock ();
			if (xX == 0 || xX == 17 || xY == 0 || xY == 17 || map[xX][xY] != 0 && map[xX][xY] != 1) flag = true;
			if (direction == 1 && FX == 1 || direction == 3 && FX == 3) map[X][Y] = 2, map[xX][xY] = ! (FX % 2) + 2;
			if (direction == 2 && FX == 2 || direction == 4 && FX == 4) map[X][Y] = 3, map[xX][xY] = ! (FX % 2) + 2;
			if (direction == 1 && FX == 4 || direction == 2 && FX == 3) map[X][Y] = 4, map[xX][xY] = ! (FX % 2) + 2;
			if (direction == 3 && FX == 4 || direction == 2 && FX == 1) map[X][Y] = 5, map[xX][xY] = ! (FX % 2) + 2;
			if (direction == 4 && FX == 3 || direction == 1 && FX == 2) map[X][Y] = 6, map[xX][xY] = ! (FX % 2) + 2;
			if (direction == 3 && FX == 2 || direction == 4 && FX == 1) map[X][Y] = 7, map[xX][xY] = ! (FX % 2) + 2;
			if (xX != 18 && xY != 18) X = xX, Y = xY, Snake[++ Jsq].x = X, Snake[Jsq].y = Y;
			else Snake[++ Jsq].x = X, Snake[Jsq].y = Y;
			direction = FX, map[Snake[max (Jsq - len, 0)].x][Snake[max (Jsq - len, 0)].y] = 0, Snake[max (Jsq - len, 0)].x = 0, Snake[max (Jsq - len, 0)].y = 0;
		}
		COORD pos = {0, 0};
		HANDLE out = GetStdHandle (STD_OUTPUT_HANDLE);
		SetConsoleCursorPosition (out, pos);
	}
	return;
}

int main ()
{
	srand (time (NULL));
    system ("mode con cols=70 lines=17");
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), & cursor_info);
	system ("title 贪吃蛇-Retro Snaker"); 
	SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 11);
	printf ("\n  ____                            ___\n  |   \\                          /   \\             |\n  |___/  ___  __|__    _  ___    \\___    __   ___  |     ___     _\n  | \\   /___\\   |   /|/  /   \\       \\ |/  \\ /   | |__/ /___\\ /|/\n  |  \\_ \\___    \\_/  |   \\___/   \\___/ |   | \\___\\ | \\_ \\___   |\n\n  ");
	SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 10);
	printf ("--------------++++++======== 贪吃蛇 ========++++++--------------\n\n  ");
	SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 8);
	printf ("复原:");
	SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 15);
	printf ("蓝星纪元年");
	SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 7);
	printf ("(原 Sile 工作室)\n\n  ");
	SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 8);
	printf ("玩法:\n    ");
	SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 15);
	printf ("吃掉地图上生成的食物,使贪吃蛇变长。\n    不要撞到墙壁或贪吃蛇的身体。\n\n  ");
	SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 4);
	printf ("(点击任意键继续)");
	while (! kbhit ()) {}
    system ("cls");
    map[8][7] = 2, map[8][8] = 2, map[8][9] = 2, Snake[++ Jsq].x = 8, Snake[Jsq].y = 7, Snake[++ Jsq].x = 8, Snake[Jsq].y = 8, Snake[++ Jsq].x = 8, Snake[Jsq].y = 9;
	system ("mode con cols=41 lines=22");
    Play ();
    system ("cls");
    system ("mode con cols=45 lines=7");
	SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 11);
    printf ("\n  _____       _____     _____        ____\n    |   |   | |         |     |\\   | |   \\\n    |   |___| |____     |____ | \\  | |    \\\n    |   |   | |         |     |  \\ | |    /\n    |   |   | |____     |____ |   \\| |___/");
    Sleep (1000);
    system ("cls");
	SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 15);
    system ("mode con cols=60 lines=5");
	return 0;
}
原文