跳到正文

个人记录

贪吃蛇

BrandonSoong约 16 分钟
原文
文章信息与版本

文章信息

发布
2019年3月15日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 16 分钟
获赞
5

版本

  1. v1当前

这是个游戏—就是那个贪吃蛇

如需更好玩耍体验,请在编译文件夹新建名字叫“记录”,文件类型为“txt”的记事本!


以下是蒟蒻代码


C++
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
#include<io.h>
using namespace std;
int mapp[30][30];
int seed=3,cal=0;
int x,y,ex,ey,sx,sy,len=2;
string c[10];
int times[6]={0,500,400,250,100,10};
int n=1;
char tou='*' ,sh='-',wei='*' ,bian='X';
int to;//shang 1,xia 2,zuo 3,you 4 
bool ok=0;
int old;
double begin;
struct j{
	int nm;
	string name;
}jilu[10000];
bool cmp(const j &a,const j &b)
{
	return a.nm >b.nm ;
}

inline void add()
{
	for(int i=2;i<=19;i++)
	for(int j=2;j<=19;j++)
	if((i!=sx||j!=sy)&&mapp[i][j]!=2435124)mapp[i][j]++;
	return;
}

inline void turn()
{
	if(to==1)
	if(x!=2)x--;
	else x=19;
	if(to==2)
	if(x!=19)x++;
	else x=2;
	if(to==3)
	if(y!=2)y--;
	else y=19;
	if(to==4)
	if(y!=19)
	y++;
	else y=2;
	return;
}

inline void rturn()
{
	cal--;
	if(to==1)
	x++;
	if(to==2)
	x--;
	if(to==3)
	y++;
	if(to==4)
	y--;
	return;
}

inline void too(int a,int b)
{
		if(mapp[a][b]==2435124)
			printf("A");
		else
			{
			if(mapp[a][b]==123454321)
				printf("o");
			else
				{
    			if(mapp[a][b]==543212345)
					printf("X"); 
				else
					{
						if(mapp[a][b]<0)
						{ 
						if(mapp[a][b]==-len||mapp[a][b]==-1)
							printf("*");
//	else if(mapp[a][b]==-1)
//	printf("*");
						else
							printf("-");
						}
					    else
							printf(" ");
					} 
			   }
			}
	return; 
}

inline void g()
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY);
	return; 
} 

inline void c1()
{
	seed=rand();
	srand(seed);
	if(seed%5==0)
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE | FOREGROUND_RED);
	if(seed%5==1)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN);
		if(seed%5==2)
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE | FOREGROUND_GREEN);
			if(seed%5==3)
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);
				if(seed%5==4)
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN);
	return; 
}

inline void c3()
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN);
	return; 
}

inline void c2()
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY);
	return; 
}

