贪吃蛇 (C++ 实现 + 复原)

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

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

玩法:

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

源码:

#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, direction = 3, X = 8, Y = 9;
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)
	{
		printf ("\n  方向-direction:");
		if (direction == 1) printf ("左-left ");
		if (direction == 2) printf ("上-Up   ");
		if (direction == 3) printf ("右-right");
		if (direction == 4) printf ("下-down ");
		printf ("\n  长度-len:%03d\n  坐标-coordinate:X:%02d Y:%02d\n", len, X, Y);
		for (int i = 0; i <= 16; i ++)
		{
			printf ("  ");
			for (int j = 0; j <= 17; j ++)
			{
				if ((j == 0 || j == 17) && i != 0) printf ("|");
				else if (i == 0 && j != 0 && j != 17) printf ("__");
				else if (i == 16 && j != 0 && j != 17 && map[i][j] == 0) 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) printf ("○");
			}
			printf ("\n");
		}
		if (flag)
		{
			Sleep (1000);
			break;
		}
		int xX = 18, xY = 18, FX = direction;
		char ch = (int) _getch ();
		if (ch == 65 || ch == 97) FX = 1, xX = X, xY = Y - 1;
		else if (ch == 87 || ch == 119) FX = 2, xX = X - 1, xY = Y;
		else if (ch == 68 || ch == 100) FX = 3, xX = X, xY = Y + 1;
		else if (ch == 83 || ch == 115) FX = 4, xX = X + 1, xY = Y;

		map[18][18] = 0;
		if (map[xX][xY] == 1)
		{
			len ++;
			while (map[gx][gy] != 0) gx = rand () % 16 + 1, gy = rand () % 16 + 1;
			map[gx][gy] = 1;
		}
		if (abs (direction - FX) != 2)
		{
			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;
			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;
		}
		system ("cls");
	}
	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"); 
	printf ("\n  ____                            ___\n  |   \\                          /   \\             |\n  |___/  ___  __|__    _  ___    \\___    __   ___  |     ___     _\n  | \\   /___\\   |   /|/  /   \\       \\ |/  \\ /   | |__/ /___\\ /|/\n  |  \\_ \\___    \\_/  |   \\___/   \\___/ |   | \\___\\ | \\_ \\___   |\n\n  --------------++++++======== 贪吃蛇 ========++++++--------------\n\n  复原:蓝星纪元年(原 Sile 工作室)\n\n  玩法:\n    吃掉地图上生成的食物,使贪吃蛇变长。\n    不要撞到墙壁或贪吃蛇的身体。\n\n  (点击任意键继续)");
	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");
    printf ("\n  _____       _____     _____        ____\n    |   |   | |         |     |\\   | |   \\\n    |   |___| |____     |____ | \\  | |    \\\n    |   |   | |         |     |  \\ | |    /\n    |   |   | |____     |____ |   \\| |___/");
    Sleep (1000);
    system ("cls");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值