这个代码要在linux环境下运行,过几天在上windows的 ,请大家多多支持,谢谢!好,话不多说,上代码!
#include <termio.h>
#include <unistd.h>
#include <bits/stdc++.h>
using namespace std;
int row,line,answer,row2,line2,point=0;
time_t start=0,finish=0;
bool secret=false;
char a[80][80]=
{
{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
{'*',' ','5','5','5',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','5','5','5',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
{'*',' ','5','5','5',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','5','5','5',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
{'*',' ','5','5','5','6',' ','6',' ','6',' ','6','6',' ',' ','6',' ','6',' ','6','6',' ','6',' ','6',' ','6',' ','6','5','5','5',' ',' ',' ','6','6','6',' ',' ',' ','6','6','6','6','6','6','6',' ',' ',' ','6','6','6',' ',' ','6',' ','*'},
{'*',' ','4','4','4','4',' ','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4',' ','*'},
{'*',' ','3','3','3','3',' ','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3',' ','*'},
{'*','1','3','3','3','3',' ','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','2','*'},
{'*','*','*','*','*','*','l','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{' ',' ',' ',' ',' ','*',' ',' ',' ',' ','s','e','c','r','e','t',' ',' ',' ','c','o','i','n','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ','i','+','p',' ',' ',' ','i','+','i','+','o','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
};
int getch(void)
{
struct termios oldt,newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
void PrintScreen()
{
srand(time(NULL));
if(answer==1)
{
cout<<"\033[2J\033[0m";
cout<<"\033[100A\033[0m";
for(int i=0;i<=15;i++)
{
for(int j=0;j<=70;j++)
{
if(a[i][j]=='1')
{
cout<<"\033[31m*\033[0m";
row=i,line=j;
}
else
if(a[i][j]=='2')
{
cout<<"\033[32m*\033[0m";
row2=i,line2=j;
}
else
if(a[i][j]=='3')
{
int draw=rand()%8;
if(draw==1)
a[i][j]='*',cout<<"*";
else
a[i][j]=' ',cout<<" ";
}
else
if(a[i][j]=='4')
{
a[i][j]=' ',cout<<" ";
}
else
if(a[i][j]=='5')
cout<<"\033[93m*\033[0m";
else
if(a[i][j]=='l')
cout<<'*';
else
if(a[i][j]=='6')
{
a[i][j]=' ',cout<<" ";
}
else
cout<<a[i][j];
}
cout<<endl;
}
cout<<"______________________________________________________________________________________________"<<endl<<"左:a 右:d 下:s 大跳:w 小跳:q 超大跳:e 二段跳:ww 神秘技能1:?+? 神秘技能2:?+?+?"<<endl;
}
if(answer==2)
{
cout<<"\033[2J\033[0m";
cout<<"\033[100A\033[0m";
for(int i=0;i<=15;i++)
{
for(int j=0;j<=70;j++)
{
if(a[i][j]=='1')
{
cout<<"\033[31m*\033[0m";
row=i,line=j;
}
else
if(a[i][j]=='2')
{
cout<<"\033[32m*\033[0m";
row2=i,line2=j;
}
else
if(a[i][j]=='3')
{
int draw=rand()%5;
if(draw==1)
a[i][j]='*',cout<<"*";
else
a[i][j]=' ',cout<<" ";
}
else
if(a[i][j]=='4')
{
a[i][j]=' ',cout<<" ";
}
else
if(a[i][j]=='5')
cout<<"\033[93m*\033[0m";
else
if(a[i][j]=='l')
cout<<'*';
else
if(a[i][j]=='6')
{
a[i][j]=' ',cout<<" ";
}
else
cout<<a[i][j];
}
cout<<endl;
}
cout<<"______________________________________________________________________________________________"<<endl<<"左:a 右:d 下:s 大跳:w 小跳:q 超大跳:e 二段跳:ww 神秘技能1:?+? 神秘技能2:?+?+?"<<endl;
}
if(answer==3)
{
cout<<"\033[2J\033[0m";
cout<<"\033[100A\033[0m";
for(int i=0;i<=15;i++)
{
for(int j=0;j<=70;j++)
{
if(a[i][j]=='1')
{
cout<<"\033[31m*\033[0m";
row=i,line=j;
}
else
if(a[i][j]=='2')
{
cout<<"\033[32m*\033[0m";
row2=i,line2=j;
}
else
if(a[i][j]=='3')
{
int draw=rand()%4;
if(draw==1)
a[i][j]='*',cout<<"*";
else
a[i][j]=' ',cout<<" ";
}
else
if(a[i][j]=='4')
{
int draw2=rand()%8;
if(draw2==1)
a[i][j]='*',cout<<"*";
else
a[i][j]=' ',cout<<" ";
}
else
if(a[i][j]=='5')
cout<<"\033[93m*\033[0m";
else
if(a[i][j]=='l')
cout<<'*';
else
if(a[i][j]=='6')
{
a[i][j]=' ',cout<<" ";
}
else
cout<<a[i][j];
}
cout<<endl;
}
cout<<"______________________________________________________________________________________________"<<endl<<"左:a 右:d 下:s 大跳:w 小跳:q 超大跳:e 二段跳:ww 神秘技能1:?+? 神秘技能2:?+?+?"<<endl;
}
if(answer==4)
{
cout<<"\033[2J\033[0m";
cout<<"\033[100A\033[0m";
for(int i=0;i<=15;i++)
{
for(int j=0;j<=70;j++)
{
if(a[i][j]=='1')
{
cout<<"\033[31m*\033[0m";
row=i,line=j;
}
else
if(a[i][j]=='2')
{
cout<<"\033[32m*\033[0m";
row2=i,line2=j;
}
else
if(a[i][j]=='3')
{
int draw=rand()%3;
if(draw==1)
a[i][j]='*',cout<<"*";
else
a[i][j]=' ',cout<<" ";
}
else
if(a[i][j]=='4')
{
int draw2=rand()%5;
if(draw2==1)
a[i][j]='*',cout<<"*";
else
a[i][j]=' ',cout<<" ";
}
else
if(a[i][j]=='5')
cout<<"\033[93m*\033[0m";
else
if(a[i][j]=='l')
cout<<'*';
else
if(a[i][j]=='6')
{
a[i][j]=' ',cout<<" ";
}
else
cout<<a[i][j];
}
cout<<endl;
}
cout<<"______________________________________________________________________________________________"<<endl<<"左:a 右:d 下:s 大跳:w 小跳:q 超大跳:e 二段跳:ww 神秘技能1:?+? 神秘技能2:?+?+?"<<endl;
}
if(answer==5)
{
cout<<"\033[2J\033[0m";
cout<<"\033[100A\033[0m";
for(int i=0;i<=15;i++)
{
for(int j=0;j<=70;j++)
{
if(a[i][j]=='1')
{
cout<<"\033[31m*\033[0m";
row=i,line=j;
}
else
if(a[i][j]=='2')
{
cout<<"\033[32m*\033[0m";
row2=i,line2=j;
}
else
if(a[i][j]=='3')
{
int draw=rand()%3;
if(draw==1)
a[i][j]='*',cout<<"*";
else
a[i][j]=' ',cout<<" ";
}
else
if(a[i][j]=='4')
{
int draw2=rand()%5;
if(draw2==1)
a[i][j]='*',cout<<"*";
else
a[i][j]=' ',cout<<" ";
}
else
if(a[i][j]=='5')
cout<<"\033[93m*\033[0m";
else
if(a[i][j]=='l')
cout<<'*';
else
if(a[i][j]=='6')
{
int pll=rand()%2;
if(pll==1)
a[i][j]='*',cout<<'*';
else
a[i][j]=' ',cout<<' ';
}
else
cout<<a[i][j];
}
cout<<endl;
}
cout<<"______________________________________________________________________________________________"<<endl<<"左:a 右:d 下:s 大跳:w 小跳:q 超大跳:e 二段跳:ww 神秘技能1:?+? 神秘技能2:?+?+?"<<endl;
}
}
void PlayDrawEasy()
{
cout<<"\033[2J\033[0m";
cout<<"\033[100A\033[0m";
for(int i=0;i<=15;i++)
{
for(int j=0;j<=70;j++)
{
if(a[i][j]=='1')
{
cout<<"\033[31m*\033[0m";
}
else
if(a[i][j]=='2')
{
cout<<"\033[32m*\033[0m";
}
else
if(a[i][j]=='5')
cout<<"\033[93m*\033[0m";
else
if(a[i][j]=='l')
cout<<'*';
else
cout<<a[i][j];
}
cout<<endl;
}
if(secret==true)
{
for(int i=16;i<=18;i++)
{
for(int j=0;j<=70;j++)
if(a[i][j]=='1')
cout<<"\033[31m*\033[0m";
else
cout<<a[i][j];
cout<<endl;
}
if(row<15)
secret=false;
}
cout<<"______________________________________________________________________________________________"<<endl<<"左:a 右:d 下:s 大跳:w 小跳:q 超大跳:e 二段跳:ww 神秘技能1:?+? 神秘技能2:?+?+?"<<endl;
}
void Menu()
{
cin>>answer;
if(answer>5||answer<1)
{
cout<<"非法输入,请重新输入:";
Menu();
}
}
void fall()
{
while(a[row+1][line]==' '||a[row+1][line]=='2'||a[row+1][line]=='5')
{
a[row+1][line]='1';
a[row][line]=' ';
row=row+1;
}
PlayDrawEasy();
}
void Play()
{
start=time(NULL);
while(a[row2][line2]=='2')
{
char k=getch();
if(k=='s')
{
if(a[row+1][line]!='*')
{
if(a[row+1][line]=='l')
{
secret=true;
a[row+2][line]='1';
a[row][line]=' ';
row=row+2;
fall();
}
else
{
a[row+1][line]='1';
a[row][line]=' ';
row=row+1;
fall();
}
}
}
if(k=='a')
{
if(a[row][line-1]!='*')
{
a[row][line-1]='1';
a[row][line]=' ';
line=line-1;
fall();
}
}
if(k=='d')
{
if(a[row][line+1]!='*')
{
a[row][line+1]='1';
a[row][line]=' ';
line=line+1;
fall();
}
}
if(k=='w')
{
if(a[row-2][line]!='*')
{
a[row-2][line]='1';
a[row][line]=' ';
row=row-2;
PlayDrawEasy();
a[row][line]=' ';
if(getch()=='a')
{
if(a[row][line-1]!='*')
{
a[row][line-1]='1';
a[row][line]=' ';
line=line-1;
}
}
if(getch()=='d')
{
if(a[row][line+1]!='*')
{
a[row][line+1]='1';
a[row][line]=' ';
line=line+1;
}
}
if(getch()=='w')
{
if(a[row-3][line]!='*')
{
a[row-3][line]='1';
a[row][line]=' ';
row=row-3;
PlayDrawEasy();
a[row][line]=' ';
if(getch()=='a')
{
if(a[row][line-1]!='*')
{
a[row][line-1]='1';
a[row][line]=' ';
line=line-1;
}
}
if(getch()=='d')
{
if(a[row][line+1]!='*')
{
a[row][line+1]='1';
a[row][line]=' ';
line=line+1;
}
}
if(getch()=='s')
{
if(a[row+1][line]!='*')
{
a[row+1][line]='1';
a[row][line]=' ';
row=row+1;
}
}
sleep(0.5);
a[row][line]=' ';
}
}
if(getch()=='s')
{
if(a[row+1][line]!='*')
{
a[row+1][line]='1';
a[row][line]=' ';
row=row+1;
}
}
sleep(0.5);
a[row][line]=' ';
fall();
}
}
if(k=='q')
{
if(a[row-1][line]!='*')
{
a[row-1][line]='1';
a[row][line]=' ';
row=row-1;
PlayDrawEasy();
a[row][line]=' ';
if(getch()=='a')
{
if(a[row][line-1]!='*')
{
a[row][line-1]='1';
a[row][line]=' ';
line=line-1;
}
}
if(getch()=='d')
{
if(a[row][line+1]!='*')
{
a[row][line+1]='1';
a[row][line]=' ';
line=line+1;
}
}
sleep(0.5);
a[row][line]=' ';
fall();
}
}
if(k=='e')
{
if(a[row-3][line]!='*')
{
a[row-3][line]='1';
a[row][line]=' ';
row=row-3;
PlayDrawEasy();
a[row][line]=' ';
if(getch()=='a')
{
if(a[row][line-1]!='*')
{
a[row][line-1]='1';
a[row][line]=' ';
line=line-1;
}
}
if(getch()=='d')
{
if(a[row][line+1]!='*')
{
a[row][line+1]='1';
a[row][line]=' ';
line=line+1;
}
}
sleep(0.5);
a[row][line]=' ';
fall();
}
}
if(k=='i')
{
if(getch()=='p')
{
for(int i=1;i<=14;i++)
for(int j=1;j<=line2;j++)
if(a[i][j]=='*')
a[i][j]=' ';
PlayDrawEasy();
}
if(getch()=='o')
{
for(int i=1;i<=14;i++)
for(int j=1;j<=line2;j++)
if(a[i][j]=='5')
a[i][j]=' ';
PlayDrawEasy();
}
}
}
finish=time(NULL);
}
void Opening()
{
cout<<"\033[2J\033[100A\033[0m";
cout<<"简单:1"<<endl<<"中等:2"<<endl<<"困难:3"<<endl<<"地狱:4"<<endl<<"无敌:5"<<endl;
}
void Point()
{
int rev=0;
for(int i=1;i<=15;i++)
for(int j=1;j<=70;j++)
if(a[i][j]=='5')
rev++;
point=18-rev;
cout<<"金币数量:"<<point<<endl;
}
void GameTime()
{
if((finish-start)/60<10)
if((finish-start)%60>=10)
cout<<"时间:"<<'0'<<(finish-start)/60<<":"<<(finish-start)%60<<endl;
else
cout<<"时间:"<<'0'<<(finish-start)/60<<":0"<<(finish-start)%60<<endl;
else
if((finish-start)%60>=10)
cout<<"时间:"<<(finish-start)/60<<":"<<(finish-start)%60<<endl;
else
cout<<"时间:"<<(finish-start)/60<<":0"<<(finish-start)%60<<endl;
}
void Game()
{
Opening();
Menu();
PrintScreen();
Play();
Point();
GameTime();
}
int main()
{
Game();
}
|