控制台游戏4-打地鼠

来自Rexdisn表述的打地鼠游戏……
被自己吐槽像金山打字通……
其实好像确实差不多qwq……

#include <iostream>
#include <cstdio>
#include <conio.h>
#include <cstdlib>
#include <windows.h>
#include <cstring>
#include <ctime>
using namespace std;

HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);

int hp=3;
struct asdf
{
	int x,y;
}w[27];
int o[27]={0,'q'-'a'+1,'w'-'a'+1,'e'-'a'+1,'r'-'a'+1,'t'-'a'+1,'y'-'a'+1,'u'-'a'+1,'i'-'a'+1,'o'-'a'+1,'p'-'a'+1,
			 'a'-'a'+1,'s'-'a'+1,'d'-'a'+1,'f'-'a'+1,'g'-'a'+1,'h'-'a'+1,'j'-'a'+1,'k'-'a'+1,'l'-'a'+1,
			 'z'-'a'+1,'x'-'a'+1,'c'-'a'+1,'v'-'a'+1,'b'-'a'+1,'n'-'a'+1,'m'-'a'+1};
void gotoxy(HANDLE hOut, int x, int y);
void getxy(HANDLE hOut, int &xx, int &yy);

void setColor(unsigned short ForeColor=7,unsigned short BackGroundColor=0)
{
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);//获取当前窗口句柄
	SetConsoleTextAttribute(handle,ForeColor+BackGroundColor*0x10);//设置颜色
}

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 = false;
	SetConsoleCursorInfo(handle, &CursorInfo); 
}

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

void Exit()
{
 	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?";
	}
}

string aa[5]={"╔═══╗",
	   		  "║      ║",
			  "║      ║",
			  "║      ║",
			  "╚═══╝"};
void Map()
{
	system("CLS");
	int tot=0;
	for (int i=0;i<5;++i,cout<<"\n") 
		for (int j=1;j<=10;++j)
		{
			if (i==2) ++tot,getxy(hOut,w[o[tot]].x,w[o[tot]].y),w[o[tot]].x+=2;
			cout<<aa[i];
		}
	cout<<"  ";
	for (int i=0;i<5;++i,cout<<"\n  ")
		for (int j=1;j<=9;++j)
		{
			if (i==2) ++tot,getxy(hOut,w[o[tot]].x,w[o[tot]].y),w[o[tot]].x+=2;
			cout<<aa[i];
		}
	cout<<"     ";
	for (int i=0;i<5;++i,cout<<"\n       ")
		for (int j=1;j<=7;++j)
		{
			if (i==2) ++tot,getxy(hOut,w[o[tot]].x,w[o[tot]].y),w[o[tot]].x+=2;
			cout<<aa[i];
		}
	for (int i=3;i>=1;--i) gotoxy(hOut,43,15),printf("%d",i),Sleep(1000);
	gotoxy(hOut,43,15),printf("go!"),Sleep(1000);
}

string happy="≧▽≦",sad="〒口〒",bye="OVO ",hit="口-"; 
int t=1,score,qaq,qwqwq=2000,qwq;
int mouse[27];
int rat[27];

void Clean()
{
	t=1,score=0,qaq=0,qwq=qwqwq,hp=3;
	memset(mouse,0,sizeof(mouse));
	memset(rat,0,sizeof(rat));
}

bool gameover()
{
	for (int i=1;i<=26;++i,rat[i-1]=0)
		if (rat[i]==1) hp--,gotoxy(hOut,w[i].x,w[i].y),mouse[i]=50,cout<<bye;
		else if (rat[i]==2) setColor(12,0),gotoxy(hOut,w[i].x,w[i].y),cout<<sad,mouse[i]=50,setColor(7,0);
		else if (rat[i]==3) setColor(12,0),gotoxy(hOut,w[i].x,w[i].y),cout<<bye,mouse[i]=50,setColor(7,0);
	if (hp<=0) return 1;
	return 0; 
}