inline void endd()
{
		Sleep(150);
		system("cls");
		printf("*         x");Sleep(200);
		system("cls");
		printf("-*        x");Sleep(200);
		system("cls");
		printf("--*       x");Sleep(200);
		system("cls");
		printf("---*      x");Sleep(200);
		system("cls");
		printf("----*     x");Sleep(200);
		system("cls");
		printf("-----*    x");Sleep(200);
		system("cls");
		printf("------*   x");Sleep(200);
		system("cls");
		printf("-------*  x");Sleep(200);
		system("cls");
		printf("--------* x");Sleep(200);
		system("cls");
		printf("---------*x");Sleep(200);
		c1();
		system("cls");
		printf("-------*^&*");Sleep(200);c1();
		system("cls");
		printf("-----*^#**#");Sleep(200);c1();
		system("cls");
		printf("----*#$^#**");Sleep(200);c1();
		system("cls");
		printf("--_#*^**#^$");Sleep(200);c1();
		system("cls");
		printf("-$^#* &#^@&");Sleep(200);
		system("cls");
		c3();
		printf("----Duang!\n");Sleep(500);
		printf("----Duang!\n\n");Sleep(500);
		printf("----Duang!");Sleep(500);
		system("cls");
		c1();
		printf("--------YOU!\n");
		Sleep(900);
		printf("--------DIE!\n\n\n");
		Sleep(1000);
		c2();
		printf("总得分:%d\n\n",len-3);
		if(len-3>jilu[5].nm )
		{
			jilu[0].nm ++;
			jilu[jilu[0].nm ].nm =len-3;
		c1();
		printf("恭喜!进入世界前五强!请输入名字:\n");
		c2();
		string s;
		cin>>s;
		jilu[jilu[0].nm].name=s;
		
		sort(jilu+1,jilu+jilu[0].nm+1,cmp);
		
		freopen("记录.txt","w",stdout);
		cout<<seed<<endl<<jilu[0].nm<<endl;
		for(int i=1;i<=jilu[0].nm;i++)
		cout<<jilu[i].name<<" "<<jilu[i].nm<<endl;
		freopen("CON","w",stdout);
		dup2(old,1);
		printf("\n");
		
		c3();
		for(int i=1;i<=min(5,jilu[0].nm);i++)
		cout<<i<<"、"<<endl<<"    "<<jilu[i].name<<":"<<endl<<"    "<<jilu[i].nm<<endl<<endl;
		c2();
		}
		qp:
		printf("----TRY AGAIN?------------\n");
		printf("----A:retry---------------\n");
		printf("----B:exit----------------\n");
		printf("----C:查看全部记录--------\n");
		char bh;
		
		bh=getch();
			if(bh=='A')
			{
			system("cls");
             memset(mapp,0,sizeof(mapp)); 
             ok=1;
             return;
			}
			if(bh=='B')
			{
				c1(); 
				printf("----GOOD-BYE!----\n");
				c2();
				Sleep(1000);
				exit(0);
			}
			if(bh=='C') 
			{
				printf("\n");
				c3();
				for(int i=1;i<=jilu[0].nm;i++)
		cout<<i<<"  "<<jilu[i].name<<" "<<jilu[i].nm<<endl;
		c2();

		goto qp;
			}
			else{system("cls");
			goto qp;}
		return;
	
}



inline void print()
{
	cal++;
//	c3() ;
//    printf("   #贪#      #吃#      #蛇#\n");
//	printf("游戏提示:\n w--↑s--↓a--←d--→\n"); c2();
	turn();
	if(mapp[x][y]==-len+1||mapp[x][y]==-len)
	rturn(); 
	else 
	{
	if(mapp[x][y]==543212345||mapp[x][y]==2435124||mapp[x][y]<-1)
	{
	add();
	endd();
	return; 
    }
	else
	{ if(x==sx&&y==sy)
	{
		len++;
		mapp[x][y]=-len;
		do{
		sx=(rand()%18)+2;
	    sy=(rand()%18)+2;
	      }while(mapp[sx][sy]<0||mapp[sx][sy]==2435124||mapp[sx][sy]==543212345);
		  mapp[sx][sy]=123454321;
	}
	else
	{
		mapp[x][y]=-len-1;
		add();
	}} 
	} 
	for(int i=1;i<=20;i++)
	{
		for(int j=1;j<=20;j++)
		{ 
		if(mapp[i][j]<0)
		c1(); 
		if(mapp[i][j]==123454321)
		c1();
		if(mapp[i][j]==2435124)
		c3();
		too(i,j);
		if(mapp[i][j]<0||mapp[i][j]==123454321||mapp[i][j]==2435124)
		c2(); 
		} 
		printf("\n");
	}  
	c3();
	printf("得分:%d",len-3);
	printf("\n用时:%f",(clock()-begin)/1000); 
	c2();
	Sleep(times[n]);
	system("cls");
	return;
}

