您的位置:首页 > 其它

用面向对象的思想设计的游戏——桃花侠大战菊花怪

2016-04-25 15:48 471 查看
#include<iostream>

#include<string>

#include<Windows.h>

#include<conio.h>

#include<stdlib.h>

#include<time.h>

#define random(x) (rand()%x)

using namespace std;

char qx[6] = { 25, 37, 46, 18, 7, 33 };

char qy[6] = { 14, 41, 36, 27, 15, 3 };

char shape1[3][3] = { '@', '_', '@', '>', 3, '<', '/', 32, '\\' };

char shape2[3][3] = { '>', '_', '<', '[', 5, ']', '/', 19, '\\' };

char court[30][50];

class Interface

{

public:
int x, y;
Interface(int xx = 0, int yy = 0) :x(xx), y(yy){}

};

class questionmark :public Interface{

public:
questionmark(int a = 0, int b = 0) :Interface(a, b){}
int fall()
{
srand((int)time(0));
return random(6) + 1;
}

};

class person :public Interface

{

public:
int HP, LP;
string name;
int damage;
string skill1;
int damage1;
int sp1;
string skill2;
int damage2;
int sp2;
char shape[3][3];
int walk;
int radius;
person(char s[3][3], int xx = 1, int yy = 1, int w = 1, int r = 1, int d = 50, int d1 = 100, int d2 = 175, string a = "Default", string b = "hentai", string c = "yucen", int hp = 1000, int lp = 1000, int s1 = 0, int s2 =
0) :Interface(xx, yy), HP(hp), LP(lp), radius(r), name(a), skill1(b), skill2(c), damage(d), damage1(d1), damage2(d2), walk(w), sp1(s1), sp2(s2)
{
int i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
shape[i][j] = s[i][j];
}
}

void changeHP()
{
HP += 100;
}
void changeD()
{
damage += 25;
}
string getn()
{
return name;
}
void move()
{
int a;
int b;
int i, j;
int m;
int flag = 1;
questionmark q;
while (flag)
{
case1:
cout << "请输入走到哪儿[dx(纵向),dy(横向)]:";
cin >> a >> b;
if (x + a<2 || y + b<2 || x + a>27 || y + b>47)
{
cout << "超出场地范围!" << endl;
goto case1;
}
else if (a > walk || b > walk)
{
cout << "超出可移动的最大半径!" << endl;
goto case1;
}
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
court[i][j] = 32;
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (court[x + a - 1 + i][y + a - 1 + j] != 32)
{
cout << "有障碍无法前行" << endl;
goto case1;
}
}
}
x += a;
y += b;
for (m = 0; m < 6; m++)
{
if ((x == qx[m]) && (y == qy[m]))
{
cout << "Surprise!" << endl;
int num = q.fall();
switch (num)
{
case 1:HP -= 75; cout << "HP-75"; break;
case 2:HP += 75; cout << "HP+75"; break;
case 3:LP -= 100; cout << "LP-100"; break;
case 4:LP += 100;  cout << "LP+100"; break;
case 5:damage -= 10; cout << "damage-10"; break;
case 6:damage += 15; cout << "damage+15"; break;
}
}
}
flag = 0;
}
}
void attack(person &p)
{
int flag = 1;
while (flag)
{
int a;
case2:
cout << "请选择进攻方式:";
cin >> a;
if (a == 1)
{
p.HP -= damage;
cout << p.name << "HP-" << damage << endl;
Slee
4000
p(1000);
flag = 0;
}
else if (a == 2)
{
p.HP -= damage1;
LP -= sp1;
if (LP <= 0)
{
cout << "输入无效!!!" << endl;
Sleep(1000);
LP += sp1;
goto case2;
}
cout << p.name << "HP-" << damage1 << endl;
Sleep(1000);
flag = 0;
}
else if (a == 3)
{
p.HP -= damage2;
LP -= sp2;
if (LP <= 0)
{
cout << "输入无效!!!" << endl;
Sleep(1000);
LP += sp2;
goto case2;
}
cout << p.name << "HP-" << damage2 << endl;
Sleep(1000);
flag = 0;
}
else
{
cout << "输入无效!!!" << endl;
Sleep(1000);
}
}
}
int search()
{
int flag = 1;
int i = 0, j = 0;
int a = 0, b = 0;
int m, n;
while (flag)
{
if (x - radius + i < 0)
{
i++;
flag = 1;
}
else
{
flag = 0;
}
}
flag = 1;
while (flag)
{
if (y - radius + j < 0)
{
j++;
flag = 1;
}
else
{
flag = 0;
}
}
flag = 1;
while (flag)
{
if (x + radius - a > 29)
{
a++;
flag = 1;
}
else
{
flag = 0;
}
}
flag = 1;
while (flag)
{
if (y + radius - b > 49)
{
b++;
flag = 1;
}
else
{
flag = 0;
}
}
flag = 1;
for (m = x - radius + i; m < x - 1; m++)
{
for (n = y - radius + j; n < y + radius + b; n++)
{
if ((court[m]
!= '|') && (court[m]
!= '-') && (court[m]
!= 32))
{
flag = 0;
}
}
}
for (m = x + radius + a; m > x + 1; m--)
{
for (n = y - radius + j; n < y + radius + b; n++)
{
if ((court[m]
!= '|') && (court[m]
!= '-') && (court[m]
!= 32))
{
flag = 0;
}
}
}
for (n = y - radius + j; n < y - 1; n++)
{
for (m = x - radius + i; m < x + radius + a; m++)
{
if ((court[m]
!= '|') && (court[m]
!= '-') && (court[m]
!= 32))
{
flag = 0;
}
}
}
for (n = y + radius + b; n > y + 1; n--)
{
for (m = x - radius + i; m < x + radius + a; m++)
{
if ((court[m]
!= '|') && (court[m]
!= '-') && (court[m]
!= 32))
{
flag = 0;
}
}
}
if (flag == 0)
{
return 1;
}
else
{
return 0;
}
}

};

