#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <stdio.h>
#include <cstdlib>
#include <cstdio>
#include <thread>
#include<conio.h>
#include<windows.h>
using namespace std;
#define Sleep(x) this_thread::sleep_for(chrono::milliseconds(x))
char ch[1000][1000];
int cnt = 0;
bool f = false;
struct player
{ // 玩家
string nm, job;
int hart, attack, wh;
string warehouse[1000000];
string thing;
};
struct enemy
{ // 怪
string nm, thing;
int hart = 100, attack; // 攻击力
bool die = false;
};
player player1;
int x = 2, y = 2;
// char getch()
// { // 输入(IOS
// char ch;
// cin >> ch;
// return ch;
// }
void erase(int n)
{
for (int i = n; i < player1.wh; i++)
{
player1.warehouse[i] = player1.warehouse[i + 1];
}
player1.wh --;
return;
}
void warehouse()
{
for (int i = 1; i <= player1.wh; i++)
{
cout << i << ". " << player1.warehouse[i]<< endl;
}
cout << endl<< "按0关闭" << endl<< "按序号选中物品" << endl<<"按-1删除元素";
int a;
cin >> a;
if (a == 0)
{
system("cls");
return;
}
if(a == -1){
cout<<"按序号删除元素";
cin>>a;
erase(a);
}
else if (a > 0 && player1.warehouse[a] == "回血卡")
{
if (player1.hart >= 50)
player1.hart = 100;
else if (player1.hart < 50)
player1.hart += 50;
erase(a);
}
else if (a > 0 && player1.warehouse[a] != "回血卡")
{
if(f)
{
swap(player1.thing,player1.warehouse[a]);
}
else
{
player1.thing = player1.warehouse[a];
erase(a);
f = true;
}
}
cout << "你现在有" << player1.hart << "滴血" << endl;
return;
}
void map()
{ // 地图
for (int i = 1; i <= 100; i++)
{
ch[1][i] = '#';
}
srand(time(0));
for (int i = 2; i <= 49; i++)
{
ch[i][1] = '#';
for (int j = 2; j <= 99; j++)
{
int n = rand() % 100;
if (n <= 15){
cnt++;
ch[i][j] = '!';
}
else if (n <= 40)
ch[i][j] = '#';
else
ch[i][j] = ' ';
}
ch[i][100] = '#';
}
for (int i = 1; i <= 100; i++)
{
ch[50][i] = '#';
}
ch[x][y] = 'X';
}
void fight()
{ // 打怪
srand(time(0));
// int n = rand() % 11 + 90;
enemy enemy1;
// enemy1.attack = n;
int n2 = 0;
if(player1.thing == "铁剑") n2 = 10;
else if(player1.thing == "木剑") n2 = 5;
else if(player1.thing == "金剑") n2 = 13;
else if(player1.thing == "弓箭") n2 = 17;
else if(player1.thing == "匕首") n2 = 7;
else if(player1.thing == "手枪") n2 = 20;
else if(player1.thing == "步枪") n2 = 30;
while (1)
{
int n = rand() % 3 + n2;
enemy1.attack = rand() % 10;
player1.attack = rand() % 20;
cout << "你有" << player1.hart << "滴血" << endl;
cout << "对方有" << enemy1.hart << "滴血" << endl;
Sleep(1000);
player1.hart -= enemy1.attack;
enemy1.hart -= player1.attack + n;
if (player1.hart <= 0)
break;
if (enemy1.hart <= 0){
cnt--;
break;
}
}
int thingcnt = rand() % 10 + 1;
for (int i = 0; i <= thingcnt; i++)
{
int th = rand() % 100;
player1.wh++;
if (th < 5)
player1.wh--;
else if (th < 15)
{
player1.warehouse[player1.wh] = "铁剑";
}
else if (th < 30)
{
player1.warehouse[player1.wh] = "木剑";
}
else if (th < 35)
{
player1.warehouse[player1.wh] = "金剑";
}
else if (th < 50)
{
player1.warehouse[player1.wh] = "弓箭";
}
else if (th < 60)
{
player1.warehouse[player1.wh] = "手枪";
}
else if (th < 68)
{
player1.warehouse[player1.wh] = "匕首";
}
else if (th < 75)
{
player1.warehouse[player1.wh] = "步枪";
}
else
{
player1.warehouse[player1.wh] = "回血卡";
}
}
return;
}
void play()
{ // 移动
char c;
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 100; j++)
{
cout << ch[i][j];
}
cout << endl;
}
while (1)
{
c = getch();
system("cls");
ch[x][y] = ' ';
if (c == 'w' && ch[x - 1][y] != '#')
{
if (ch[x - 1][y] == '!')
fight();
x--;
}
else if (c == 's' && ch[x + 1][y] != '#')
{
if (ch[x + 1][y] == '!')
fight();
x++;
}
else if (c == 'a' && ch[x][y - 1] != '#')
{
if (ch[x][y - 1] == '!')
fight();
y--;
}
else if (c == 'd' && ch[x][y + 1] != '#')
{
if (ch[x][y + 1] == '!')
fight();
y++;
}
else if (c == 'e')
{
warehouse();
}
if(cnt == 0){
system("cls");
cout<<"你赢了";
break;
}
if (player1.hart <= 0)
{
system("cls");
cout << "你输了";
break;
}
cout << "武器: " << player1.thing << " " << "血量: " << player1.hart << endl;
ch[x][y] = 'X';
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 100; j++)
{
cout << ch[i][j];
}
cout << endl;
}
}
}
int main()
{
player1.thing = "无";
player1.hart = 100;
map();
play();
return 0;
}
苹果系统把#include<conio.h>#include<winbdows.h>删了,把
// char getch()
// { // 输入(IOS
// char ch;
// cin >> ch;
// return ch;
// }取消注释