跳到正文

剪贴板

剪贴板 vepwnfmt

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
void clearScreen(){
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coordScreen = { 0, 0 };    // home for the cursor
    SetConsoleCursorPosition( hConsole, coordScreen );
}
string l="@!&#Oo@X&#O%=*$UIY";
string mp[5005],lr;
char yd;bool gamest;
const int waitround=5;
const int width=40;
const int height=200;
const int random_ground=73;
const int random_barrier=25;
const int random_trap=2;
//ground+barrier=100
int xz=height;
int yz=width/2;
int cnt=-waitround+1,rdtick,realt;
void init(){
	for(int i=1;i<=height;i++){mp[i]="";}gamest=0;
	cnt=-waitround+1,rdtick=0;xz=height;yz=width/2;lr="";
}
void stcb(int fc,int bc){
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),(((bc&0x0F)<<4)+(fc&0x0F)));
}
bool trap_check(char x){
	if(x!='+' && x!='-'){return 1;}
	if((x=='-'&&(realt&1))||(x=='+'&&((realt&1)^1))){return 0;}
	return 1;
}
void xput(string x){
	int st=0;
	for(auto i:x){
		if((i=='+'&&(realt&1))||(i=='-'&&((realt&1)^1))){if(st!=3)st=3,stcb(4,0);}
		else if(i=='+'||i=='-'){if(st!=8)st=8,stcb(8,0);}
		else{if(st!=15)st=15,stcb(15,0);}
		cout<<(st==3?'+':(st==8?'-':i));
	}
	stcb(15,0);
}
void over(string ya){
	system("cls");stcb(15,0);
	string xd="";for(int i=1;i<=(width-8)/2;i++){xd=xd+" ";}
	string xd2="";for(int i=1;i<=width-8-(width-8)/2;i++){xd2=xd2+" ";}
	cout<<"           "<<"Game Over!  Press any key to restart..."<<endl;
	for(int j=-22;j<=+23;j++){
		cout<<"          |"<<xd;
		if(j==0){cout<<ya;}
		else{cout<<"        ";}
		cout<<xd2<<"|"<<endl;
	}
	getch();
	return;
}
int main(){
	for(;;){
		init();realt=0;cnt=-waitround+1;
		srand((int)time(0));
		for(int i=1;i<=height;i++){
			mp[i]="|";
			for(int j=1;j<=width;j++){
				rdtick=rand()%100;
				if(rdtick<random_ground
					-((width-j+1>=3 && (mp[i-1][j]=='+' && mp[i-1][j-1]!='-'))?114514:0)){
					mp[i]=mp[i]+" ";
				}else if(rdtick<random_ground+random_barrier
					+((width-j+1>=3 && i<height)?0:random_trap)
					-((width-j+1>=3 && (mp[i-1][j]=='+' && mp[i-1][j-1]!='-'))?114514:0)
					){
					mp[i]=mp[i]+""+l[rand()%l.size()];
				}else{
					if(mp[i-1][j]=='+' && mp[i-1][j-1]!='-') mp[i]=mp[i]+"-+-";
					else mp[i]=mp[i]+"+-+";
					j+=2;
				}
			}
			mp[i]=mp[i]+"|";
		}
		mp[xz][yz]='X';
		mp[0]="|";
		for(int j=1;j<=width;j++){mp[0]=mp[0]+" ";lr+="-";}
		mp[0]+="|";
		for(int i=height-24;;i--){
			clearScreen();
			if(xz==0){over("You win!");break;}
			i+=(max(cnt,1)%2);cnt++;i=min(i-22,xz-4)+22;i=max(i,-23);
			if(xz>i+23){over("Failed..");break;}
			printf("           %d/%d                                         \n",height+1-xz,height);
			for(int j=i-22;j<=i+23;j++){
				if(j>0) cout<<setw(5)<<height+1-j;
				else if(j==0) printf("  win");
				else printf("     ");
				if(j==xz){
					cout<<"     ";xput(mp[j].substr(0,yz));
					stcb(6,0);cout<<mp[j][yz];
					stcb(15,0);xput(mp[j].substr(yz+1,width-yz+1));cout<<endl;
				}
				else{cout<<"     ";xput(mp[max(j,0)]);;cout<<endl;}
			}
			cout<<"  die     |";
			stcb(4,0);cout<<lr;
			stcb(15,0);cout<<"|"<<endl;
			for(int t=1;(t<=100 || !gamest);t++){
				if(kbhit()){
					yd=getch();mp[xz][yz]=' ';
					realt++;
					switch(yd){
						case 'W':
						case 'w':
							xz--;break;
						case 'A':
						case 'a':
							yz--;break;
						case 'S':
						case 's':
							xz++;break;
						case 'D':
						case 'd':
							yz++;break;
					}
					break;
				}
				Sleep(10);
			}
			gamest=1;
			if(mp[xz][yz]!=' ' && mp[xz][yz]!='X' && trap_check(mp[xz][yz])){over("Failed..");break;}
			mp[xz][yz]='X';
		}
	}
	return 0;
}