/*class terrain :public Interface

{

protected:

int HP;

public:

terrain(int xx = 0, int yy = 0, int hp = 400) :Interface(xx, yy), HP(hp){}

int fall()

{

srand((int)time(0));

return random(3) + 1;

}

void HPdecline(int a)

{

HP -= a;

}

};*/

/*class tree :public terrain{

public:

char T[2][1];

tree(int xx = 0, int yy = 0, int hp = 400) :terrain(xx,yy,hp)

{

T[0][0] = 2;

T[1][0] = 19;

}

};*/

/*class stone :public terrain{

public:

char S[2][2];

stone(int xx = 0, int yy = 0, int hp = 400) :terrain(xx,yy,hp)

{

S[0][0] = 'o';

S[0][1] = 'o';

S[1][0] = 'o';

S[1][1] = 'o';

}

};*/

void changeP(int x, int y, char s[3][3])

{
court[x][y] = s[1][1];
court[x - 1][y] = s[0][1];
court[x + 1][y] = s[2][1];
court[x][y - 1] = s[1][0];
court[x][y + 1] = s[1][2];
court[x - 1][y - 1] = s[0][0];
court[x - 1][y + 1] = s[0][2];
court[x + 1][y - 1] = s[2][0];
court[x + 1][y + 1] = s[2][2];

}

void draw()

{
int i, j;
for (i = 1; i < 29; i++)
{
for (j = 1; j < 49; j++)
court[i][j] = ' ';
}
for (i = 1; i < 29; i++)
{
court[i][0] = '|';
court[i][49] = '|';
}
for (j = 0; j < 50; j++)
{
court[0][j] = '-';
court[29][j] = '-';
}

}

int main()

