C++ 跑酷
这是一个跑酷作品
允许自行创建地图
游戏规则:
WASD 移动, 空格键切换 建造/游玩 ZXCVBNM 功能键,详见游戏内部
#include<game.h>
#undef UNICODE
#undef _UNICODE
using namespace std;
const int GY=500,GX=500;
int mute=1;
/***************************************
Paste the copy code here and press F9 |
***************************************/
char world[GX][GY];/*=
{
" ",
};*/
void Kill()
{
if(mute==0)Beep(750,100);
cout<<"You're dead!"<<endl;
cout<<"Press R to continue"<<endl;
while(1)
{
if(mute==0)Beep(750,100);
if(GetKeyState('R')<0)break;
}
}
struct music
{
int d=100;
int pitch=300;
};
music sound;
void Sound(int t)
{
if(sound.pitch<=300)sound.d=100;
if(sound.pitch>=1200)sound.d=-100;
sound.pitch+=sound.d;
if(mute==0)Beep(sound.pitch,t);
else Sleep(t);
}
int PlayerX=30,PlayerY=31,CursorX,CursorY,SpawnX=31,SpawnY=35,Speed;
bool Game;
void Read()
{
ifstream ifs;
ifs.open("Parkour.txt", ios::out);
if (!ifs.is_open())
{
ofstream ofs;
ofs.open("Parkour.txt", ios::out);
}
char c=0;
int n=0,m=0,i=0;
while ((c = ifs.get()) != EOF)
{
m++;
if(m>=GY)
{
m=0;
n++;
}
world[n][m-1]=c;
}
//ofs.close();
ifs.open("SpawnX.txt", ios::out);
if (!ifs.is_open())
{
ofstream ofs;
ofs.open("SpawnX.txt", ios::out);
}
//SpawnX=0;
int a;
while ((c = ifs.get()) != EOF)
{
if(c=='0')a=0;
if(c=='1')a=1;
if(c=='2')a=2;
if(c=='3')a=2;
if(c=='4')a=4;
if(c=='5')a=5;
if(c=='6')a=6;
if(c=='7')a=7;
if(c=='8')a=8;
if(c=='9')a=9;
//SpawnX=SpawnX*10+a;
}
//ofs.close();
ifs.open("SpawnY.txt", ios::out);
if (!ifs.is_open())
{
ofstream ofs;
ofs.open("SpawnY.txt", ios::out);
}
//SpawnY=0;
while ((c = ifs.get()) != EOF)
{
if(c=='0')a=0;
if(c=='1')a=1;
if(c=='2')a=2;
if(c=='3')a=2;
if(c=='4')a=4;
if(c=='5')a=5;
if(c=='6')a=6;
if(c=='7')a=7;
if(c=='8')a=8;
if(c=='9')a=9;
//SpawnY=SpawnY*10+a;
}
//ofs.close();
//ofs.close();
}
void Reset()
{
for(int i=0;i<GY;i++)
{
for(int j=0;j<GY;j++)
{
world[i][j]=' ';
if(i>GY-31)world[i][j]='%';
}
}
}
void Load()
{
/*system("cls");
cout<<"Please enter the code"<<endl;
cin>>SpawnX>>SpawnY;
int cnt,num;
char tile;
int i=0;
int n,m;
while(cnt!=0)
{
cin>>cnt>>tile;
num=i;
for(;i<cnt+num;i++)
{
n=i/GY;
m=i%GY;
world[n][m]=tile;
}
}
system("mode con cols=70 lines=28");*/
}
void Save()
{
/*system("cls");
cout<<"COPY THE SAVE STRING BELOW"<<endl;
system("mode con cols=300 lines=75");
cout<<SpawnX<<" "<<SpawnY<<endl;
for(int i=0;i<GX;i++)
{
cout<<"{";
for(int j=0;j<GY;j++)
{
//cout<<i<<" "<<j<<" ";
cout<<"'";
cout<<world[i][j];
cout<<"',";
}
cout<<"}"<<endl;
}
int i=0,cnt=0,n=0,m=0;
char tile=0;
while(i<(GX-0)*(GY-0))
{
if(world[n][m]==tile)
{
cnt++;
}else
{
//cout<<tile<<" "<<cnt<<" ";
cnt=1;
tile=world[n][m];
}
i++;
n=i/GY;
m=i%GY;
//cout<<n<<" "<<m<<" "<<i<<endl;
}
cout<<"0 *";
cout<<"-----------------------------------------------------------"<<endl;
system("pause");
system("mode con cols=70 lines=28");*/
ofstream ofs;
ofs.open("Parkour.txt", ios::out);
for(int i=0;i<GX;i++)
{
for(int j=0;j<GY;j++)
{
ofs<<world[i][j];
}
}
ofs.close();
ofs.open("SpawnX.txt", ios::out);
ofs<<SpawnX;
ofs.close();
ofs.open("SpawnY.txt", ios::out);
ofs<<SpawnY;
ofs.close();
}
void ScreenUpdate()
{
//system("cls");
gotoxy(0,0);
bool x;
for(int i=PlayerY+10;i>PlayerY-9;i--)
{
for(int j=PlayerX-15;j<15+PlayerX;j++)
{
char c=world[GY-i-1][j];
//if(PlayerX==j&&PlayerY==1-i)
if(PlayerX==j&&PlayerY==i)
{
if(Game==1)
{
Color(BLACK,LYELLOW);
cout<<"Me";
}
else
{
Color(BLACK,BLACK);
cout<<" ";
}
}
else if(Game==0&&SpawnX==j&&SpawnY==i)
{
Color(BLACK,PURPLE);
cout<<" ";
}
else if(c=='T')
{
Color(BLACK,GREEN);
cout<<" ";
}
else if(c=='%')
{
Color(BLACK,RED);
cout<<" ";
}
else if(c=='=')
{
Color(BLACK,DIRT);
cout<<" ";
}
else if(c=='#')
{
Color(BLACK,LPURPLE);
cout<<" ";
}
else if(c==' ')
{
Color(BLACK,SBLUE);
cout<<" ";
}
}
Color(BLACK,BLACK);
cout<<" "<<endl;
}
Color(WHITE,BLACK);
cout<<"------------------------------------------------------------"<<endl;
cout<<"Z-Eraser X-Floor C-Bounce V-Spike B-Save N-Load M-End"<<endl;
cout<<"X: "<<PlayerX-29<<" \tY: "<<PlayerY-29<<" \tT-Command ";
}
void Command()
{
system("cls");
cout<<" Here's some command that you can use"<<endl;
cout<<"Press Enter to delet the disgusting string of wwwasddddwwassd..."<<endl;
cout<<"_________________________________________________________________"<<endl;
cout<<"/teleport <X,Y>"<<endl;
cout<<"/set <X,Y,tile>"<<endl;
cout<<"/setrect <X1,Y1,X2,Y2,tile>"<<endl;
cout<<"/erase <X1,Y1,X2,Y2,tile>"<<endl;
cout<<"/getscene <X,Y>"<<endl;
cout<<"/reset"<<endl;
cout<<"/mute"<<endl;
string str;
cin>>str;
str="";
cin>>str;
int X,Y,X2,Y2;
char Tile;
if(str=="/teleport")
{
cin>>X>>Y;
system("cls");
cout<<"Completed teleporting to "<<X<<","<<Y<<endl;
PlayerX=X;
PlayerY=Y;
}else if(str=="/set")
{
cin>>X>>Y>>Tile;
X+=29;Y+=29;
world[Y][X]=Tile;
}else if(str=="/setrect")
{
cin>>X>>Y>>X2>>Y2>>Tile;
X+=29;Y+=29;X2+=29;Y2+=29;
system("cls");
cout<<"Completed setting rectangle"<<endl;
for(int i=Y;i<Y2;i++)
{
for(int j=X;j<X2;j++)
{
world[i][j]=Tile;
}
}
}else if(str=="/erase")
{
cin>>X>>Y>>X2>>Y2;
X+=29;Y+=29;X2+=29;Y2+=29;
system("cls");
cout<<"Completed setting rectangle"<<endl;
for(int i=Y;i<Y2;i++)
{
for(int j=X;j<X2;j++)
{
world[i][j]=' ';
}
}
}else if(str=="/reset")
{
Reset();
}else if(str=="/mute")
{
mute=1-mute;
}
else
{
cout<<"Unknown command "<<str<<endl;
}
Sleep(1000);
system("cls");
}
int Jump,Bounce;
void TryMove()
{
if(Game==1)
{
if(Jump<0)Jump=0;
if(Bounce<0)Bounce=0;
if(world[GY-PlayerY][PlayerX]=='T')Bounce=5;
if(Jump==0&&Bounce==0)
{
if(world[GY-PlayerY][PlayerX]==' '||world[GY-PlayerY][PlayerX]=='%')PlayerY--;
}
else if(world[GY-2-PlayerY][PlayerX]==' '||world[GY-2-PlayerY][PlayerX]=='%')
{
if(Jump>0)PlayerY++;
else
{
PlayerY++;
if(world[GY-2-PlayerY][PlayerX]==' '||world[GY-2-PlayerY][PlayerX]=='%')
PlayerY++;
}
}
else
{
Jump=0;
Bounce=0;
}
Jump--;Bounce--;
if(GetKeyState('W')<0&&world[GY-PlayerY][PlayerX]=='=')
{
Jump=5;
}
}
if(GetKeyState('W')<0&&Game==0)PlayerY++;
if(GetKeyState('S')<0&&Game==0)PlayerY--;
if(GetKeyState('S')<0&&Game==1)Jump=0;
if(GetKeyState('B')<0&&Game==0)Save();
if(GetKeyState('N')<0&&Game==0)Read();
if(GetKeyState('T')<0&&Game==0)Command();
if(GetKeyState('A')<0)
{
if(Game==1&&world[GY-1-PlayerY][PlayerX-1]!='='||Game==0)PlayerX--;
if(GetKeyState('Q')<0)
if(Game==1&&world[GY-1-PlayerY][PlayerX-1]!='='||Game==0)PlayerX--;
}
if(GetKeyState('D')<0)
{
if(Game==1&&world[GY-1-PlayerY][PlayerX+1]!='='||Game==0)PlayerX++;
if(GetKeyState('Q')<0)
if(Game==1&&world[GY-1-PlayerY][PlayerX+1]!='='||Game==0)PlayerX++;
}
if(PlayerY<31||world[GY-1-PlayerY][PlayerX]=='%')
{
if(Game==1)
{
PlayerY=SpawnY;
PlayerX=SpawnX;
Jump=0;
system("cls");
Kill();
}
}
if(PlayerX<30)PlayerX=30;
if(PlayerX>GY-31)PlayerX=GY-31;
if(PlayerY>GX-1)PlayerY=GX-1;
if(PlayerY<30)PlayerY=30;
}
void Make()
{
char key;
while(1)
{
Game=0;
ScreenUpdate();
if(GetKeyState('E')<0)Speed=50;
else Speed=150;
Sound(Speed);
//Sleep(Speed);
TryMove();
if(GetKeyState('Z')<0)world[GY-1-PlayerY][PlayerX]=' ';
if(GetKeyState('X')<0)world[GY-1-PlayerY][PlayerX]='=';
if(GetKeyState('C')<0)world[GY-1-PlayerY][PlayerX]='T';
if(GetKeyState('V')<0)world[GY-1-PlayerY][PlayerX]='%';
if(GetKeyState('M')<0)
{
world[GY-1-PlayerY][PlayerX]='#';
}
if(GetKeyState(' ')<0)
{
Game=1;
system("mode con cols=70 lines=28");
PlayerY=SpawnY;
PlayerX=SpawnX;
while(GetKeyState(' ')<0);
while(GetKeyState(' ')>=0)
{
TryMove();
ScreenUpdate();
if(world[GY-1-PlayerY][PlayerX]=='#')
{
system("cls");
cout<<"You win!";
MessageBox(NULL,"","YOU WIN!",MB_OK);
PlayerY=SpawnY;
PlayerX=SpawnX;
}
Sound(100);
//Sleep(100);
}
}
}
}
int main()
{
//SetConsoleCursorInfo(hOut,&cci);
Reset();
system("mode con cols=70 lines=28");
//MainPage();
Make();
return 0;
}