void GG()
{
	gotoxy(hOut,0,15);
	printf("                                        Gameover!                                        ");
	gotoxy(hOut,37,16);
	printf("您的最终得分是:%d",score);
	getch();
}

void newgame()
{
	Clean();
	Map();
	while (1)
	{
		if (gameover())
		{
			GG();
			break;
		}
		t=min(10,1+score/20);
		qaq=qwq-t*10;
		if (t==100) qwq--;
		if (hp==0) break;
		for (int i=1;i<=26;++i)
			if (rand()%100==rand()%100&&mouse[i]==0) rat[i]=2;
			else if (rand()%250==rand()%250&&mouse[i]==0) rat[i]=3;
			else if (rand()%26<t&&mouse[i]==0) rat[i]=1;
		for (int i=1;i<=26;++i)
			if (rat[i]==1) gotoxy(hOut,w[i].x,w[i].y),cout<<happy;
			else if (rat[i]==2) setColor(12,0),gotoxy(hOut,w[i].x,w[i].y),cout<<happy,setColor(7,0);
			else if (rat[i]==3) setColor(9,0),gotoxy(hOut,w[i].x,w[i].y),cout<<happy,setColor(7,0);
		for (int i=1;i<=qaq;++i)
		{
			for (int j=1;j<=26;++j)
				if (mouse[j])
				{
					mouse[j]--;
					if (mouse[j]==0) gotoxy(hOut,w[j].x,w[j].y-1),printf("      "),gotoxy(hOut,w[j].x,w[j].y),printf("      ");
				}
			Sleep(1);
			if (kbhit())
			{
				char x=getch();
				if (x>'z'||x<'a') continue;
				if (rat[x-'a'+1])
				{
					mouse[x-'a'+1]=300;
					if (rat[x-'a'+1]==1) score++;
					if (rat[x-'a'+1]==2) hp--,setColor(12,0);
					if (rat[x-'a'+1]==3) hp++,setColor(9,0);
					rat[x-'a'+1]=0;
					gotoxy(hOut,w[x-'a'+1].x+2,w[x-'a'+1].y-1);
					cout<<hit;
					Sleep(25),qaq-=25;
					gotoxy(hOut,w[x-'a'+1].x,w[x-'a'+1].y);
					cout<<sad;
					setColor(7,0);
					if (hp==0)
					{
						GG();
						break;
					}
				}
				gotoxy(hOut,0,15);
				for (int i=0;i<102;++i) printf(" ");
				gotoxy(hOut,40-(hp-3)*3,15);
				for (int i=1;i<=hp;++i) printf("生命 ");
			}
		}
	}
}

void fengmian()
{
	string ss="打地鼠\n输入\"Start\"以开始游戏。\n输入\"Exit\"以结束游戏。\n输入\"Rule\"以查看规则。\n输入\"Change\"以改变游戏难度。\n";
	string rule="游戏规则\n随机出现白鼠和少量的红鼠,\n按下\"a\"~\"z\"键对相应位置的地鼠进行打击。\n击中一只白鼠加1分,\n击中一只红鼠减1生命,\n击中一只蓝鼠增加1生命。\n漏击一只白鼠减少1生命,\n生命减少至0游戏结束。\n";
	int qaq=50;
	string s;
	while (1)
	{
		system("CLS");
		for (int i=0;ss[i];i++)
		{
			cout<<ss[i];
			if (kbhit()) getch(),qaq=0;
			Sleep(qaq);
		}
		cin>>s;
		if (s=="Start") hide(),newgame(),ehide();	
		if (s=="Exit") Exit();
		if (s=="Rule")
		{
			qaq=50;
			system("CLS");
			for (int i=0;rule[i];i++)
			{
				cout<<rule[i];
				if (kbhit()) getch(),qaq=0;
				Sleep(qaq);
			}
			getch();
		}
		if (s=="Change")
		{
			printf("请输入每轮地鼠出现的间隔(ms)。\n");
			cin>>qwqwq;
		}
	} 
}

int main()
{
	system("mode con cols=101 lines=30");
    srand(time(NULL));
	fengmian();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值