控制台游戏5-贪吃蛇

c语言结课作业要写个控制台大程序,
xxx管理系统or小游戏。
然后给无数同学的贪吃蛇代码Debug……
上网扒代码为什么不参考我的博客呢x
(原来是忘了发贪吃蛇x

#include <iostream>
#include <cstdio>
#include <conio.h>
#include <cstdlib>
#include <windows.h>
#include <cstring>
#include <ctime>
using namespace std;
int a[22][22],food;
string guize="贪吃蛇\n0、当蛇增长至填满棋盘,游戏胜利。\n1、\"w\",\"s\",\"a\",\"d\"控制蛇的运动方向。\n2、棋盘中散落着一些食物,收集食物可得到一定的积分,并增加蛇长。\n3、当蛇头撞上墙或自身,游戏结束。";
int xx,yy,snake[401][2]={0,0,10,9,10,10,10,11},qaq,tot=3;
char t='a';
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
void gotoxy(HANDLE hOut, int x, int y);
void getxy(HANDLE hOut, int &xx, int &yy);

void gotoxy(HANDLE hOut, int x, int y)
{
    COORD pos;
    pos.X=x;
    pos.Y=y;
    SetConsoleCursorPosition(hOut,pos);
}

void getxy(HANDLE hOut,int &xx,int &yy)
{
    CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
    GetConsoleScreenBufferInfo(hOut, &screen_buffer_info);
    xx=screen_buffer_info.dwCursorPosition.X;
    yy=screen_buffer_info.dwCursorPosition.Y;
}

void hide()
{
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);  
	CONSOLE_CURSOR_INFO CursorInfo;  
	GetConsoleCursorInfo(handle, &CursorInfo);
	CursorInfo.bVisible=!CursorInfo.bVisible;
	SetConsoleCursorInfo(handle, &CursorInfo); 
}

void print();

void Exit()
{
	system("CLS");
	hide();
 	cout<<"What a pity! Are you sure?(Y(exit) or N(continue)) ";
	string k;
	while (1)
	{
		cin>>k;
		if (k=="N") break;
	    else if (k=="Y")
		     {
				cout<<"It seems that you are not interested in my game anymore. \nThen, you dog, why don't you like my game. \nDon't let me see you!";
			    system("shutdown -s");
			 } 
			 else cout<<"Why don't obey my order? Are you a fool?";
	}
	print();
	hide();
}

void rule()
{
	system("CLS");
	for (int i=0;i<guize.size();++i) cout<<guize[i],Sleep(50);
	getch();
}

void make()
{
	memset(a,0,sizeof(a));
	memset(snake,0,sizeof(snake));
	snake[1][0]=snake[2][0]=snake[2][1]=snake[3][0]=10;
	snake[1][1]=9,snake[3][1]=11;
	qaq=20,tot=3;
	food=0;
	t='a';
	for (int i=0;i<22;++i) a[0][i]=a[i][0]=a[21][i]=a[i][21]=1;
	a[10][10]=a[10][9]=a[10][11]=1;
}

void print()
{
	system("CLS");
	for (int i=0;i<=21;++i)
	{
		for (int j=0;j<=21;++j) 
			if (a[i][j]) printf("■");
			else printf("  ");
		printf("\n");
	}
	getxy(hOut,xx,yy);
}

void gameover()
{
	gotoxy(hOut,xx,yy);
	printf("Gameover!\n你的最终得分是%d",tot);
	return;
}

char x;

void win()
{
	gotoxy(hOut,xx,yy);
	printf("You win!\n你的最终得分是%d",tot);
	return;
}

bool move()
{
	if (t=='a') snake[0][0]=snake[1][0],snake[0][1]=snake[1][1]-1;
	if (t=='d') snake[0][0]=snake[1][0],snake[0][1]=snake[1][1]+1;
	if (t=='w') snake[0][1]=snake[1][1],snake[0][0]=snake[1][0]-1;
	if (t=='s') snake[0][1]=snake[1][1],snake[0][0]=snake[1][0]+1;
	if (a[snake[0][0]][snake[0][1]]==1)
	{
		gameover();
		return 1;
	}
	if (a[snake[0][0]][snake[0][1]]==2147483647) tot++,food=0;
	else
	{
		a[snake[tot][0]][snake[tot][1]]=0;
		gotoxy(hOut,snake[tot][1]*2,snake[tot][0]);
		printf("  ");
	}
	for (int i=tot;i>=1;--i) snake[i][0]=snake[i-1][0],snake[i][1]=snake[i-1][1];
	a[snake[1][0]][snake[1][1]]=1;
	gotoxy(hOut,snake[1][1]*2,snake[1][0]);
	printf("■");
	if (tot==400)
	{
	    win();
	    return 1;
	}
	return 0;
}

void newgame()
{
	print();
	while (1)
	{
		if (!food)
		{
			int u=rand()%20+1,v=rand()%20+1;
			while (a[u][v]!=0) u=rand()%20+1,v=rand()%20+1;
			a[u][v]=2147483647;
			gotoxy(hOut,v*2,u);
			printf("★");
			food=1;
		}
		int j=1;
		Sleep(100);
		for (int i=1;i<=10;++i)
		{
			if (kbhit())
			{
				x=getch();
				if (x=='a'||x=='s'||x=='d'||x=='w') 
				{
					t=x;
					if (move()) return;
					j=0;
				}
			}
		}
		if (j&&move()) return;
	}
}

void fengmian()
{
	string ss="贪吃蛇\n输入\"Start\"以开始游戏。\n输入\"Rule\"以查看规则。\n输入\"Exit\"以结束游戏。\n"; 
	system("CLS");
	for (int i=0;i<ss.size();++i) cout<<ss[i],Sleep(50);
	string s;
	while (1)
	{
		make();
		cin>>s;
		if (s=="Start") {hide();newgame();hide();break;}
		if (s=="Rule") {rule();break;}
		if (s=="Exit") Exit();
	}
	getch();
}

int main()
{
	srand(time(0));
	make();
	while (1) fengmian();
	return 0;
}
···
上一篇:2021-02-24


下一篇:我罗斯方块最终篇