跳到正文

休闲·娱乐

贪Q蛇

paper_约 14 分钟
原文
文章信息与版本

文章信息

发布
2024年7月20日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 14 分钟

版本

  1. v1当前

超好玩的改版贪吃蛇,游戏里有规则,自己看吧。

(头图是用bug造的,仅供参考。)

C++
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
const int N = 25;
bool CW = 0, LOSE;
mt19937 thr(time(0));
int ad[7][7][3], LX = 1, LY = 1, n = 20;
int dx[5] = {0, 0, 1, 0, -1}, dy[5] = {0, 1, 0, -1, 0};
typedef pair<int, int> PII;
PII st, ed;
mt19937 rd(time(0));
int sna[N][N], eg[N][N], lg10[100000];
int U, have1, have2, len, uu, kongge, longer, del;
int fen3, bei = 1, st4, hd = 6, st5;
void HIDE() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);  // 获取控制台输出的句柄
    CONSOLE_CURSOR_INFO cursorInfo;                     // 定义光标信息结构体变量
    GetConsoleCursorInfo(hConsole, &cursorInfo);        // 获取当前光标信息
    cursorInfo.bVisible = FALSE;                        // 将光标设置为不可见
    SetConsoleCursorInfo(hConsole, &cursorInfo);        // 应用新的光标信息到控制台
}
void color(int x) {
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
}
void gotoxy(short x, short y) {
    swap(x, y);
    x *= 2;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { x, y });
}
bool IsKeyPressed(int keyCode) {
    return GetAsyncKeyState(keyCode) & 0x8000;
}
void Move() {
	while (1) {
        char cc = getch();
        if (cc == '\xE0') { // 特殊键的编码
            char key2 = getch(); // 获取特殊键的具体编码
            if (key2 == 72 && (uu != 2 || len == 1)) U = 4;
            if (key2 == 80 && (uu != 4 || len == 1)) U = 2;
            if (key2 == 75 && (uu != 1 || len == 1)) U = 3;
            if (key2 == 77 && (uu != 3 || len == 1)) U = 1;
        }
        else if (cc == ' ') U = 0;
    }
}
void EGG() {
	int op = rd() % 14;
	if (op <= 6) {
		int x, y;
		x = rand() % 18 + 1, y = rand() % 18 + 1;
		while (sna[x][y]) x = rand() % 18 + 1, y = rand() % 18 + 1;
		gotoxy(x, y);eg[x][y] = 1;
		color(7);
		__builtin_printf("●");
	}
	else if (op <= 8) {
		int x, y;
		x = rand() % 18 + 1, y = rand() % 18 + 1;
		while (sna[x][y]) x = rand() % 18 + 1, y = rand() % 18 + 1;
		gotoxy(x, y);eg[x][y] = 2;
		color(2);
		__builtin_printf("●");
	}
	else if (op <= 10) {
		int x, y;
		x = rand() % 18 + 1, y = rand() % 18 + 1;
		while (sna[x][y]) x = rand() % 18 + 1, y = rand() % 18 + 1;
		gotoxy(x, y);eg[x][y] = 3;
		color(5); __builtin_printf("●");
		fen3 = clock();
	}
	else if (op <= 12) {
		int x, y;
		x = rand() % 18 + 1, y = rand() % 18 + 1;
		while (sna[x][y]) x = rand() % 18 + 1, y = rand() % 18 + 1;
		gotoxy(x, y);eg[x][y] = 4;
		color(4); __builtin_printf("●");
	}
	else {
		int x, y;
		x = rand() % 18 + 1, y = rand() % 18 + 1;
		while (sna[x][y]) x = rand() % 18 + 1, y = rand() % 18 + 1;
		gotoxy(x, y);eg[x][y] = 5;
		color(14); __builtin_printf("●");
	}
}
void DO_YIN() {
	for (int i = 1; i <= 18; i ++ )
		for (int j = 1; j <= 18; j ++ ) {
			if (eg[i][j] == 1) gotoxy(i, j), color(7), __builtin_printf("●");
			if (eg[i][j] == 2) gotoxy(i, j), color(2), __builtin_printf("●");
			if (eg[i][j] == 3) gotoxy(i, j), color(5), __builtin_printf("●");
			if (eg[i][j] == 4) gotoxy(i, j), color(4), __builtin_printf("●");
			if (eg[i][j] == 5) gotoxy(i, j), color(14), __builtin_printf("●");
		}
}
void PLAY() {
	system("cls");
	HIDE();
    srand(time(0));
    for (int i = 1; i <= n; i ++ ) {
    	for (int j = 1; j <= n; j ++ ) {
    		color(7);
    		if (i == 1 || j == 1 || i == n || j == n) __builtin_printf("■");
    		else __builtin_printf("□");
		}
		cout << '\n';
	}
	st = {9, 9};
	for (int i = 10; i <= 1000; i ++ ) lg10[i] = lg10[i / 10] + 1;
	deque<PII> q;q.push_back(st);
	thread move(Move);
	move.detach();
	EGG();LOSE = 0;len = 1, longer = 0;int timec = 0;
	gotoxy(20, 0);
	color(7);cout << "得分:" << len - 1;
	while (1) {
		timec ++;
        PII ed = q.front();q.pop_front();
        PII ST;
        uu = U;
		ST = {st.first + dx[uu], st.second + dy[uu]};
	    if (ST.first == 0) ST.first = n - 2, LOSE = CW;if (ST.first == n - 1) ST.first = 1, LOSE = CW;
	    if (ST.second == 0) ST.second = n - 2, LOSE = CW;if (ST.second == n - 1) ST.second = 1, LOSE = CW;
	    sna[ed.first][ed.second] = 0;
	    if (sna[ST.first][ST.second] && uu) LOSE = 1;
	    if (LOSE) {
	    	gotoxy(20, 0);
	    	cout << "                                          ";
	    	gotoxy(20, 0);
	    	color(6);cout << "You Are Lose.";
	    	exit(1);
		}
		gotoxy(st.first, st.second);
        color(6);__builtin_printf("○");
        gotoxy(ed.first, ed.second);
        color(7);__builtin_printf("□");
		gotoxy(ST.first, ST.second);
        color(hd);__builtin_printf("●");
        sna[ST.first][ST.second] = 1;
        st = ST;
        if (eg[st.first][st.second]) {
        	if (eg[st.first][st.second] == 1) longer += (rd() % 3 == 0);
        	if (eg[st.first][st.second] == 2) {
        		longer += 29;
        		del += 29;
			}
			if (eg[st.first][st.second] == 3) {
				len += max(0, 8 - int((clock() - fen3) / CLOCKS_PER_SEC) * 2);
				fen3 = 0;
			}
			if (eg[st.first][st.second] == 4) {
				bei = 2;st4 = len;
			}
			if (eg[st.first][st.second] == 5) {
				hd = 14, st5 = len;CW = 1;
			}
        	eg[st.first][st.second] = 0;
	        longer ++;
	        len ++;
       		EGG();
			gotoxy(20, 0);
	    	cout << "                                          ";
	    	gotoxy(20, 0);
	    	color(7);cout << "得分:" << len - 1;
			if (lg10[len - 1] % 2 == 0) cout << ' ';
		}
		if (len - 1 >= 100) {
			cout << "                                          ";
	    	gotoxy(20, 0);
	    	color(6);cout << "You Win!!!" << len - 1;
	    	exit(1);
		}
		if (longer) {
			longer --;
			eg[st.first][st.second] = 0;
	        gotoxy(ed.first, ed.second);
	        sna[ed.first][ed.second] = 1;
	        q.push_front(ed);
		    color(6);__builtin_printf("○");
		}
		if (!longer) {
			for (int j = 1; j <= del; j ++ ) {
				gotoxy(q.front().first, q.front().second);
				sna[q.front().first][q.front().second] = 0;
				color(7);__builtin_printf("□");
				q.pop_front();
			}
			del = 0;
		}
		if (timec % 20 == 0) DO_YIN();
		if (st4 && len - st4 >= 3) {
			st4 = 0;
			bei = 1;
		}
		if (st5 && len - st5 >= 6) {
			st5 = 0;CW = 0;
			hd = 6;
		}
        q.push_back(st);
        Sleep(80 / bei);
    }
}
void RULE() {
	color(7);cout << "\n贪吃蛇游戏,18 * 18 的格子,得分 100 分后获胜。蛇头不能撞到蛇身,一般情况下可以穿墙,上下左右键操控。\n";
	color(7);__builtin_printf("●");cout << " 得分 1 分,有三分之一概率长度加 2,否则长度加 1。\n";
	color(2);__builtin_printf("●");cout << " 得分 1 分,长度加 1,蛇的尾部会静止 30 格,之后缩回正常长度。\n";
	color(5);__builtin_printf("●");cout << " 初始得分 9 分,每过一秒增加分数减 2,到 1 后停止,长度加 1。\n";
	color(4);__builtin_printf("●");cout << " 得分 1 分,长度加 1,移速翻倍,再得 2 分后解除效果。\n";
	color(14);__builtin_printf("●");cout << " 得分 1 分,长度加 1,将蛇头变为亮黄色,再得 5 分后解除效果,这期间蛇不能穿墙。\n";
	color(7);cout << "按任意键继续。";
	char c = getch();
}
int main() {
//	HWND hwnd = GetForegroundWindow();
//	ShowWindow(hwnd, SW_MAXIMIZE);
//  SetWindowPos(hwnd, NULL, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_SHOWWINDOW);
	while (1) {
		system("cls");
		color(6);
		cout << "贪 Q 蛇\n\n1.开始游戏\n\n2.规则说明\n";
		char c = getch();
		if (c == '1') PLAY();
		else RULE();
	}
    return 0;
}

更新日志: 2024.7.20:制作出游戏,修复球消失的bug。

2024.7.22:修复通关相关bug。

原文
存档评论3 条抓取于