最近写了一段迷宫打怪游戏,我想着要有生命值、分数、name(名字),干脆用了类,实际用不用都一样...不要自己改代码,因为里面的x/y坐标是反着的,你们搞不清楚,一改就废
代码(dev-c++运行通过,无报错无警告):
#include<iostream>
#include<cstdio>
#include<time.h>
#include<windows.h>
#include<iomanip>
#include<string>
#include<conio.h>
using namespace std;
class game
{
public:
void setcoin(int c,string n,int r)
{
coin = c;
name = n;
ren = r;
}
void increasecoin()
{
coin += 5;
}
void increaseren()
{
ren -= 5;
}
void print()
{
cout<<endl<<"coin = "<<coin;
cout<<endl<<"name = "<<name<<endl;
cout<<endl<<"生命 = "<<ren;
}
private:
int coin;
string name;
int ren;
};
int main()
{
start:
system("color F0");
/*****地图*****/
char a[1000][1000] = {
"########################################",
"#* # #",
"## $######### ################# ###### #",
"# # # # # ## # #",
"# # # #### ## # ##@# #### ##### ## # # #",
"# # # # # # # # # # # #",
"# # # #### ###### ############### # #",
"# # ## # # # # # #",
"# # #### ######## ################# # #",
"# ## # #$ #%# ##### # # #",
"# #####$### # # # ### # #",
"### # # # # ##### # # #",
"# ######################## # # # #",
"# # # # ## # ###",
"# ### #### ####### ########### #### ## #",
"# # # # +#",
"########################################",//大怪4 小怪3
};
/*****初始化*****/
cout<<"走迷宫游戏(+是出口,$是金币,%是大怪兽,@是小怪兽,打掉2个怪兽且生命大于80才能通关)"<<endl;
cout<<"请输入你的昵称"<<endl;
string na;
cin>>na;
game player;
player.setcoin(0,na,100);
int jb=0;
int rr=100;
/*****打印map(a)*****/
for (int i=0;i<=16;i++)
{
puts(a[i]);
}
/*****移动*****/
char ch;
int x=1,y=1;
while(1){
ch=_getch();
if(ch=='a'){
if(a[x][y-1]!='#'){
a[x][y]=' ';
y--;
a[x][y]='*';
}
}
if(ch=='s'){
if(a[x+1][y]!='#'){
a[x][y]=' ';
x++;
a[x][y]='*';
}
}
if(ch=='d'){
if(a[x][y+1]!='#'){
a[x][y]=' ';
y++;
a[x][y]='*';
}
}
if(ch=='w'){
if(a[x-1][y]!='#'){
a[x][y]=' ';
x--;
a[x][y]='*';
}
}
system("cls");
for(int i=0;i<=16;i++)
puts(a[i]);
if (x==15&&y==37)
{
break;
}
if (x==2&&y==3)
{
cout<<endl<<"金币+5";
player.increasecoin();
jb+=5;
}
if (x==9&&y==15)
{
cout<<endl<<"金币+5";
player.increasecoin();
jb+=5;
}
if (x==10&&y==9)
{
cout<<endl<<"金币+5";
player.increasecoin();
jb+=5;
}
if (x==9&&y==18)
{
a[10][16] = '#';
cout<<endl<<"你的战斗力是"<<jb/3<<",大怪兽战斗力是4,";
if (jb/3>4){
cout<<"你赢了, 但你的生命值-5";
player.increaseren();
rr -= 5;
if (x==9&&y==18)
{
cout<<" ";
}
}
if (jb/3<4){
cout<<"你输了,但怪兽被打死,你的生命值-15";
player.increaseren();
player.increaseren();
player.increaseren();
rr-=15;
}
}
if (x==4&&y==18)
{
cout<<endl<<"你的战斗力是"<<jb/4<<",小怪兽战斗力是3,";
if (jb/4>3){
cout<<"你赢了, 但你的生命值-5";
player.increaseren();
rr -= 5;
}
if (jb/4<3){
cout<<"你输了,但怪兽被打死,你的生命值-15";
player.increaseren();
player.increaseren();
player.increaseren();
rr-=15;
}
if (jb/4==3)
{
cout<<"生命值不变";
}
}
}
if (rr>=80)
{
system("cls");
cout<<"你赢了!";
player.print();
}
if (rr<80)
{
system("cls");
cout<<"你输了!";
player.print();
cout<<endl<<"按下a再来一局";
int a =_getch();
if (a==97)
{
goto start;
}
}
return 0;
}