C++ ちょっと今作れるものを作ってお勉強① 書いて覚える初心者自己中記事
コマンドでひたすら敵と戦ってレベル上げていく不毛のゲームができた。
Source.cpp
void main() { srand((unsigned)time(NULL)); Status player; player.LevelUp(1); while (player.Battle(player)) { //何もしない }; system("pause"); }
class.h
const int SLEEPTIME = 850; class Status { public: Status(); //コンストラクタ void LevelUp(int up); void Show(); int ShowLevel(); void SetExp(int exp); void MpDown(int mp); void ReHp(int mp); void Exp(const Status& player); int Getexp(); bool Dmage(Status& target, int dmage); int DmageCal(Status& target, int c); bool Commond(Status& target); bool EnemyCommond(Status& player, Status& enemy); int GetCom(); bool GetBflag(); void SetBflag(bool b); void BSt(Status& target); void BStEnemy(); bool Battle(Status& player); private: int StUp(int up, int base = 0); protected: int HitPoint; int MaxHitPoint; int MagicPoint; int MaxMagicPoint; int Atack; int Magic; int Speed; int Defence; int MagicDefence; int Avoidance; //回避 int Luck; //運 int Level; //現在のレベル int Experience; //経験値 int NextExperience; int Com; bool Bflag; };
class.cpp
Status::Status() { HitPoint = 0; MaxHitPoint = 0; MagicPoint = 0; MaxMagicPoint = 0; Atack = 0; Magic = 0; Speed = 0; Defence = 0; MagicDefence = 0; Avoidance = 0; //回避 Luck = 0; Level = 0; //現在のレベル Experience = 0; //経験値 NextExperience = 0; } int Status::StUp(int up, int base){ //パラメータ上昇量の計算 int upst(0); for (int i = 0; i < up; ++i) { upst += rand() % 5 + 1 + base; } return upst; } void Status::LevelUp(int up) { //パラメータ上昇値の代入 MaxHitPoint += StUp(up,10); HitPoint = MaxHitPoint; MaxMagicPoint += StUp(up, 5); MagicPoint = MaxMagicPoint; Atack += StUp(up); Magic += StUp(up); Speed += StUp(up); Defence += StUp(up); MagicDefence += StUp(up); Avoidance += StUp(up); Luck += StUp(up); Level += up; NextExperience += Level * 100; } void Status::SetExp(int exp) { //playerに経験値代入 Experience += exp; while(Experience >= NextExperience) { LevelUp(1); Sleep(SLEEPTIME); printf("レベルが %d に上がった!!\n", Level); Sleep(SLEEPTIME); }; } void Status::MpDown(int mp) { //魔法攻撃で消費するMPをMagicPointから減少させる MagicPoint -= mp; Sleep(SLEEPTIME); printf("MPを %d 消費した\n", mp); } void Status::ReHp(int mp) { //回復 MagicPoint -= mp; Sleep(SLEEPTIME); printf("MPを %d 消費した\n", mp); int re = rand() % 10 + Magic * 7; HitPoint += re; if (HitPoint > MaxHitPoint) { re -= HitPoint - MaxHitPoint; HitPoint = MaxHitPoint; } Sleep(SLEEPTIME); std::cout << "HPが" << re << " 回復した" << std::endl; Sleep(SLEEPTIME); } void Status::Show() { //ステータス確認 printf("Level %d\nHP %d / %d\nMP %d / %d\n物理 %d\n魔力 %d\n速さ %d\n防御 %d\n魔防 %d\n回避 %d\n幸運 %d\n経験値 %d / %d\n",Level, HitPoint, MaxHitPoint, MagicPoint, MaxMagicPoint, Atack, Magic, Speed, Defence, MagicDefence, Avoidance, Luck, Experience, NextExperience); } int Status::ShowLevel() { //Level値取得 int n = Level + rand() % 4 - 2; if (n <= 0) { return 1; } return n; } void Status::Exp(const Status& player) { //敵の持つ経験値計算と代入 int enemyst; int playerst; enemyst = Level + MaxHitPoint / 10 + MaxMagicPoint / 10 + Atack + Magic + Speed + Defence + MagicDefence + Avoidance + Luck; playerst = player.Level + player.MaxHitPoint / 10 + player.MaxMagicPoint / 10 + player.Atack + player.Magic + player.Speed + player.Defence + player.MagicDefence + player.Avoidance + player.Luck; Experience = enemyst * 2 - playerst; if (Experience < 5) { Experience = 5; } Experience += 10; //Experience += 10000; } int Status::Getexp() { //敵の持つ経験値を取得 return Experience; } bool Status::GetBflag() { return Bflag; } void Status::SetBflag(bool b) { Bflag = b; } bool Status::Dmage(Status& target, int dmage) { //引数に与ダメいれてHP減少 if (target.HitPoint > dmage) { target.HitPoint -= dmage; return true; } else { target.HitPoint = 0; //勝利フラグ Bflag = true; return false; } } int Status::DmageCal(Status& target, int c) { //targetに与えるダメージ計算 int at = Atack; int lu = Luck; int le = Level; int dm; switch (c) { case 1: dm = (le * 2 / 5 + 2) * at * at * (at / le * 2) / target.Defence / 50 + 2; dm += rand() % 10; break; case 2: dm = (le * 2 / 5 + 2) * at * at * (at / le * 2) / target.MagicDefence / 50 + 2; dm += rand() % 10; break; case 11: dm = (le * 2 / 5 + 2) * at * at * (at / le * 2) / target.Defence / 50 + 2; dm += rand() % 10; dm = dm / 2; break; case 22: dm = (le * 2 / 5 + 2) * at * at * (at / le * 2) / target.MagicDefence / 50 + 2; dm += rand() % 10; dm = dm / 2; break; } Sleep(SLEEPTIME); if ((lu + 10 - target.Luck) / 10 >= rand() % 10 + 1) { std::cout << "クリティカル!!" << std::endl; Sleep(SLEEPTIME); std::cout << dm * 2 << " のダメージ" << std::endl; Sleep(SLEEPTIME); return dm * 2; } if ((lu - target.Avoidance + 5) / 10 >= rand() % 10 + 1) { std::cout << "MISS!!" << std::endl; Sleep(SLEEPTIME); return 0; } std::cout << dm << " のダメージ" << std::endl; Sleep(SLEEPTIME); return dm; } int Status::GetCom() { return Com; } bool Status::Commond(Status& target) { bool flag; //true = 先頭続行 false = 先頭終了 std::cout << "1.物理攻撃 2.魔法攻撃 3.魔法回復 4.ステータス確認 5.終了" << std::endl; std::cin >> Com; switch (Com) { case 1: flag = Dmage(target,DmageCal(target, 1)); break; case 2: if (MagicPoint > (Level + 1) / 2) { MpDown((Level + 1) / 2); flag = Dmage(target,DmageCal(target, 2)); Com = 1; } else { std::cout << "MPが足りません" << std::endl; flag = true; Com = 2; } break; case 3: if (MagicPoint > (Level + 1) / 2) { ReHp((Level + 1) / 2); flag = true; Com = 1; } else { std::cout << "MPが足りません" << std::endl; flag = true; Com = 2; } break; case 4: std::cout << "ステータス" << std::endl; Show(); flag = true; Com = 2; break; case 5: exit(EXIT_FAILURE); } return flag; }; bool Status::EnemyCommond(Status& player, Status& enemy) { bool flag; if (enemy.Atack < enemy.Magic && enemy.MagicPoint >= (enemy.Level + 1 / 2)) { std::cout << "敵の魔法攻撃!!" << std::endl; flag = Dmage(player, DmageCal(player, 22)); } else { std::cout << "敵の攻撃!!" << std::endl; flag = Dmage(player, DmageCal(player, 11)); } return flag; } void Status::BSt(Status& target) { printf("\n自分 HP %d / %d MP %d / %d || 敵 HP %d / %d MP %d / %d\n\n", HitPoint, MaxHitPoint, MagicPoint, MaxMagicPoint, target.HitPoint, target.MaxHitPoint, target.MagicPoint, target.MaxMagicPoint); } void Status::BStEnemy() { Sleep(SLEEPTIME); printf("敵情報 Level %d HP %d MP %d\n\n", Level, MaxHitPoint, MaxMagicPoint); Sleep(SLEEPTIME); } bool Status::Battle(Status& player) { std::cout << std::endl; Sleep(SLEEPTIME); std::cout << "敵が現れた!!" << std::endl; Status enemy; enemy.LevelUp(player.ShowLevel()); enemy.Exp(player); enemy.BStEnemy(); while (player.Commond(enemy)) { if (player.GetCom() == 1) { if (!enemy.EnemyCommond(player, enemy)) { Sleep(SLEEPTIME); std::cout << "負けた・・・" << std::endl; player.SetBflag(false); return false; } } player.BSt(enemy); }; if (player.GetBflag()) { Sleep(SLEEPTIME); std::cout << "敵を撃破した" << std::endl; Sleep(SLEEPTIME); std::cout << enemy.Getexp() << " 経験値を手に入れた" << std::endl; player.SetExp(enemy.Getexp()); return true; } return 0; }
初心者を脱出したらこの記事を見てニヤニヤしよう。
よし、テキスト勉強再開。