其他分享
首页 > 其他分享> > 贪吃蛇(优化++)

贪吃蛇(优化++)

作者:互联网

本人比较菜,只能微调,有bug找我

//本程序属个人劳动成果,如有雷同,纯属巧合(全是手打的,思路也是自己的)
#include<bits/stdc++.h>
#include<windows.h>
#include <conio.h>
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
using namespace std;
inline void gotoxy(int x,int y) {
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(handle,pos);
	return;
}
int mp[1000][1000],len=3,now_len;
string x,y;
int way,nowx,nowy;
int xinx,xiny;
int head;
bool now_has;
char opt,last;
int fx[5]= {0,1,-1,0,0},fy[5]= {0,0,0,-1,1};
void color(int a) {//颜色函数
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
bool check_canmove() {//判断是否有路可走
	rep(i,1,4) {
		if(mp[nowx+fx[i]][nowy+fy[i]]!=1)return true;
	}
	return false;
}
void make_map() { //生成地图
	memset(mp,0,sizeof(mp));
	if(head)x.erase(0,1),y.erase(0,1);;
	int vlen=x.size()-1;
	mp[xinx][xiny]=2;
	vlen=x.size()-1;
	mp[x[0]-'0'][y[0]-'0']=8;
	rep(i,1,vlen-1) {
		int how;
		bool l,r,h,u;
		l=h=r=u=0;
		if(y[i]<y[i-1])l=1;
		else if(y[i]>y[i-1])r=1;
		else if(x[i]>x[i-1])h=1;
		else u=1;
		if(y[i]<y[i+1])l=1;
		else if(y[i]>y[i+1])r=1;
		else if(x[i]>x[i+1])h=1;
		else u=1;
		if(h&&l)how=5;
		else if(l&&u)how=6;
		else if(h&&u)how=4;
		else if(h&&r)how=9;
		else if(u&&r)how=3;
		else if(l&&r)how=7;
		mp[x[i]-'0'][y[i]-'0']=how;
	}
	mp[x[vlen]-'0'][y[vlen]-'0']=1;
	head=0;
}
//1:Θ
//3:╗
//4:║
//5:╚
//6:╔
//7;═
//9:╝
//8:@
void print_map() {//输出地图
	gotoxy(0,0);
	cout<<"by lize yyd 2021.9.2 8:06~9.37\n";
	cout<<"wsad:上下左右\n";
	cout<<"r:重开 空格:暂停\n";
	color(648);
	cout<<"———————————————————————————\n";
	rep(i,1,15) {
		color(648);
		cout<<"—";
		color(7);
		rep(j,1,25) {
			if(mp[i][j]==0)color(51),cout<<"■";
			color(63);
			if(mp[i][j]==1)cout<<"Θ";
			else if(mp[i][j]>2) {
				if(mp[i][j]==3)cout<<"╗ ";
				else if(mp[i][j]==4)cout<<"║ ";
				else if(mp[i][j]==5)cout<<"╚ ";
				else if(mp[i][j]==6)cout<<"╔ ";
				else if(mp[i][j]==7)cout<<"═ ";
				else if(mp[i][j]==8)cout<<"·";
				else cout<<"╝ ";
			}
			color(54);
			if(mp[i][j]==2)cout<<"★";
			color(7);
		}
		color(648);
		cout<<"—\n";
	}
	color(648);
	cout<<"———————————————————————————\n";
	color(7);
	cout<<"现在长度:" <<now_len<<endl<<"最长长度:"<<len<<endl<<"吃掉星星:"<<len-3<<endl;
}
void print_gameover() {//游戏结束
	color(4);
	gotoxy(55,7),printf("   _____          __  __ ______     ______      ________ _____  \n");
	gotoxy(55,8),printf("  / ____|   /\\   |  \\/  |  ____|   / __ \\ \\    / /  ____|  __ \\\n");
	gotoxy(55,9),printf(" | |  __   /  \\  | \\  / | |__     | |  | \\ \\  / /| |__  | |__) |\n");
	gotoxy(55,10),printf(" | | |_ | / /\\ \\ | |\\/| |  __|    | |  | |\\ \\/ / |  __| |  _  / \n");
	gotoxy(55,11),printf(" | |__| |/ ____ \\| |  | | |____   | |__| | \\  /  | |____| | \\ \\ \n");
	gotoxy(55,12),printf("  \\_____/_/    \\_\\_|  |_|______|   \\____/   \\/   |______|_|  \\_\\\n");
	gotoxy(55,14),printf("                         按任意键继续\n");
	color(7);
	while(!kbhit());
	char tmpppppp=_getch();
}
void print_cls() { //清空
	gotoxy(55,7),printf("                                                \n");
	gotoxy(55,8),printf("                                                \n");
	gotoxy(55,9),printf("                                                \n");
	gotoxy(55,10),printf("                                                \n");
	gotoxy(55,11),printf("                                                \n");
	gotoxy(55,12),printf("                                                \n");
}
void print_win() {//你赢了
	color(10);
	gotoxy(55,7),printf(" __     ______  _    _   __          _______ _   _\n");
	gotoxy(55,8),printf(" \\ \\   / / __ \\| |  | |  \\ \\        / /_   _| \\ | |\n");
	gotoxy(55,9),printf("  \\ \\_/ / |  | | |  | |   \\ \\  /\\  / /  | | |  \\| |\n");
	gotoxy(55,10),printf("   \\   /| |  | | |  | |    \\ \\/  \\/ /   | | | . ` |\n");
	gotoxy(55,11),printf("    | | | |__| | |__| |     \\  /\\  /   _| |_| |\\  |\n");
	gotoxy(55,12),printf("    |_|  \\____/ \\____/       \\/  \\/   |_____|_| \\_|\n");
	gotoxy(55,14),printf("                         按任意键继续\n");
	color(7);
	while(!kbhit());
	char tmpppppp=_getch();
}
void print_stop() {//游戏暂停
	color(4);
	gotoxy(55,7),printf("  ____________________________ __________ \n");
	gotoxy(55,8),printf(" /   _____/\\__    ___/\\_____  \\\\______   \\\n");
	gotoxy(55,9),printf(" \\_____  \\   |    |    /   |   \\|     ___/\n");
	gotoxy(55,10),printf(" /        \\  |    |   /    |    \\    |\n");
	gotoxy(55,11),printf("/_______  /  |____|   \\_______  /____|\n");
	gotoxy(55,12),printf("        \\/                    \\/ \n");
	color(7);
}
void init() {
	system("cls");
	printf(
		"                      ________.__          __    __                                     \n"
        "                     /  _____/|  |  __ ___/  |__/  |_  ____   ____   ____  __ __  ______\n"
        "                    /   \\  ___|  | |  |  \\   __\\   __\\/  _ \\ /    \\ /  _ \\|  |  \\/  ___/\n"
        "                    \\    \\_\\  \\  |_|  |  /|  |  |  | (  <_> )   |  (  <_> )  |  /\\___ \\\n"
        "                     \\______  /____/____/ |__|  |__|  \\____/|___|  /\\____/|____//____  >\n"
        "                            \\/                                   \\/                  \\/\n"
        "                                _________              __           \n"
        "                               /   _____/ ____ _____  |  | __ ____  \n"
        "                               \\_____  \\ /    \\\\__  \\ |  |/ // __ \\\n"
        "                               /        \\   |  \\/ __ \\|    <\\  ___/\n"
        "                              /_______  /___|  (____  /__|_ \\\\___  >\n"
        "                                      \\/     \\/     \\/     \\/    \\/ \n"
		"\n\n\n"
		"                                                按任意键开始\n"
	);
	memset(mp,0,sizeof(mp));
	x=y="";
	len=3,now_len=0;
	way=nowx=nowy=0;
	xinx=xiny=0;
	head=0;
	now_has=false;
	opt='\0',last='\0';
	while(!kbhit());
	char tmpppppp=_getch();
	system("cls");
}
int main() {
	//system("stty -icanon");
	chongkai:
	init();
	way=rand()%4+1;
	srand(time(NULL));
	nowx=rand()%10+5,nowy=rand()%10+10;
	x.push_back(nowx),y.push_back(nowy),now_len=1;
	while(check_canmove()) {
		srand(time(NULL));
		if(!now_has) {
			do xinx=rand()%15+1,xiny=rand()%25+1;
			while(mp[xinx][xiny]==1||mp[xinx][xiny]>2);
			mp[xinx][xiny]=2;
			now_has=true;
		}
		if(kbhit())opt=_getch();
		if(opt==' ') {
			print_stop();
			nospace:;
			while(!kbhit());
			opt=_getch();
			if(opt!=' ')goto nospace;
			opt=last;
			print_cls();
		}
		if(opt=='r')goto chongkai;
		if(opt=='w')way=2;
		if(opt=='s')way=1;
		if(opt=='d')way=4;
		if(opt=='a')way=3;
		last=opt;
		nowx+=fx[way],nowy+=fy[way];
		if(mp[nowx][nowy]==2)len++,now_has=false;
		x+=(nowx+'0'),y+=(nowy+'0');
		if(now_len>=len)head++;
		else now_len++;
		if(((nowx!=x[head-1]||nowy!=y[head-1])&&(mp[nowx][nowy]==1||mp[nowx][nowy]>2))||nowx==0||nowy==0||nowx==16||nowy==26)
			{print_gameover();goto chongkai;}
		make_map();
		print_map();
		Sleep(100);
		//可以自己调时间,单位ms
		if(len==15*25){print_win();goto chongkai;}
	}
	return 0;
}

标签:opt,++,int,贪吃蛇,nowx,mp,now,优化,nowy
来源: https://blog.csdn.net/wy749669095/article/details/121407164