C++小游戏

前言

水一篇,洛谷欢迎加团

https://www.luogu.com.cn/team/79951

完整代码

#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
void S(int ForgC, int BackC) {
	WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
void G(int x,int y){
	COORD pos={x,y};
	HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut,pos);
	return ;
}
struct ren{
	int x,y,HP,G;
	bool fx;
}A,B;
bool f,f2,F,F2,kx,kx2,JN,JN2;
int a[21][31];
double j,j2,cs,cs2,JS,JS2,pg=0.3,pg2=0.3,PG=0.3,PG2=0.3;
void fzsc(){
	for(int i=1;i<=20;i++){
		for(int j=1;j<=30;j++){
			a[i][j]=0;
			if(j==1||j==30)a[i][j]=4;
			if(i==20)a[i][j]=3;
			if(i==A.x&&j==A.y)a[i][j]=1;
			if(i==B.x&&j==B.y)a[i][j]=2;
			if(f){
				if(A.fx){
					a[A.x-1][A.y+1]=5;
					a[A.x][A.y+2]=7;
					f=true;
				}else{
					a[A.x-1][A.y-1]=6;
					a[A.x][A.y-2]=7;
					f=true;
				}
			}
			if(f2){
				if(B.fx){
					a[B.x-1][B.y+1]=8;
					a[B.x][B.y+2]=10;
					f2=true;
				}else{
					a[B.x-1][B.y-1]=9;
					a[B.x][B.y-2]=10;
					f2=true;
				}
			}
			if(F){
				if(A.fx){
					a[A.x][A.y-1]=11;
					a[A.x][A.y+1]=11;
					a[A.x][A.y+2]=11;
					a[A.x][A.y+3]=11;
					F=true;
				}else{
					a[A.x][A.y-3]=11;
					a[A.x][A.y-1]=11;
					a[A.x][A.y-2]=11;
					a[A.x][A.y+1]=11;
					F=true;
				}
			}
			if(F2){
				if(B.fx){
					a[B.x][B.y-1]=12;
					a[B.x][B.y+1]=12;
					a[B.x][B.y+2]=12;
					a[B.x][B.y+3]=12;
					F2=true;
				}else{
					a[B.x][B.y-3]=12;
					a[B.x][B.y-1]=12;
					a[B.x][B.y-2]=12;
					a[B.x][B.y+1]=12;
					F2=true;
				}
			}
			if(JN){
				if(i==A.x&&!(i==A.x&&j==A.y)&&i<20&&j>1&&j<29){
					a[i][j]=11;
				}
				if(j==A.y&&!(i==A.x&&j==A.y)&&i<20&&j>1&&j<29){
					a[i][j]=7;
				}
			}
			if(JN2){
				if(i==B.x&&!(i==B.x&&j==B.y)&&i<20&&j>1&&j<29){
					a[i][j]=12;
				}
				if(j==B.y&&!(i==B.x&&j==B.y)&&i<20&&j>1&&j<29){
					a[i][j]=10;
				}
			}
			if(A.fx){
				a[A.x][A.y+1]=14;
			}else{
				a[A.x][A.y-1]=13;
			}
			if(B.fx){
				a[B.x][B.y+1]=16;
			}else{
				a[B.x][B.y-1]=15;
			}
			if(a[i][j]==1||a[i][j]==5||a[i][j]==6||a[i][j]==7||a[i][j]==11||a[i][j]==13||a[i][j]==14)S(12,0);
			else if(a[i][j]==2||a[i][j]==8||a[i][j]==9||a[i][j]==10||a[i][j]==12||a[i][j]==15||a[i][j]==16)S(9,0);
			if(a[i][j]==0)cout<<" ";
			if(a[i][j]==1)cout<<"A";
			if(a[i][j]==2)cout<<"B";
			if(a[i][j]==3)cout<<"-";
			if(a[i][j]==4)cout<<"|";
			if(a[i][j]==5)cout<<"\\";
			if(a[i][j]==6)cout<<"/";
			if(a[i][j]==7)cout<<"|";
			if(a[i][j]==8)cout<<"\\";
			if(a[i][j]==9)cout<<"/";
			if(a[i][j]==10)cout<<"|";
			if(a[i][j]==11)cout<<"-";
			if(a[i][j]==12)cout<<"-";
			if(a[i][j]==13)cout<<"<";
			if(a[i][j]==14)cout<<">";
			if(a[i][j]==15)cout<<"<";
			if(a[i][j]==16)cout<<">";
			S(15,0); 
		}
		cout<<endl;
	}
	if(kx==true)S(12,0);
	cout<<"A:"<<A.HP;
	if(A.HP<100)cout<<"\b ";
	cout<<endl;
	printf("技能:%.1lf 秒\n",j);
	S(15,0);
	if(kx2==true)S(12,0);
	cout<<"B:"<<B.HP;
	if(B.HP<100)cout<<"\b ";
	cout<<endl;
	printf("技能:%.1lf 秒\n",j2);
	S(12,0);
	cout<<"A";
	for(int i=1;i<=cs;i++){
		S(12,0);
		cout<<"×";
	}
	for(int i=cs+1;i<=3;i++){
		S(15,0);
		cout<<"○";
	}
	cout<<endl;
	S(9,0);
	cout<<"B";
	for(int i=1;i<=cs2;i++){
		S(12,0);
		cout<<"×";
	}
	for(int i=cs2+1;i<=3;i++){
		S(15,0);
		cout<<"○";
	}
	cout<<endl<<"    "<<JS<<":"<<JS2;
	S(15,0);
}
void js(){
	int n;
	system("cls");
	cout<<"双人对决,初始血量为300,攻击力为20"<<endl;
	cout<<"有三种攻击方法:重击,转身刀和十字激光[技能,冷却5.0 S](伤害均为20)"<<endl;
	S(12,0);
	cout<<"玩家一(A)"<<endl;
	cout<<"W 跳 A D左右移动 J 重击 K 转身刀 [空格] 十字激光"<<endl;
	S(9,0);
	cout<<"玩家二(B)"<<endl;
	cout<<"↑ 跳 ← →左右移动 小键盘0 重击 小键盘1 转身刀 小键盘2 十字激光"<<endl;
	S(15,0);
	cout<<"输入0返回";
	cin>>n;
	if(n==0)return;
	else return js();
}
int kt(){
	system("cls");
	printf("\n\n  --------------------\n");
	printf(" |     1.开始游戏     |\n");
	printf(" |--------------------|\n");
	printf(" |     2.游戏介绍     |\n");
	printf(" |--------------------|\n");
	printf(" |     3.退出游戏     |\n");
	printf("  -------------------- \n");
	int n;
	cin>>n;
	if(n==1){
		system("cls");
		cout<<"那就开始吧!";
		Sleep(1000);
		return 1; 
	}else if(n==2){
		js();
		return kt();
	}else{
		return 3;
	}
}
int main(){
	system("mode con cols=50 lines=40");
	while(true){
		G(0,0);
		if(kt()==3){
			system("cls");
			cout<<"\n  ##########\n";
			cout<<"  #退出成功#\n";
			cout<<"  ##########\n";
			Sleep(3000);
			return 0;
		}
		JS=0;
		JS2=0;
		while(JS<4&&JS2<4){
			A.x=3;A.y=5;A.HP=300;A.G=20;A.fx=true;
			B.x=3;B.y=26;B.HP=300;B.G=20;B.fx=false;
			j=5;
			j2=5;
			cs=0;
			cs2=0;
			while(cs<3&&cs2<3){
				while(A.HP>0&&B.HP>0){
					fzsc();
					JN=false;
					JN2=false;
					f=false;
			        f2=false;
			        F=false;
			        F2=false;
			        kx=false;
			        kx2=false;
					if(GetAsyncKeyState('A')){
						if(A.y>2)A.y--;
						A.fx=false;
					}
					if(GetAsyncKeyState('D')){
						if(A.y<29)A.y++;
						A.fx=true;
					}
					if(GetAsyncKeyState('W')){
						if(A.x>2)A.x-=3;
					}
					if(GetAsyncKeyState('J')&&pg==0){
						f=true;
						pg=0.3;
						if(A.fx){
							if(B.x>=A.x-1&&B.y<=A.y+2&&B.x<=A.x+1&&B.y>=A.y){
								B.HP-=A.G;
								kx2=true;
							}
						}else{
							if(B.x>=A.x-1&&B.y>=A.y-2&&B.x<=A.x+1&&B.y<=A.y){
								B.HP-=A.G;
								kx2=true;
							}
						}
					}
					if(GetAsyncKeyState('K')&&pg2==0){
						F=true;
						pg2=0.3;
						if(A.fx){
							A.y-=1;
							if(B.x==A.x&&B.y>=A.y-3&&B.y<=A.y+1){
								B.HP-=A.G;
								kx2=true;
							}
						}else{
							A.y+=1;
							if(B.x==A.x&&B.y>=A.y-1&&B.y<=A.y+3){
								B.HP-=A.G;
								kx2=true;
							}
						}
						A.fx=!A.fx;
					}
					if(GetAsyncKeyState(VK_SPACE)&&j==0){
						JN=true;
						if(A.y==B.y||A.x==B.x){
							B.HP-=A.G;
							kx2=true;
						}
						j=5;
					}
					if(GetAsyncKeyState(VK_LEFT)){
						if(B.y>2)B.y--;
						B.fx=false;
					} 
					if(GetAsyncKeyState(VK_UP)){
						if(B.x>2)B.x-=3;
					}
					if(GetAsyncKeyState(VK_RIGHT)){
						if(B.y<29)B.y++;
						B.fx=true;
					}
					if(GetAsyncKeyState(VK_NUMPAD0)&&PG==0){
						f2=true;
						PG=0.3;
						if(B.fx){
							if(A.x>=B.x-1&&A.y<=B.y+2&&A.x<=B.x+1&&A.y>=B.y){
								A.HP-=B.G;
								kx=true;
							}
						}else{
							if(A.x>=B.x-1&&A.y>=B.y-2&&A.x<=B.x+1&&A.y<=B.y){
								A.HP-=B.G;
								kx=true;
							}
						}
					}
					if(GetAsyncKeyState(VK_NUMPAD1)&&PG2==0){
						F2=true;
						PG2=0.3;
						if(B.fx){
							B.y-=1;
							if(A.x==B.x&&A.y>=B.y-3&&A.y<=B.y+1){
								A.HP-=B.G;
								kx=true;
							}
						}else{
							B.y+=1;
							if(A.x==B.x&&A.y>=B.y-1&&A.y<=B.y+3){
								A.HP-=B.G;
								kx=true;
							}
						}
						B.fx=!B.fx;
					}
					if(GetAsyncKeyState(VK_NUMPAD2)&&j2==0){
						JN2=true;
						if(B.y==A.y||B.x==A.x){
							A.HP-=B.G;
							kx=true;
						}
						j2=5;
					}
					if(A.x<19)A.x++;
					if(B.x<19)B.x++;
					Sleep(17);
					if(j>0.1)j-=0.1;
					else j=0;
					if(j2>0.1)j2-=0.1;
					else j2=0;
					if(pg>0.1)pg-=0.1;
					else pg=0;
					if(pg2>0.1)pg2-=0.1;
					else pg2=0;
					if(PG>0.1)PG-=0.1;
					else PG=0;
					if(PG2>0.1)PG2-=0.1;
					else PG2=0;
			        G(0,0);
				}
				G(0,0);
				fzsc();
				Sleep(1000);
				G(0,0);
				if(A.HP<=0){
					A.x=3;A.y=5;A.HP=300;A.G=20;A.fx=true;
					cs++;
				}
				if(B.HP<=0){
					B.x=3;B.y=26;B.HP=300;B.G=20;B.fx=false;
					cs2++;
				}
			}
			system("cls");
			S(15,0);
			cout<<"本轮获胜者"<<endl;
			Sleep(500);
			if(cs==3){
				S(9,0);
				cout<<"     B"<<endl;
				JS2++;
			}else{
				S(12,0);
				cout<<"     A"<<endl;
				JS++;
			}
			Sleep(1500);
			S(15,0);
			G(0,0);
		}
		system("cls");
		cout<<"最终获胜者是...";
		Sleep(1145);
		if(JS==4){
			S(12,0);
			cout<<"     A"<<endl;
		}else{
			S(9,0);
			cout<<"     B"<<endl;
		}
		Sleep(2000);
		S(15,0);
		cout<<"按[空格]再来一局"<<endl;
		while(true)if(GetAsyncKeyState(VK_SPACE))break;
	}
	return 0;
}

注:本蒟蒻啥也不会,G函数和S函数参考了别的大佬求赞qwq。

                                                                         qwq

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值