{
person P1(shape1, 2, 2, 4, 5, 50, 100, 200, "桃花侠", "桃花波", "桃花剑舞", 1000, 1250, 100, 175);
person P2(shape2, 27, 47, 3, 3, 40, 80, 160, "菊花怪", "一只菊", "菊花爆满山", 1250, 1500, 115, 195);
int flag = 1;
int fat = 1;
while (fat)
{
cout << "
桃花侠大战菊花怪!" << endl;
int i, j;
for (i = 1; i < 29; i++)
{
for (j = 1; j < 49; j++)
court[i][j] = ' ';
}
for (i = 1; i < 29; i++)
{
court[i][0] = '|';
court[i][49] = '|';
}
for (j = 0; j < 50; j++)
{
court[0][j] = '-';
court[29][j] = '-';
}

changeP(P1.x, P1.y, P1.shape);

changeP(P2.x, P2.y, P2.shape);

for (i = 0; i < 30; i++)
{
for (j = 0; j < 50; j++)
{
if (P1.x - 1 <= i&&P1.x + 1 >= i&&P1.y - 1 <= j&&P1.y + 1 >= j)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
| FOREGROUND_RED);
cout << court[i][j];
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
else if (P2.x - 1 <= i&&P2.x + 1 >= i&&P2.y - 1 <= j&&P2.y + 1 >= j)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
| FOREGROUND_RED | FOREGROUND_GREEN);
cout << court[i][j];
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
else
cout << court[i][j];
}
if (j == 50)
cout << '\n';
}
cout << "P1 桃花侠" << "\t\t\t" << "P2 菊花怪" << endl;
cout << "HP=" << P1.HP << ',' << "LP=" << P1.LP << "\t\t\t" << "HP=" << P2.HP << ',' << "LP=" << P2.LP << endl;
cout << "1.Attack" << "\t\t\t" << "1.Attack" << endl;
cout << "Damage:" << P1.damage << "\t\t\t" << "Damage:" << P2.damage << endl;
cout << "2.Skill One:" << P1.skill1 << "\t\t" << "2.Skill One:" << P2.skill1 << endl;
cout << "Damage:" << P1.damage1 << "\t\t\t" << "Damage:" << P2.damage1 << endl;
cout << "Consume LP:" << P1.sp1 << "\t\t\t" << "Consume LP:" << P2.sp1 << endl;
cout << "3.Skill Two:" << P1.skill2 << "\t\t" << "3.Skill Two:" << P2.skill2 << endl;
cout << "Damage:" << P1.damage2 << "\t\t\t" << "Damage:" << P2.damage2 << endl;
cout << "Consume LP:" << P1.sp2 << "\t\t\t" << "Consume LP:" << P2.sp2 << endl;
cout << "Attack Radius:" << P1.radius << "\t\t\t" << "Attack Radius:" << P2.radius << endl;
cout << "Moving Radius:" << P1.walk << "\t\t\t" << "Moving Radius:" << P2.walk << endl;
if (flag == 1)
{
cout << "桃花侠's Round:" << endl;
P1.move();
system("cls");
cout << "
桃花侠大战菊花怪!" << endl;
for (i = 1; i < 29; i++)
{
for (j = 1; j < 49; j++)
court[i][j] = ' ';
}
for (i = 1; i < 29; i++)
{
court[i][0] = '|';
court[i][49] = '|';
}
for (j = 0; j < 50; j++)
{
court[0][j] = '-';
court[29][j] = '-';
}
changeP(P1.x, P1.y, P1.shape);
changeP(P2.x, P2.y, P2.shape);
for (i = 0; i < 30; i++)
{
for (j = 0; j < 50; j++)
cout << court[i][j];
if (j == 50)
cout << '\n';
}
cout << "P1 桃花侠" << "\t\t\t" << "P2 菊花怪" << endl;
cout << "HP=" << P1.HP << ',' << "LP=" << P1.LP << "\t\t\t" << "HP=" << P2.HP << ',' << "LP=" << P2.LP << endl;
cout << "1.Attack" << "\t\t\t" << "1.Attack" << endl;
cout << "Damage:" << P1.damage << "\t\t\t" << "Damage:" << P2.damage << endl;
cout << "2.Skill One:" << P1.skill1 << "\t\t" << "2.Skill One:" << P2.skill1 << endl;
cout << "Damage:" << P1.damage1 << "\t\t\t" << "Damage:" << P2.damage1 << endl;
cout << "Consume LP:" << P1.sp1 << "\t\t\t" << "Consume LP:" << P2.sp1 << endl;
cout << "3.Skill Two:" << P1.skill2 << "\t\t" << "3.Skill Two:" << P2.skill2 << endl;
cout << "Damage:" << P1.damage2 << "\t\t\t" << "Damage:" << P2.damage2 << endl;
cout << "Consume LP:" << P1.sp2 << "\t\t\t" << "Consume LP:" << P2.sp2 << endl;
cout << "Attack Radius:" << P1.radius << "\t\t\t" << "Attack Radius:" << P2.radius << endl;
cout << "Moving Radius:" << P1.walk << "\t\t\t" << "Moving Radius:" << P2.walk << endl;
if (P1.search() == 1)
{
int t;
cout << "请选择下一步的操作:" << endl;
cout << "1.攻击" << endl;
cout << "2.一动不动" << endl;
cin >> t;
if (t == 1)
{
P1.attack(P2);
}
if (t == 2)
{
cout << "愚蠢!" << endl;
}
}
flag = 0;
}
else
{
cout << "菊花怪's Round:" << endl;
P2.move();
system("cls");
cout << "
桃花侠大战菊花怪!" << endl;
for (i = 1; i < 29; i++)
{
for (j = 1; j < 49; j++)
court[i][j] = ' ';
}
for (i = 1; i < 29; i++)
{
court[i][0] = '|';
court[i][49] = '|';
}
for (j = 0; j < 50; j++)
{
court[0][j] = '-';
court[29][j] = '-';
}
changeP(P1.x, P1.y, P1.shape);
changeP(P2.x, P2.y, P2.shape);
for (i = 0; i < 30; i++)
{
for (j = 0; j < 50; j++)
{
if (P1.x - 1 <= i&&P1.x + 1 >= i&&P1.y - 1 <= j&&P1.y + 1 >= j)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
| FOREGROUND_RED);
cout << court[i][j];
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
else if (P2.x - 1 <= i&&P2.x + 1 >= i&&P2.y - 1 <= j&&P2.y + 1 >= j)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
| FOREGROUND_RED | FOREGROUND_GREEN);
cout << court[i][j];
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
else
cout << court[i][j];
}
if (j == 50)
cout << '\n';
}
cout << "P1 桃花侠" << "\t\t\t" << "P2 菊花怪" << endl;
cout << "HP=" << P1.HP << ',' << "LP=" << P1.LP << "\t\t\t" << "HP=" << P2.HP << ',' << "LP=" << P2.LP << endl;
cout << "1.Attack" << "\t\t\t" << "1.Attack" << endl;
cout << "Damage:" << P1.damage << "\t\t\t" << "Damage:" << P2.damage << endl;
cout << "2.Skill One:" << P1.skill1 << "\t\t" << "2.Skill One:" << P2.skill1 << endl;
cout << "Damage:" << P1.damage1 << "\t\t\t" << "Damage:" << P2.damage1 << endl;
cout << "Consume LP:" << P1.sp1 << "\t\t\t" << "Consume LP:" << P2.sp1 << endl;
cout << "3.Skill Two:" << P1.skill2 << "\t\t" << "3.Skill Two:" << P2.skill2 << endl;
cout << "Damage:" << P1.damage2 << "\t\t\t" << "Damage:" << P2.damage2 << endl;
cout << "Consume LP:" << P1.sp2 << "\t\t\t" << "Consume LP:" << P2.sp2 << endl;
cout << "Attack Radius:" << P1.radius << "\t\t\t" << "Attack Radius:" << P2.radius << endl;
cout << "Moving Radius:" << P1.walk << "\t\t\t" << "Moving Radius:" << P2.walk << endl;
if (P2.search() == 1)
{
int t;
case1:
cout << "请选择下一步的操作:" << endl;
cout << "1.攻击" << endl;
cout << "2.一动不动" << endl;
cin >> t;
if (t == 1)
{
P2.attack(P1);
}
else if (t == 2)
{
cout << "愚蠢!" << endl;
Sleep(2000);
}
else
{
cout << "Error!";
Sleep(1500);
goto case1;
}
}
flag = 1;
}
system("cls");
if ((P1.HP <= 0))
{
cout << "菊花怪胜利!" << endl;
fat = 0;
}
else if ((P2.HP <= 0))
{
cout << "桃花侠胜利!" << endl;
fat = 0;
}
}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  面向对象 游戏 设计