【c++】打地鼠

用c++做的一个简单的打地鼠游戏。

不需要键盘操作,只需要鼠标就行了。(使用时程序要开全屏)

#include<windows.h>
#include<iostream>
#include<cstdlib>
#include<ctime>
#pragma GCC optimize(2)
using namespace std;
const int X=8,Y=17,SX=1,SY=20;//如果版本比较新,又无法正确甄别鼠标,试试SX=5,SY=40
const int zk=3;const int N=100;
int gk=1;
int wide[zk+1] {0,3,4,5},heigh[zk+1] {0,3,3,4},Time[zk+1] {0,50,45,40},zd[zk+1] {0,0,1,1},pro[zk+1][5]{{0},{0,40,40,10,10},{0,20,30,30,20},{0,10,30,35,25}};
int lie[N][N],mice[N][N];int hope[zk+1] {0,50,75,90}; int bpro[zk+1][4]{{0},{0},{30,50,10,10},{20,30,30,20}};
int point,times,cnt,tcnt;int bomb[N][N];
string gs[zk+1]{"","第一关","第二关","第三关"};
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
#define close HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);	DWORD mode;GetConsoleMode(hStdin, &mode);mode &= ~ENABLE_QUICK_EDIT_MODE;SetConsoleMode(hStdin, mode)
void window();
void hide(){
	HANDLE h_GAME =GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cursor_info;
	GetConsoleCursorInfo(h_GAME,&cursor_info);
	cursor_info.bVisible=false;
	SetConsoleCursorInfo(h_GAME,&cursor_info);
}
int random(int x){
	return rand()%x+1;
}
int random2(int bg){
	int x=random(100);int cnt=0;
	for(int i=1;i<=4;i++){
		cnt+=pro[bg][i];if(cnt>=x)return i;
	} return 1;
}
int random3(int bg){
	int x=random(100);int cnt=0;
	for(int i=0;i<=3;i++){
		cnt+=bpro[bg][i];if(cnt>=x)return i;
	} return 0;
}
void gotoxy(int x, int y){
	HANDLE hout;COORD coord={x,y}; 
	hout=GetStdHandle(STD_OUTPUT_HANDLE);  
	SetConsoleCursorPosition(hout,coord);  
}
void color(int ForgC, int BackC) {
	WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
bool ok(POINT p,int sx,int sy,int l,int h){
	sx--,sy--;
	return p.x>=SX+sx*X&&p.x<=SX+sx*X+l*X&&p.y>=SY+sy*Y&&p.y<=SY+sy*Y+h*Y;
}
void say(string s,int SLeepTime){
	int n=s.length();for(int i=0;i<n;i+=2)Sleep(SLeepTime),printf("%c%c",s[i],s[i+1]);return;
}
void qsay(string s){
	cout<<s;Sleep(500);cout<<endl;
}
void go(int bg){
	hide();
	point=0,times=60,cnt=0,tcnt=0;
	system("cls");
	printf("本关目标:"); 
	Sleep(500);
	for(int i=1;i<=50;i++){
		printf("%d分",random(150));
		Sleep(50);gotoxy(9,0);
		printf("      ");gotoxy(9,0);
	}
	printf("%d分",hope[bg]);
	Sleep(500);
	qsay("\n好");qsay("准备");qsay("开始!");
	system("cls");
	for(int i=3;i>0;i--)
		printf("\b%d",i),Sleep(1000);
	system("cls");
	while(times){
		gotoxy(0,0);color(7,0);
		printf("分数:%d 时间:  \b\b%d\n",point,times);
		for(int i=1;i<=wide[bg];i++){
			for(int j=1;j<=heigh[bg];j++){
				if(mice[i][j])color(6,0),printf("地鼠 ");
				else if(bomb[i][j])color(8,0),printf("炸弹 ");
				else if(lie[i][j]<0)color(4,0),printf("-5分 ");
				else if(lie[i][j]>0)color(14,0),printf("+1分 ");
				else color(7,0),printf("空洞 ");
			}
			printf("\n");
		}
		Sleep(20);
		tcnt++;cnt++;
		if(tcnt>=30)
			times--,tcnt=tcnt-30;
		if(cnt>=Time[bg]){
			for(int i=1;i<=wide[bg];i++)
				for(int j=1;j<=heigh[bg];j++)
					mice[i][j]=0,bomb[i][j]=0;
			int x,y,n=random2(bg);
			while(n--){
				do{
					x=random(wide[bg]);
					y=random(heigh[bg]);
				}while(lie[x][y]!=0||mice[x][y]||bomb[x][y]);
				mice[x][y]=1;
			}n=random3(bg);
			while(n--){
				do{
					x=random(wide[bg]);
					y=random(heigh[bg]);
				}while(lie[x][y]!=0||mice[x][y]||bomb[x][y]);
				bomb[x][y]=1;
			}cnt=0;
		}
		POINT p;
		GetCursorPos(&p);
		for(int i=1;i<=wide[bg];i++){
			for(int j=1;j<=heigh[bg];j++){
				if(ok(p,(j-1)*5+1,i+1,4,1)&&KEY_DOWN(0x01)){
					if(mice[i][j])mice[i][j]=0,lie[i][j]=16,point++;
					if(bomb[i][j])bomb[i][j]=0,lie[i][j]=-16,point-=5;
				}
				if(lie[i][j]>0)lie[i][j]--;
				else if(lie[i][j]<0)lie[i][j]++;
			}
		}
	}
	color(7,0);
	system("cls");
	printf("目标分数是:");
	Sleep(500);printf("%d分",hope[bg]);
	Sleep(500);printf("\n你的分数是:");
	Sleep(500);printf("%d分",point);
	Sleep(500);
	if(hope[bg]<=point){
		printf("\n你赢了!");
		gk++; 
	}
	else printf("\n你输了!");
	Sleep(1000);system("cls");
	window();
	return;
}
void start(){
	hide();
	color(14,0);
	printf("   ");
	say("打地鼠\n",500);
	int k=0;
	while(true){
		k=0;
		printf("\b\b\b\b\b\b\b\b\b\b\b");
		POINT p;
		GetCursorPos(&p);
		if(ok(p,3,2,8,1)){
			color(7,0);
			printf("  千万别点");
			while(ok(p,3,2,8,1)){
				GetCursorPos(&p);
				if(KEY_DOWN(0x01)){
					color(4,0),
					printf("\b\b\b\b\b\b\b\b\b\b\b  不!别点");
					Sleep(500);
					while(ok(p,3,2,8,1)){
						GetCursorPos(&p);
						if(KEY_DOWN(0x01)){
							system("cls");
							return;
						}
					}	
				}
			}
		}
		else{
			color(2,0);
			printf("  开始游戏");
		}
		Sleep(0);
	}
}
void window(){
	hide();
	int GGG=0;
	while(true){
		gotoxy(0,0);
		for(int i=1;i<=zk;i++){
			if(i<=gk)color(2,0);
			else color(8,0);
			cout<<gs[i]<<endl;
		}
		for(int i=1;i<=zk;i++){
			POINT p;
			GetCursorPos(&p);
			if(ok(p,1,i,6,1)){
				gotoxy(0,i-1);
				if(i<=gk)color(2,14);
				else color(8,0);
				cout<<gs[i];
				if(KEY_DOWN(0x01)&&i<=gk){
					GGG=i;break;
				}
			}
		}
		if(GGG)break;
		Sleep(20);
	}
	color(7,0);
	go(GGG);
	return;
}
int main(){
	close;hide();
	srand(time(0));
	start();window();
	return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值