1.法术对战
#include<iostream>
using namespace std;
int main()
{
int yhp=444;
int chp=2000;
int aaa;
int lan=2000;
int du=0;
int tuy=0;
for(int i=1;i<=9999;i++)
{
cout<<"你的血量:" <<yhp<<endl<<"电脑血量:"<<chp<<endl<<"剩余法术值:"<<lan<<endl<<"毒素层数:"<<du<<endl<<"护盾值:"<<tuy<<endl;
cout<<"1、普攻:恢复5生命,恢复25法术值,造成10点伤害"<<endl;
cout<<"2、恢复:恢复25(增强后30)生命,消耗10法术值,造成0点伤害"<<endl;
cout<<"3、火球术:恢复3生命,消耗33法术值,造成66点伤害"<<endl;
cout<<"4、剧毒:恢复10(削弱后0)生命,消耗44法术值,造成50(削弱后25)点伤害,毒素伤害每回合造成5(削弱后4)伤害,可叠加"<<endl;
cout<<"5、土元素护盾:恢复15生命,消耗55法术值,护盾生命为自身生命值的50%"<<endl;
cout<<"6、召唤物攻击:恢复5生命,消耗50法术值,造成75点伤害"<<endl;
cout<<"7、恢复法术值:恢复15生命,恢复66法术值,造成0点伤害"<<endl;
cin>>aaa;
if(aaa==1)
{
yhp+=5;
chp-=10;
lan+=25;
}
if(aaa==2)
{
yhp+=30;
lan-=10;
}
if(aaa==3)
{
yhp+=3;
lan-=33;
chp-=66;
}
if(aaa==4)
{
lan-=44;
chp-=25;
du++;
}
if(aaa==5)
{
yhp+=15;
lan-=55;
tuy+=yhp/2;
}
if(aaa==6)
{
yhp+=5;
lan-=50;
chp-=75;
}
if(aaa==7)
{
yhp+=15;
lan+=66;
}
chp-=du*4;
//电脑攻击
if(tuy==0)
{
yhp-=50;
}
else if(tuy<50)
{
yhp-=50-tuy;
tuy=0;
}
else
{
tuy-=50;
}
if(yhp<=0)
{
cout<<"你输了!";
break;
}
if(chp<=0)
{
cout<<"你赢了!";
break;
}
}
return 0;
}
2.数字华容道
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <termio.h>
#include <iomanip>
#define clear() cout << "\033c" << flush
using namespace std;
int num[10][10];
int rd, tme;
bool validMove;
int getch() // 不回显函数
{
struct termios nts, ots;
// 得到当前终端(0表示标准输入)的设置
if (tcgetattr(0, &ots) < 0) return EOF;
// 设置终端为Raw原始模式,该模式下所有的输入数据以字节为单位被处理
nts = ots;
cfmakeraw(&nts);
// 设置上更改之后的设置
if (tcsetattr(0, TCSANOW, &nts) < 0) return EOF;
// 设置还原成老的模式
int cr;
cr = getchar();
if (tcsetattr(0, TCSANOW, &ots) < 0) return EOF;
return cr;
}
void intro() // 游戏规则展示
{
cout << "***********************************************************" << endl;
cout << "欢迎来到 数字华容道 游戏~" << endl;
cout << "在这个游戏里会有一个 4*4 的表格,每个网格中有一个1~15之间的数" << endl;
cout << "可以选择 w、s、a、d,上下左右的滑动,移动数" << endl;
cout << "当按大小顺序从左到右、从上到下排列好所有的数,游戏结束!" << endl;
cout << "点击【ENTER】开始游戏" << endl;
cout << "点击【ESC】退出游戏" << endl;
cout << "***********************************************************" << endl;
}
void init() // 初始化网格
{
int a[20] = {};
for (int i = 1; i <= 15; i++)
{
a[i] = i;
}
srand(time(0));
random_shuffle(a + 1, a + 15 + 1);
int cnt = 0;
for (int i = 1; i <= 15; i++)
{
for (int j = i+1; j <= 15; j++)
{
if (a[i] > a[j]) // 逆序数对个数
{
cnt++;
}
}
}
if (cnt % 2 == 1) // 逆序数对为奇数无解,减少一个
{
for (int i = 1; i <= 14; i++)
{
if (a[i] > a[i+1])
{
swap(a[i], a[i+1]);
break;
}
}
}
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 4; j++)
{
num[i][j] = a[(i-1)*4+j];
}
}
}
void drawBoard() // 打印当前表格
{
// 输出上边框
cout << " ╔";
for (int i = 1; i <= 4 - 1; i++) cout << "════╤";
cout << "════╗\n";
// 输出中间部分
for (int i = 1; i <= 4; i++) // 行
{
cout << " ║";
for (int j = 1; j <= 4; j++) // 列
{
if (num[i][j] >= 10)
cout << " " << num[i][j] << " ";
else if (num[i][j] != 0)
cout << " " << num[i][j] << " ";
else
cout << " ";
if (j != 4)
cout << "│";
else
cout << "║";
}
cout << " \n";
// 输出下边框
if (i != 4)
{
cout << " ╟";
for (int i = 1; i <= 4 - 1; i++) cout << "────┼";
cout << "────╢\n";
}
else
{
cout << " ╚";
for (int i = 1; i <= 4 - 1; i++) cout << "════╧";
cout << "════╝\n";
}
}
}
bool gameOver()
{
int a[20];
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 4; j++)
{
a[(i-1)*4+j] = num[i][j];
}
}
for (int i = 1; i <= 15; i++)
{
if (a[i] != i)
{
return false;
}
}
return true;
}
void MoveUp() // 向上移动
{
int x, y;
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 4; j++)
{
if (num[i][j] == 0)
{
x = i;
y = j;
}
}
}
if (x == 4) validMove = false;
else
{
validMove = true;
swap(num[x][y], num[x+1][y]);
}
}
void MoveDown() // 向下移动
{
int x, y;
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 4; j++)
{
if (num[i][j] == 0)
{
x = i;
y = j;
}
}
}
if (x == 1) validMove = false;
else
{
validMove = true;
swap(num[x][y], num[x-1][y]);
}
}
void MoveLeft() // 向左移动
{
int x, y;
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 4; j++)
{
if (num[i][j] == 0)
{
x = i;
y = j;
}
}
}
if (y == 4) validMove = false;
else
{
validMove = true;
swap(num[x][y], num[x][y+1]);
}
}
void MoveRight() // 向右移动
{
int x, y;
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 4; j++)
{
if (num[i][j] == 0)
{
x = i;
y = j;
}
}
}
if (y == 1) validMove = false;
else
{
validMove = true;
swap(num[x][y], num[x][y-1]);
}
}
void Win() // 玩家获胜
{
cout << "**********************************" << endl;
cout << "——————————— ROUND:" << rd << " ————————————" << endl;
cout << "——————————— TIME:" << tme << " s———————————" << endl;
cout << "——————————— 玩家获胜! ————————————" << endl;
cout << "**********************************" << endl << endl;
exit(0);
}
void game() // 开始游戏
{
cout << "\033c" << flush;
cout << "**********************************" << endl;
cout << "—————————— Ready Go~ ————————————" << endl;
cout << "**********************************" << endl;
drawBoard(); // 打印棋盘
char ch = 0;
int sta = time(0);
while (!gameOver()) // 当游戏没结束时
{
tme = time(0) - sta;
// 按键 操作数 时间
cout << "Key:" << ch << " Round:" << rd << " Time:" << tme << endl;
cout << "Tips:" << endl;
cout << "w —— 上移" << endl;
cout << "s —— 下移" << endl;
cout << "a —— 左移" << endl;
cout << "d —— 右移" << endl;
cout << "e —— 退出游戏" << endl;
cout << "r —— 开始新游戏" << endl;
cout << "选择以上指令!" << endl;
ch = getch(); // 输入操作指令
if (ch == 'w') MoveUp();
else if (ch == 's') MoveDown();
else if (ch == 'a') MoveLeft();
else if (ch == 'd') MoveRight();
else if (ch == 'e') exit(0);
else if (ch == 'r') // 开始新游戏
{
sta = time(0); // 清空开始时间
rd = 0; // 清空操作次数
memset(num, 0, sizeof(num)); // 清空表格数据
return;
}
else
{
cout << endl << "———————输入有误,请重新输入指令!———————" << endl << endl;
continue;
}
cout << "\033c" << flush;
drawBoard(); // 打印棋盘
if (validMove) rd++; // 记录操作次数
}
Win();
}
int main()
{
while (true)
{
intro(); // 游戏规则展示
char ch = getch(); // 不回显函数,当用户按下某个字符时,函数自动读取,无需按回车
if (ch == 13) // CR回车的ascii是13
{
init(); //初始化方格数据
game(); // 开始游戏
}
else if (ch == 27) // ESC的ascii是27
{
cout << "退出游戏!" << endl;
break;
}
else cout << "输入有误,请再次输入!" << endl;
}
return 0;
}
3.怪物猎人
#include <iostream>
using namespace std;
double shengmingli=2000;//定义主角初始生命力
int gongjili=150;//定义主角初始攻击力
int fangyuli=200;//定义主角初始防御力
int money=20;//定义主角初始金钱数量
bool guoguan;//定义是否通关判定
void wuqidian();//定义武器店函数
void yaodian();//定义药店函数
void guaiwu1();//定义小怪物函数
void guaiwu2();//定义大怪物函数
int main()
{
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
int xiaozhen;//定义选择项目
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
cin>>xiaozhen;
while(xiaozhen!=5)//输入5时退出游戏
{
if(shengmingli<=0)//主角生命力小于等于0时游戏结束
{
cout<<"你死啦!"<<endl;
break;
}
if(guoguan)
{
cout<<"恭喜通关!"<<endl;
break;
}
if(xiaozhen==6)//输入6可检测自己的状态
{
cout<<"你的生命力:"<<shengmingli<<endl;
cout<<"你的攻击力:"<<gongjili<<endl;
cout<<"你的防御力:"<<fangyuli<<endl;
cout<<"你拥有的钱:"<<money<<endl;
}
else
switch(xiaozhen)
{
case 1 : wuqidian();break;
case 2 : yaodian();break;
case 3 : guaiwu1();break;
case 4 : guaiwu2();break;
default : cout<<"请不要乱选!"<<endl;break;
}
cin>>xiaozhen;
}
if(xiaozhen==5)
{
cout<<"正在退出游戏……"<<endl;
}
cin.get();
cin.get();
return 0;
}
void wuqidian()
{
cout<<"欢迎来到武器店!"<<endl;
cout<<"1、买小刀(1M加2攻击力)"<<endl;
cout<<"2、买短剑(2M加20攻击力)"<<endl;
cout<<"3、买大砍刀(5M加40攻击力)"<<endl;
cout<<"4、买双节棍(7M加60攻击力)"<<endl;
cout<<"5、买盾牌(2M加30防御力)"<<endl;
cout<<"6、买铠甲(5M加60防御力)"<<endl;
cout<<"7、离开武器店"<<endl;
int wuqidian;
cin>>wuqidian;
while(wuqidian!=7)//输入7时结束函数
{
switch(wuqidian)
{
case 1 : if(money<10)
cout<<"你的钱不够"<<endl;//钱不够时返回Flase
else
cout<<"购买成功!"<<endl;//钱足够时返回True
gongjili+=2;
money-=1;
break;
case 2 : if(money<80)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
gongjili+=20;
money-=80;
break;
case 3 : if(money<140)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
gongjili+=40;
money-=140;
break;
case 4 : if(money<200)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
gongjili+=60;
money-=200;
break;
case 5 : if(money<60)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
fangyuli+=30;
money-=60;
break;
fangyuli+=60;
money-=100;
break;
default : cout<<"无"<<endl;
break;
}
cin>>wuqidian;
}
if(wuqidian==7)
{ //返回main()主函数
cout<<"欢迎下次再来!"<<endl;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
}
/*
yaodian()的设置与wuqidian()相同,可参照阅读.
*/
void yaodian()
{
cout<<"欢迎来到药品店!"<<endl;
cout<<"1、买1号补血药(10M加200生命)"<<endl;
cout<<"2、买2号补血药(50M加1000生命力)"<<endl;
cout<<"3、买3号补血药(100M加2200生命力)"<<endl;
cout<<"4、离开武器店"<<endl;
int yaodian;
cin>>yaodian;
while(yaodian!=4)
{
switch(yaodian)
{
case 1 : if(money<10)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
shengmingli+=200;
money-=10;
break;
case 2 : if(money<50)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
shengmingli+=1000;
money-=50;
break;
case 3 : if(money<100)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
shengmingli+=2200;
money-=100;
break;
default : cout<<"无"<<endl;
break;
}
cin>>yaodian;
}
if(yaodian==4)
{
cout<<"欢迎下次再来!"<<endl;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
}
/*这里是两个战斗函数,使用指针来处理.避免造成内存崩溃.*/
void guaiwu1()
{
cout<<"开始与小怪物战斗!!!"<<endl;
double* g_shengmingli=new double;//定义怪物生命
int* g_gongjili=new int;//定义怪物攻击力
int* g_fangyuli=new int;//定义怪物防御力
int* g_money=new int;//定义怪物金钱
*g_shengmingli=100;
*g_gongjili=5;
*g_fangyuli=3;
*g_money=5;
double* tongji1=new double;//用来计算主角对怪物的杀伤
double* tongji2=new double;//用来计算怪物对主角的杀伤
*tongji1=0;
*tongji2=0;
int* huihe=new int;//定义回合数
*huihe=1;
cout<<"你开始对小怪物进行攻击!"<<endl;
int* xuanze=new int;
/*
攻击计算公式
杀伤=攻击力*2-防御力
玩家每回合可以选择攻击与逃跑
*/
while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2)
{
cout<<"现在是"<<"第"<<*huihe<<"回合!"<<endl;
cout<<"请选择你的动作:\n";
cout<<"1、攻击\n2、逃跑\n";
cin>>*xuanze;
switch((*xuanze))
{
case 1 : cout<<"你对小怪物发动了攻击!"<<endl;
*g_shengmingli-=gongjili*2-(*g_fangyuli);
*tongji1=gongjili*2-(*g_fangyuli);
cout<<"你打掉了小怪物"<<*tongji1<<"的生命!"<<endl;
cout<<"小怪物还剩"<<(*g_shengmingli)-(*tongji1)<<"点生命"<<endl;
shengmingli-=(*g_gongjili)*2-fangyuli;
*tongji2=(*g_gongjili)*2-fangyuli;
cout<<"小怪物对你发动了攻击!"<<endl;
cout<<"小怪物打掉了你"<<*tongji2<<"的生命!"<<endl;
cout<<"你还剩"<<shengmingli-(*tongji2)<<"点生命"<<endl;break;
case 2 : cout<<"你决定逃跑!"<<endl;
cout<<"逃跑成功!"<<endl;continue;
default : cout<<"请不要乱选!"<<endl;
}
(*huihe)++;
}
if((*g_shengmingli)<=0)
{//杀死怪物后的返回
cout<<"小怪物被你杀死了!你真厉害!!!"<<endl;
money+=(*g_money);
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
else
if(shengmingli<=0)
{//被怪物杀死后的返回
cout<<"你被小怪物杀死了!游戏结束!!!"<<endl;
}
else
if((*xuanze)==2)
{//逃跑的返回
cout<<"你逃回了小镇!"<<endl;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
delete g_shengmingli;
delete g_gongjili;
delete g_fangyuli;
delete g_money;
delete tongji1;
delete tongji2;
}
/*
设置均与void函数guaiwu1()相同,可参照上例阅读.
*/
void guaiwu2()
{
cout<<"开始与大怪物战斗!!!"<<endl;
double* g_shengmingli=new double;
int* g_gongjili=new int;
int* g_fangyuli=new int;
*g_shengmingli=3600;
*g_gongjili=500;
*g_fangyuli=500;
double* tongji1=new double;
double* tongji2=new double;
*tongji1=0;
*tongji2=0;
int* huihe=new int;
*huihe=1;
cout<<"你开始对大怪物进行攻击!"<<endl;
int* xuanze=new int;
while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2)
{
cout<<"现在是"<<"第"<<*huihe<<"回合!"<<endl;
cout<<"请选择你的动作:\n";
cout<<"1、攻击\n2、逃跑\n";
cin>>*xuanze;
switch((*xuanze))
{
case 1 : cout<<"你对大怪物发动了攻击!"<<endl;
*g_shengmingli-=gongjili*2-(*g_fangyuli);
*tongji1=gongjili*2-(*g_fangyuli);
cout<<"你打掉了大怪物"<<*tongji1<<"的生命!"<<endl;
cout<<"大怪物还剩"<<(*g_shengmingli)-(*tongji1)<<"点生命"<<endl;
shengmingli-=(*g_gongjili)*2-fangyuli;
*tongji2=(*g_gongjili)*2-fangyuli;
cout<<"大怪物对你发动了攻击!"<<endl;
cout<<"大怪物打掉了你"<<*tongji2<<"的生命!"<<endl;
cout<<"你还剩"<<shengmingli-(*tongji2)<<"点生命"<<endl;break;
case 2 : cout<<"你决定逃跑!"<<endl;
cout<<"逃跑成功!"<<endl;continue;
default : cout<<"请不要乱选!"<<endl;
}
(*huihe)++;
}
if((*g_shengmingli)<=0)
{
cout<<"大怪物被你杀死了!你真厉害!!!"<<endl;
guoguan=true;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
else
if(shengmingli<=0)
{
cout<<"你被大怪物杀死了!游戏结束!!!"<<endl;
}
else
if((*xuanze)==2)
{
cout<<"你逃回了小镇!"<<endl;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
delete g_shengmingli;
delete g_gongjili;
delete g_fangyuli;
delete tongji1;
delete tongji2;
}
标签:lan,分享,aaa,游戏,int,c++,yhp,include,cout From: https://blog.csdn.net/2401_88851881/article/details/143663276