利用C++編寫簡(jiǎn)易寶可夢(mèng)對(duì)戰(zhàn)小游戲
最近想到了用C++寫個(gè)小時(shí)候課間嘴上玩的那種寶可夢(mèng)游戲,先試寫了個(gè)demo,只有早期的三個(gè)寶可夢(mèng)和基礎(chǔ)招式,感興趣的朋友可以再自己添加,也沒有各種物防特攻數(shù)值啥的,等以后會(huì)搞圖形化界面了再做個(gè)復(fù)雜的,功能完善的。
玩法就是選擇寶可夢(mèng)和電腦對(duì)戰(zhàn),除了攻擊還有三次防御的機(jī)會(huì),可以完全抵消對(duì)面這輪的攻擊,增加了一點(diǎn)博弈的感覺(其實(shí)和電腦也沒啥好博弈的)
上代碼
#include<iostream>
#include<windows.h>
#include<ctime>
enum Type {normal,fire,grass,water};//定義招式與pokemon屬性
struct Atk//招式結(jié)構(gòu)體
{
char name[20];//招式名
int damage;//傷害
Type type;//屬性
int speed;//招式速度
int pp; //招式技能點(diǎn)數(shù)量
};
Atk def{"def",0,normal,10,3};//防御
struct pokemon//pokemon結(jié)構(gòu)體
{
char name[30];//名
int hp;//初始血量
int speed;//pokemon本身速度
Type type;//屬性
Atk atk1;//招式1到4
Atk atk2;
Atk atk3;
Atk atk4;
int def;//防御點(diǎn)數(shù)
};
void initial();
int choose(int);
int AIchoose(int);
int p_choose(int,pokemon);
int AI_choose(int,pokemon);
void duel(pokemon p_pokemon,pokemon AI_pokemmon);
double damage_coeff(Type type1,Type type2);
int main()
{
using namespace std;
int cont=1;//設(shè)定循環(huán)游戲的flag
while(cont==1)
{
system("cls");//新游戲開始時(shí)清屏
//初始化各個(gè)招式與pokemon
Atk watergun{"water gun",40,water,2,2};//水槍
Atk quickattack{"quick attack",40,normal,5,3};//電光石火
Atk scratch{"scratch",40,normal,2,8};//抓
Atk bubblebeam{"bubble beam",65,water,1,1};//泡沫光線
Atk razorleaf{"razor leaf",55,grass,3,1};//飛葉快刀
Atk vinewhip{"vine whip",35,grass,4,2};//藤鞭
Atk tackle{"tackle",35,normal,2,10};//撞擊
Atk ember{"ember",40,fire,2,1};//火苗
Atk firespin{"fire spin",35,fire,4,2};//火焰旋渦
pokemon bulbasaur{"bulbasaur",200,3,grass,tackle,quickattack,vinewhip,razorleaf,3};//妙蛙種子
pokemon charmander{"charmander",200,2,fire,scratch,quickattack,ember,firespin,3};//小火龍
pokemon squirtle{"squirtle",200,1,water,tackle,quickattack,watergun,bubblebeam,3};//杰尼龜
initial();
pokemon p_pokemon;
pokemon AI_pokemon;
int Num1,Num2;
Num1=choose(Num1);
Num2=AIchoose(Num2);
//確定雙方選擇的pokemon
switch(Num1)
{
case 1:
p_pokemon=bulbasaur;
break;
case 2:
p_pokemon=charmander;
break;
case 3:
p_pokemon=squirtle;
break;
}
switch(Num2)
{
case 0:
AI_pokemon=bulbasaur;
break;
case 1:
AI_pokemon=charmander;
break;
case 2:
AI_pokemon=squirtle;
break;
}
system("cls");
Sleep(500);
cout <<"You choose "<<p_pokemon.name<<" and your opponent choose "<<AI_pokemon.name<<endl;
duel(p_pokemon,AI_pokemon);
cout <<"If you want to play another round,enter number 1,or enter others number to quit:"<<endl;
cin >>cont;
}
cin.get();
return 0;
}
void initial()//初始化界面
{
using namespace std;
cout <<"Welcome to the pokemon duel!"<<endl;
cout <<"Your pokemon has 3 chances to defend.Once you decide to defend,you will not get damage this turn."<<endl;
cout <<"But also,you can not attack this turn."<<endl;
cout <<"Now choose your pokemon by enter the number:";
cout <<"1.bulbasaur 2.charmander 3.squirtle";
}
int choose(int Num)//玩家選擇pokemon,利用遞歸讓玩家正確輸入
{
using namespace std;
cin >>Num;
if(Num!=1&&Num!=2&&Num!=3)
{
cout <<"You choose a wrong number, choose it again:";
Num=choose(Num);
}
return Num;
}
int AIchoose(int Num)//電腦選擇pokemon,利用隨機(jī)數(shù)讓電腦隨機(jī)選擇
{
using namespace std;
srand((unsigned)time(NULL));
Num = rand() % 3;
return Num;
}
int p_choose(int pchoose,pokemon p_pokemon)//玩家選擇招式,利用遞歸讓玩家正確輸入
{
using namespace std;
cin >>pchoose;
//判斷玩家所選的招式PP是否足夠
if(pchoose==1&&p_pokemon.atk1.pp<=0)
{
cout <<"You don't have enough pp"<<endl;
pchoose=p_choose(pchoose,p_pokemon);
}
if(pchoose==2&&p_pokemon.atk2.pp<=0)
{
cout <<"You don't have enough pp"<<endl;
pchoose=p_choose(pchoose,p_pokemon);
}
if(pchoose==3&&p_pokemon.atk3.pp<=0)
{
cout <<"You don't have enough pp"<<endl;
pchoose=p_choose(pchoose,p_pokemon);
}
if(pchoose==4&&p_pokemon.atk4.pp<=0)
{
cout <<"You don't have enough pp"<<endl;
pchoose=p_choose(pchoose,p_pokemon);
}
if(pchoose==5&&p_pokemon.def<=0)
{
cout <<"You don't have enough pp"<<endl;
pchoose=p_choose(pchoose,p_pokemon);
}
if(pchoose>5||pchoose<=0)
{
cout <<"You choose a wrong number, choose it again:";
pchoose=p_choose(pchoose,p_pokemon);
}
return pchoose;
}
int AI_choose(int AIchoose,pokemon AI_pokemon)//利用隨機(jī)數(shù)讓電腦隨機(jī)選擇
{
using namespace std;
srand((unsigned)time(NULL));
AIchoose = rand() % 5;
//判斷電腦選擇招式的PP是否足夠
if(AIchoose==0&&AI_pokemon.atk1.pp<=0)
{
Sleep(1000);
AIchoose=AI_choose(AIchoose,AI_pokemon);
}
if(AIchoose==1&&AI_pokemon.atk2.pp<=0)
{
Sleep(1000);
AIchoose=AI_choose(AIchoose,AI_pokemon);
}
if(AIchoose==2&&AI_pokemon.atk3.pp<=0)
{
Sleep(1000);
AIchoose=AI_choose(AIchoose,AI_pokemon);
}
if(AIchoose==3&&AI_pokemon.atk4.pp<=0)
{
Sleep(1000);
AIchoose=AI_choose(AIchoose,AI_pokemon);
}
if(AIchoose==4&&AI_pokemon.def<=0)
{
Sleep(1000);
AIchoose=AI_choose(AIchoose,AI_pokemon);
}
return AIchoose;
}
double damage_coeff(Type type1,Type type2)//計(jì)算攻擊時(shí)傷害加成系數(shù),type1為招式屬性,type2為pokemon屬性
{
if(type1==normal)
{
return 1;
}
if(type1==grass)
{
switch(type2)
{
case grass:
return 0.5;
break;
case fire:
return 0.5;
break;
case water:
return 2;
break;
}
}
if(type1==fire)
{
switch(type2)
{
case grass:
return 2;
break;
case fire:
return 0.5;
break;
case water:
return 0.5;
break;
}
}
if(type1==water)
{
switch(type2)
{
case grass:
return 0.5;
break;
case fire:
return 2;
break;
case water:
return 0.5;
break;
}
}
}
void duel(pokemon p_pokemon,pokemon AI_pokemmon)//戰(zhàn)斗流程函數(shù)
{
using namespace std;
while(p_pokemon.hp>0&&AI_pokemmon.hp>0)//雙方HP均大于0時(shí)繼續(xù)戰(zhàn)斗
{
//顯示雙方相關(guān)信息
cout <<"Your HP:"<<p_pokemon.hp<<" Your opponent's HP:"<<AI_pokemmon.hp<<endl<<endl;
cout <<"Choose your action this turn by ernter the number:"<<endl;
cout <<"1."<<p_pokemon.atk1.name<<" PP:"<<p_pokemon.atk1.pp<<endl;
cout <<"2."<<p_pokemon.atk2.name<<" PP:"<<p_pokemon.atk2.pp<<endl;
cout <<"3."<<p_pokemon.atk3.name<<" PP:"<<p_pokemon.atk3.pp<<endl;
cout <<"4."<<p_pokemon.atk4.name<<" PP:"<<p_pokemon.atk4.pp<<endl;
cout <<"5.deffense PP:"<<p_pokemon.def;
Atk this_turn_p_atk;//定義玩家本輪所選招式
Atk this_turn_AI_atk;//定義電腦本輪所選招式
int pchoose;
pchoose=p_choose(pchoose,p_pokemon);
int AIchoose;
AIchoose=AI_choose(AIchoose,AI_pokemmon);
switch(pchoose)
{
case 1:
p_pokemon.atk1.pp=p_pokemon.atk1.pp-1;
this_turn_p_atk=p_pokemon.atk1;
break;
case 2:
p_pokemon.atk2.pp=p_pokemon.atk2.pp-1;
this_turn_p_atk=p_pokemon.atk2;
break;
case 3:
p_pokemon.atk3.pp=p_pokemon.atk3.pp-1;
this_turn_p_atk=p_pokemon.atk3;
break;
case 4:
p_pokemon.atk4.pp=p_pokemon.atk4.pp-1;
this_turn_p_atk=p_pokemon.atk4;
break;
case 5:
p_pokemon.def=p_pokemon.def-1;
this_turn_p_atk=def;
break;
}
switch(AIchoose)
{
case 0:
AI_pokemmon.atk1.pp=AI_pokemmon.atk1.pp-1;
this_turn_AI_atk=AI_pokemmon.atk1;
break;
case 1:
AI_pokemmon.atk2.pp=AI_pokemmon.atk2.pp-1;
this_turn_AI_atk=AI_pokemmon.atk2;
break;
case 2:
AI_pokemmon.atk3.pp=AI_pokemmon.atk3.pp-1;
this_turn_AI_atk=AI_pokemmon.atk3;
break;
case 3:
AI_pokemmon.atk4.pp=AI_pokemmon.atk4.pp-1;
this_turn_AI_atk=AI_pokemmon.atk4;
break;
case 4:
AI_pokemmon.def=AI_pokemmon.def-1;
this_turn_AI_atk=def;
break;
}
system("cls");
//判斷是否有玩家本輪采取防御
if(AIchoose==4&&pchoose!=5)
{
cout <<"You use "<<this_turn_p_atk.name<<".";
Sleep(500);
cout <<"But your attack was defended!"<<endl;
}
else if(pchoose==5&&AIchoose!=4)
{
cout <<"Your opponent use "<<this_turn_AI_atk.name<<".";
Sleep(500);
cout <<"But you defend the attack from the opponent!"<<endl;
}
else if(pchoose==5&&AIchoose==4)
{
cout <<"You both defend this turn."<<endl;
Sleep(250);
}
else//雙方都沒有防御的情況
{
int p_speed,AI_speed;
double p_coeff,AI_coeff;
//真正的速度為pokemon本身速度加招式速度
p_speed=p_pokemon.speed+this_turn_p_atk.speed;//本輪玩家真正的速度
AI_speed=AI_pokemmon.speed+this_turn_AI_atk.speed;//本輪電腦真正的速度
//計(jì)算傷害系數(shù)
p_coeff=damage_coeff(this_turn_p_atk.type,AI_pokemmon.type);
AI_coeff=damage_coeff(this_turn_AI_atk.type,p_pokemon.type);
//比較雙方誰(shuí)本輪速度快,判斷先手
if(AI_speed>p_speed)
{
cout <<"Your opponent use "<<this_turn_AI_atk.name<<" !"<<endl;
Sleep(500);
p_pokemon.hp=p_pokemon.hp-this_turn_AI_atk.damage*AI_coeff;//計(jì)算收到的傷害,傷害為招式傷害乘以傷害系數(shù)
if(p_pokemon.hp<=0)
{
cout <<"You have been defeated!"<<endl;
}
else if(p_pokemon.hp>0)
{
cout <<"You use "<<this_turn_p_atk.name<<" !"<<endl;
Sleep(500);
AI_pokemmon.hp=AI_pokemmon.hp-this_turn_p_atk.damage*p_coeff;
if(AI_pokemmon.hp<=0)
{
cout <<"You win!"<<endl;
}
}
}
else if(AI_speed<p_speed)
{
cout <<"You use "<<this_turn_p_atk.name<<" !"<<endl;
Sleep(500);
AI_pokemmon.hp=AI_pokemmon.hp-this_turn_p_atk.damage*p_coeff;
if(AI_pokemmon.hp<=0)
{
cout <<"You win!"<<endl;
}
else
{
cout <<"Your opponent use "<<this_turn_AI_atk.name<<" !"<<endl;
Sleep(500);
p_pokemon.hp=p_pokemon.hp-this_turn_AI_atk.damage*AI_coeff;
if(p_pokemon.hp<=0)
{
cout <<"You have been defeated!"<<endl;
}
}
}
else if(AI_speed==p_speed)
{
cout <<"You both attack at the same time!"<<endl;
Sleep(500);
cout <<"You use "<<this_turn_p_atk.name<<" !"<<endl;
Sleep(500);
cout <<" Your opponent use "<<this_turn_AI_atk.name<<" !"<<endl;
Sleep(500);
p_pokemon.hp=p_pokemon.hp-this_turn_AI_atk.damage*AI_coeff;
AI_pokemmon.hp=AI_pokemmon.hp-this_turn_p_atk.damage*p_coeff;
if(AI_pokemmon.hp<=0&&p_pokemon.hp>0)
{
cout <<"You win!"<<endl;
}
else if(AI_pokemmon.hp>0&&p_pokemon.hp<=0)
{
cout <<"You have been defeated!"<<endl;
}
else if(AI_pokemmon.hp<=0&&p_pokemon.hp<=0)
{
cout <<"Draw game!";
}
}
}
}
}以上就是利用C++編寫簡(jiǎn)易寶可夢(mèng)對(duì)戰(zhàn)小游戲的詳細(xì)內(nèi)容,更多關(guān)于C++寶可夢(mèng)對(duì)戰(zhàn)游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)貪吃蛇游戲設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)貪吃蛇游戲設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
C++連接mysql的方法(直接調(diào)用C-API)
首先安裝mysql,點(diǎn)完全安裝,才能在在安裝目錄include找到相應(yīng)的頭文件,注意,是完全安裝,需要的朋友可以參考下2017-06-06
C++實(shí)現(xiàn)LeetCode(124.求二叉樹的最大路徑和)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(124.求二叉樹的最大路徑和),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++實(shí)現(xiàn)轉(zhuǎn)置矩陣的循環(huán)
大家好,本篇文章主要講的是C++實(shí)現(xiàn)轉(zhuǎn)置矩陣的循環(huán),感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01
一起來學(xué)習(xí)C++中remove與erase的理解
這篇文章主要為大家詳細(xì)介紹了C++的remove與erase,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
C語(yǔ)言驅(qū)動(dòng)開發(fā)內(nèi)核枚舉IoTimer定時(shí)器解析
這篇文章主要為大家介紹了C語(yǔ)言驅(qū)動(dòng)開發(fā)內(nèi)核枚舉IoTimer定時(shí)器解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
DHCP:解析開發(fā)板上動(dòng)態(tài)獲取ip的2種實(shí)現(xiàn)方法詳解
本篇文章是對(duì)開發(fā)板上動(dòng)態(tài)獲取ip的2種實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++?Boost?ProgramOptions超詳細(xì)講解
Boost是為C++語(yǔ)言標(biāo)準(zhǔn)庫(kù)提供擴(kuò)展的一些C++程序庫(kù)的總稱。Boost庫(kù)是一個(gè)可移植、提供源代碼的C++庫(kù),作為標(biāo)準(zhǔn)庫(kù)的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語(yǔ)言標(biāo)準(zhǔn)庫(kù)提供擴(kuò)展的一些C++程序庫(kù)的總稱2022-11-11