int main()
{
	old=dup(1);
	fstream file;
	file.open("记录.txt",ios::in);
	char ui=file.get();
	file.close();
	
	if(ui!=EOF)
	{
		freopen("记录.txt","r",stdin);
		scanf("%d%d",&seed,&jilu[0].nm);
		for(int i=1;i<=jilu[0].nm;i++)
		{
			cin>>jilu[i].name;
			scanf("%d",&jilu[i].nm);
		}
		freopen("CON","r",stdin);
		dup2(old,1);
	} 
	

	c[1]="   1、新手档(500  ms/格)    \n";
	c[2]="   2、普通档(400  ms/格)   \n";
	c[3]="   3、进阶档(250  ms/格)   \n";
	c[4]="   4、高手档(100  ms/格)   \n";
	c[5]="   5、超神档(10   ms/格)   \n";
	c[6]="   6、高分榜                \n";
	char k;
	lable:
	{ c3();
	printf("   #贪#      #吃#      #蛇#\n");
	printf("游戏操作: w--↑s--↓a--←d--→\n");
	printf("请选择档位 ( w , s 选择,任意键继续) :\n");
	c2();
	for(int i=1;i<=6;i++)
	{
		if(i==n)
		{
			g();
			cout<<c[i];
			c2(); 
		}
		else
		cout<<c[i];
	}
	k=getch();
	if(k=='w'||k=='W'||k==38)
	{
		if(n!=1)
		n--;
		else
		n=6;
		system("cls");
		goto lable;
	}
	else if(k=='s'||k=='S'||k==40)
	{
		if(n!=6)
		n++;
		else
		n=1;
		system("cls");
		goto lable;
	}
}
	if(n==6)
	{ 
	c3();
	system("cls"); 
	if(jilu[0].nm==0)
    	printf("----暂无记录-----------\n\n");
	else 
	for(int i=1;i<=jilu[0].nm;i++)
	cout<<i<<"、"<<endl<<"    "<<jilu[i].name<<":"<<endl<<"    "<<jilu[i].nm<<endl<<endl;
		c2();
		printf("----任意键:exit--------\n");
		char bh;
		bh=getch();
		system("cls");
			goto lable;
	} 
	for(int i=1;i<=20;i++)
	mapp[1][i]=543212345; 
	for(int i=1;i<=20;i++)
	mapp[i][1]=543212345; 
	for(int i=1;i<=20;i++)
	mapp[20][i]=543212345; 
	for(int i=1;i<=20;i++)
	mapp[i][20]=543212345; 
	
	for(int i=6;i<=14;i++)
	{
	mapp[i][6]=2435124;
    mapp[i][14]=2435124;
    mapp[6][i]=2435124;
    mapp[14][i]=2435124;
	}
	mapp[6][9]=0;
	mapp[6][10]=0;
	mapp[9][6]=0;
	mapp[10][6]=0;
	mapp[14][9]=0;
	mapp[14][10]=0;
	mapp[9][14]=0;
	mapp[10][14]=0;
	x=2,y=4,ex=2,ey=2,to=4,len=3;
	mapp[2][2]=-1;
	mapp[2][3]=-2;
	mapp[2][4]=-3; 
	sx=(rand()%18)+2;
	sy=(rand()%18)+2;
	mapp[sx][sy]=123454321;
	begin=clock();
	while(1)
	{	
	
	while(!kbhit())
	{ 
	seed+=rand()%21412;
	srand(seed);
      print();
      if(ok==1)
      {
      	ok=0;
      	goto lable;
	  }
	}
	k=getch();
	if(k=='w'||k=='W'||k==38)
	if(to!=2)to=1;
	if(k=='a'||k=='A'||k==37)
	if(to!=4)to=3;
	if(k=='s'||k=='S'||k==40)
	if(to!=1)to=2;
	if(k=='d'||k=='D'||k==39)
	if(to!=3)to=4;
	} 
	
    return 0;
}
 
原文
存档评论1 条抓取于