- 分享
如何用c++字符来做3d游戏(5)
- 2024-2-6 13:15:46 @
接上期
我们继续补全主函数
//主函数
int main() {
long long map[50][50][20] = {0};
long long delay;//延迟
application1();
application2();
system("cls");//清屏
application3();
system("cls");//清屏
application4();
system("cls");//清屏
application5();
system("cls");//清屏
for (;;) {
cout << "输入密码:";
getline(cin, character); ////一整行的输入数据都识别
if (character != password1) {
cout << "密码错误";
Sleep(500);//等待0.5秒
system("cls");//清屏
} else {
printf("ok");
Sleep(500);//等待0.5秒
system("cls");//清屏
break;
}
}
cout << "延迟:";
cin >> delay;
system("cls");//清屏
//以防死亡就不能继续玩
hp02 = hp2;
for (;;) {
//赋值
hp01 = hp1;
height = 0;
x = homex;
y = homey;
h = homeh;
electricity = 100;
direction1 = 0;
direction2 = 90;
attack = 0;
system("cls");//清屏
//游戏内部
for (;;) {
//加载地面
for (long long i = x - 1; i < x + 2; i++)
for (long long j = y - 1; j < y + 2; j++)
if (map[i][j][25] != 1)
map[i][j][25] = 2;
//加载边界墙
if (x == 49) {
map[x + 1][y][h] = 3;
}
if (y == 49) {
map[x][y + 1][h] = 3;
}
if (h == 19) {
map[x][y][h + 1] = 3;
}
if (x == 1) {
map[x - 1][y][h] = 3;
}
if (y == 1) {
map[x][y - 1][h] = 3;
}
if (h == 1) {
map[x][y][h - 1] = 3;
}
//主题
cout << "坐标:" << x << ',' << y << ',' << h << endl;
cout << "我的血量:" << hp1 << "\\" << hp01 << endl;
cout << "Boss的血量:" << hp2 << "\\" << hp02 << endl;
cout << "手电筒的电量:" << electricity << endl;
//显示
//前
if (direction1 == 0 && direction2 == 90) {
for (long long i = y + 1; i > y - 2; i--) {
for (long long j = h + 1; j < h + 3; j++) {
if (map[x + 1][i][j] == 0) {
cout << ' ';
} else if (map[x + 1][i][j] == 1) {
cout << '?';
} else if (map[x + 1][i][j] == 2) {
cout << '#';
} else if (map[x + 1][i][j] == 3) {
cout << '&';
}
}
cout << endl;
}
}
//后
if (direction1 == 180 && direction2 == 90) {
for (long long i = y + 1; i > y - 2; i--) {
for (long long j = h + 1; j < h + 3; j++) {
if (map[x - 1][i][j] == 0) {
cout << ' ';
} else if (map[x - 1][i][j] == 1) {
cout << '?';
} else if (map[x - 1][i][j] == 2) {
cout << '#';
} else if (map[x - 1][i][j] == 3) {
cout << '&';
}
}
cout << endl;
}
}
//左
if (direction1 == 270 && direction2 == 90) {
for (long long i = x + 1; i > x - 2; i--) {
for (long long j = h + 1; j < h + 3; j++) {
if (map[i][y - 1][j] == 0) {
cout << ' ';
} else if (map[i][y - 1][j] == 1) {
cout << '?';
} else if (map[i][y - 1][j] == 2) {
cout << '#';
} else if (map[i][y - 1][j] == 3) {
cout << '&';
}
}
cout << endl;
}
}
//右
if (direction1 == 90 && direction2 == 90) {
for (long long i = x + 1; i > x - 2; i--) {
for (long long j = h + 1; j < h + 3; j++) {
if (map[i][y + 1][j] == 0) {
cout << ' ';
} else if (map[i][y + 1][j] == 1) {
cout << '?';
} else if (map[i][y + 1][j] == 2) {
cout << '#';
} else if (map[i][y + 1][j] == 3) {
cout << '&';
}
}
cout << endl;
}
}
//上
if (direction2 == 180) {
system("cls");//清屏
cout << "% %" << endl << endl;
cout << "% %" << endl;
cout << " % %" << endl;
cout << " % %" << endl;
cout << " % %" << endl;
cout << " %%";
Sleep(5);//等待0.05秒
system("cls");//清屏
cout << "Game Over!!!";
Sleep(2000);//等待2秒
system("cls");//清屏
death++;
break;
}
//下
if (direction2 == 0) {
for (long long i = x - 1; i < x + 3; i++) {
for (long long j = y + 1; j > y - 2; j--) {
if (map[i][j][h - 1] == 0) {
cout << ' ';
} else if (map[i][j][h - 1] == 1) {
cout << '?';
} else if (map[i][j][h - 1] == 2) {
cout << '#';
} else if (map[i][j][h - 1] == 3) {
cout << '&';
}
}
cout << endl;
}
}
key = _getch(); //读取键盘
//按键
if (key == 119 && direction1 == 0 && direction2 == 90 && map[x + 1][y][h] == 0) {
x++;
} else if (key == 115 && direction1 == 0 && direction2 == 90 && map[x - 1][y][h] == 0) {
x--;
} else if (key == 97 && direction1 == 0 && direction2 == 90 && map[x][y - 1][h] == 0) {
y--;
} else if (key == 100 && direction1 == 0 && direction2 == 90 && map[x][y + 1][h] == 0) {
y++;
} else if (key == 103 && direction1 == 0 && direction2 == 90) {
Staging1 = x + 1;
Staging2 = y;
Staging3 = h;
map[x + 1][y][h] = 4;
attack = attack + 3;
} else if (key == 104 && direction1 == 0 && direction2 == 90 && map[x + 1][y][h] == 4) {
hp02 = hp02 - 10;
} else if (key == 119 && direction1 == 180 && direction2 == 90 && map[x - 1][y][h] == 0) {
x--;
} else if (key == 115 && direction1 == 180 && direction2 == 90 && map[x + 1][y][h] == 0) {
x++;
} else if (key == 97 && direction1 == 180 && direction2 == 90 && map[x][y + 1][h] == 0) {
y++;
} else if (key == 100 && direction1 == 180 && direction2 == 90 && map[x][y - 1][h] == 0) {
y--;
} else if (key == 103 && direction1 == 180 && direction2 == 90) {
Staging1 = x - 1;
Staging2 = y;
Staging3 = h;
map[x - 1][y][h] = 4;
attack = attack + 3;
} else if (key == 104 && direction1 == 180 && direction2 == 90 && map[x - 1][y][h] == 4) {
hp02 = hp02 - 10;
} else if (key == 119 && direction1 == 270 && direction2 == 90 && map[x][y - 1][h] == 0) {
y--;
} else if (key == 115 && direction1 == 270 && direction2 == 90 && map[x][y + 1][h] == 0) {
y++;
} else if (key == 97 && direction1 == 270 && direction2 == 90 && map[x - 1][y][h] == 0) {
x--;
} else if (key == 100 && direction1 == 270 && direction2 == 90 && map[x + 1][y][h] == 0) {
x++;
} else if (key == 103 && direction1 == 270 && direction2 == 90) {
Staging1 = x;
Staging2 = y - 1;
Staging3 = h;
map[x][y - 1][h] = 4;
attack = attack + 3;
} else if (key == 104 && direction1 == 270 && direction2 == 90 && map[x][y - 1][h] == 4) {
hp02 = hp02 - 10;
} else if (key == 119 && direction1 == 90 && direction2 == 90 && map[x][y + 1][h] == 0) {
y++;
} else if (key == 115 && direction1 == 90 && direction2 == 90 && map[x][y - 1][h] == 0) {
y--;
} else if (key == 97 && direction1 == 90 && direction2 == 90 && map[x + 1][y][h] == 0) {
x++;
} else if (key == 100 && direction1 == 90 && direction2 == 90 && map[x - 1][y][h] == 0) {
x--;
} else if (key == 103 && direction1 == 90 && direction2 == 90) {
Staging1 = x;
Staging2 = y + 1;
Staging3 = h;
map[x][y + 1][h] = 4;
attack = attack + 3;
} else if (key == 104 && direction1 == 0 && direction2 == 90 && map[x][y + 1][h] == 4) {
hp02 = hp02 - 10;
} else if (key == 105) {
direction2 = direction2 + 90;
if (direction2 == 360) {
direction2 = 0;
}
} else if (key == 107) {
if (direction2 == 0) {
direction2 = 360;
}
direction2 = direction2 - 90;
} else if (key == 106) {
if (direction1 == 0) {
direction1 = 360;
}
direction1 = direction1 - 90;
} else if (key == 108) {
direction1 = direction1 + 90;
if (direction1 == 360) {
direction1 = 0;
}
} else if (key == 27) {
for (;;) {
system("cls");//清屏
cout << "1.设置出生点" << endl;
cout << "2.退出游戏" << endl;
cout << "3.继续游戏" << endl;
key = _getch(); //读取键盘
if (key == 49) {
system("cls");//清屏
homex = x;
homey = y;
homeh = 26;
break;
} else if (key == 50) {
for (;;) {
system("cls");//清屏
cout << "Y\\N" << endl;
key = _getch(); //读取键盘
if (key == 121) {
system("cls");//清屏
return 0;
} else if (key == 110) {
system("cls");//清屏
break;
} else {
cout << "输入错误";
Sleep(500);//等待0.5秒
system("cls");//清屏
}
}
} else if (key == 51) {
system("cls");//清屏
break;
} else {
cout << "输入错误";
Sleep(500);//等待0.5秒
system("cls");//清屏
}
}
} else if (key == 32) {
h = h + 2;
height = height + 2;
}
Sleep(delay);//延迟
system("cls");//清屏
electricity = electricity - 0.5;
//加血
if (hp01 != hp1) {
hp01++;
}
//攻击次数-1
if (attack > 0) {
attack--;
hp01 = hp01 - 15;
}
//消除Boss
if (attack == 0) {
map[Staging1][Staging2][Staging3] = 0;
}
//死亡
if (hp01 <= 0 || electricity <= 0 || direction2 == 270) {
cout << "Game Over!!!";
Sleep(2000);//等待2秒
system("cls");//清屏
death++;
break;
}
//胜利
if (hp02 <= 0) {
system("cls");//清屏
cout << "你赢了,Game Over" << endl;
cout << "死亡次数:" << death << endl;
cout << "Boss血量为" << hp2;
key = _getch(); //读取键盘
system("cls");//清屏
application6();
return 0;
}
//落下
if (height > 0) {
height--;
h--;
}
}
}
return 0;
}
由于代码连起来有点长,所以留到下期 如有疑问,即可提出