设计并编写代码自动格斗类游戏
生活随笔
收集整理的這篇文章主要介紹了
设计并编写代码自动格斗类游戏
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述:
1)角色類CRole為基類:
構造函數、析構函數;
成員變量:頭像、HP(血量)、ATK(攻擊力)、DEF(防御力)、Lv(等級),EXP(經驗值);
成員函數:武器攻擊、跳躍。
2)英雄類CHero繼承于CRole類
構造函數、析構函數;
英雄類新增技能踢腿(成員函數)、抱摔(成員函數),給對方造成傷害具體值由學員自己進行設定;
3)敵人類CEnemy繼承于CRole類
構造函數、析構函數;
新增技能劈掌(成員函數)、連環腿(成員函數),給對方造成傷害值由學員自己進行設定;
4)戰斗類CBattle
構造函數、析構函數及其他成員函數
雙方HP(血量)初始值為100,開始戰斗以后,英雄和敵人輪流攻擊對方,掉血量=攻擊方技能傷害值+ATK(攻擊方的攻擊力)-DEF(防御方的防御力),當一方血量為0時,戰斗結束,玩家經驗值增加相應點數,當經驗等級達一定時候,玩家等級提升。
“game.h”
#pragma once #include<iostream> #include<string>//角色類 class CRole{public:std::string sculpture; //頭像int HP; //血量int ATK; //攻擊力int DEF; //防御力int Lv; //等級int EXP; //經驗public:CRole() {}; //默認構造函數CRole(const std::string sculpture,int HP,int ATK,int DEF,int Lv,int EXP);~CRole();void WeaponAttack(); //武器攻擊void Jump(); //跳躍void show();public:int wa_ATK; };//英雄類 class CHero :public CRole {public:CHero() {}; //默認構造函數CHero(const std::string sculpture, int HP, int ATK, int DEF, int Lv, int EXP);~CHero(); //析構函數void Kicking(); //踢腿void Wresting(); //抱摔public:int k_ATK; //踢腿攻擊力int w_ATK; //抱摔攻擊力 };//敵人類 class CEnemy:public CRole {public:CEnemy() {}; //默認構造函數CEnemy(const std::string sculpture, int HP, int ATK, int DEF, int Lv, int EXP);~CEnemy(); //析構函數void Chopping_Palm(); //劈掌void Serial_Leg(); //連環腿public:int p_ATK; //劈掌攻擊力int s_ATK; //連環腿攻擊力 };//戰斗類 class CBattle :public CRole {public:CBattle(); //構造函數~CBattle(); //析構函數void ready(CHero &obj1, CEnemy &obj2);//戰斗準備函數void fighting(CHero obj1, CEnemy obj2);//戰斗函數 };"game.cpp"
#include"pch.h" #include"game.h" #include<string> #include<Windows.h> #include<time.h>//角色類——構造函數 CRole::CRole(const std::string sculpture, int HP, int ATK, int DEF, int Lv, int EXP) {this->sculpture = sculpture;this->HP = HP;this->ATK = ATK;this->DEF = DEF;this->Lv = Lv;this->EXP = EXP; } void CRole::show() {std::cout << "角色:" << this->sculpture << " 血量:" << this->HP << " 攻擊力:"<< this->ATK << " 防御力:" << this->DEF << " 等級:" << this->Lv << " 經驗:" << this->EXP << std::endl; } //角色類——析構函數 CRole::~CRole(){}//角色類——武器攻擊 void CRole::WeaponAttack() {wa_ATK =10; }//角色類——跳躍 void CRole::Jump() {std::cout << "miss" << std::endl; }//英雄類——構造函數 CHero::CHero(const std::string sculpture, int HP, int ATK, int DEF, int Lv, int EXP):CRole(sculpture,HP,ATK,DEF,Lv,EXP) {}//英雄類——析構函數 CHero::~CHero() {}//英雄類——踢腿 void CHero::Kicking() {k_ATK = 10; }//英雄類——抱摔 void CHero::Wresting() {w_ATK = 15; }//敵人類——構造函數 CEnemy::CEnemy(const std::string sculpture, int HP, int ATK, int DEF, int Lv, int EXP):CRole(sculpture, HP, ATK, DEF, Lv, EXP) { }//敵人類——析構函數 CEnemy::~CEnemy() {}//敵人類——劈掌 void CEnemy::Chopping_Palm() {p_ATK = 10; }//敵人類——連環腿 void CEnemy::Serial_Leg() {s_ATK = 16; }//戰斗類——構造函數 CBattle::CBattle() {std::cout << "自動格斗小游戲" << std::endl; }//戰斗類——準備函數 void CBattle::ready(CHero &obj1, CEnemy &obj2){std::cout << "戰斗雙方" << std::endl;obj1.show();obj2.show();std::cout << "準備戰斗" << std::endl;for (int i = 3;i > 0;i--) {std::cout << i << std::endl;Sleep(1000);}std::cout << "戰斗開始" << std::endl;Sleep(1000); }//戰斗類——析構函數 CBattle::~CBattle() {}//戰斗類——格斗函數 void CBattle::fighting(CHero obj1, CEnemy obj2) {srand((unsigned)time(0));int Blood_Loss;int temp, temp1;int t = 1;//掉血量=攻擊方技能傷害+ATK(攻擊方的攻擊力)-防御方的防御力DEFwhile (obj1.HP > 0 && obj2.HP > 0) {std::cout << "第" << t << "回合" << std::endl;temp = rand() % (3 - 0 + 1) + 0;//隨機生成攻擊類型temp1 = rand() % (2 - 0 + 1) + 0;//隨機產生跳躍躲避攻擊,三分之一的概率觸發跳躍閃避if (t % 2 == 1) {switch (temp) {case 0://普通攻擊if (temp1 == 1) {obj2.Jump();std::cout<< obj1.sculpture << "對" << obj2.sculpture << "使出的普通攻擊被" << obj2.sculpture << "跳躍躲避了" << std::endl;}else {Blood_Loss = obj1.ATK - obj2.DEF;//掉血量obj2.HP -= Blood_Loss;//obj2剩余血量std::cout << obj1.sculpture << "使出普通攻擊對" << obj2.sculpture << "造成了" << Blood_Loss << "點傷害" << std::endl;}break;case 1:if (temp1 == 1) {//武器攻擊obj2.Jump();std::cout << obj1.sculpture << "對" << obj2.sculpture << "使出的武器攻擊被" << obj2.sculpture << "跳躍躲避了" << std::endl;}else {obj1.WeaponAttack();Blood_Loss = obj1.ATK + obj1.wa_ATK - obj2.DEF;obj2.HP -= Blood_Loss;std::cout << obj1.sculpture << "使用武器對" << obj2.sculpture << "造成了" << Blood_Loss << "點傷害" << std::endl;}break;case 2:if (temp1 == 1) {//踢腿攻擊obj2.Jump();std::cout << obj1.sculpture << "對" << obj2.sculpture << "使出踢腿攻擊被" << obj2.sculpture << "跳躍躲避了" << std::endl;}else {obj1.Kicking();Blood_Loss = obj1.ATK + obj1.k_ATK - obj2.DEF;obj2.HP -= Blood_Loss;std::cout << obj1.sculpture << "使用踢腿對" << obj2.sculpture << "造成了" << Blood_Loss << "點傷害" << std::endl;}break;case 3:if (temp1 == 1) {//抱摔攻擊obj2.Jump();std::cout << obj1.sculpture << "對" << obj2.sculpture << "使出抱摔攻擊被" << obj2.sculpture << "跳躍躲避了" << std::endl;}else {obj1.Wresting();//抱摔Blood_Loss = obj1.ATK + obj1.w_ATK - obj2.DEF;obj2.HP -= Blood_Loss;std::cout << obj1.sculpture << "使用抱摔對" << obj2.sculpture << "造成了" << Blood_Loss << "點傷害" << std::endl;}break;}}if (t % 2 == 0) {switch (temp) {case 0://普通攻擊if (temp1 == 1) {obj1.Jump();std::cout << obj2.sculpture << "對" << obj1.sculpture << "使出的普通攻擊被" << obj1.sculpture << "跳躍躲避了" << std::endl;}else {Blood_Loss = obj2.ATK - obj1.DEF;//掉血量obj1.HP -= Blood_Loss;//obj2剩余血量std::cout << obj2.sculpture << "使出普通攻擊對" << obj1.sculpture << "造成了" << Blood_Loss << "點傷害" << std::endl;}break;case 1:if (temp1 == 1) {//武器攻擊obj1.Jump();std::cout << obj2.sculpture << "對" << obj1.sculpture << "使出的武器攻擊被" << obj1.sculpture << "跳躍躲避了" << std::endl;}else {obj2.WeaponAttack();Blood_Loss = obj2.ATK + obj2.wa_ATK - obj1.DEF;obj1.HP -= Blood_Loss;std::cout << obj2.sculpture << "使用武器對" << obj1.sculpture << "造成了" << Blood_Loss << "點傷害" << std::endl;}break;case 2:if (temp1 == 1) {//劈掌攻擊obj1.Jump();std::cout << obj2.sculpture << "對" << obj1.sculpture << "使出的劈掌攻擊被" << obj1.sculpture << "跳躍躲避了" << std::endl;}else {obj2.Chopping_Palm();//劈掌Blood_Loss = obj2.ATK + obj2.p_ATK - obj1.DEF;obj1.HP -= Blood_Loss;std::cout << obj2.sculpture << "使用劈掌對" << obj1.sculpture << "造成了" << Blood_Loss << "點傷害" << std::endl;}break;case 3:if (temp1 == 1) {//連環腿攻擊obj1.Jump();std::cout << obj2.sculpture << "對" << obj1.sculpture << "使出的連環腿攻擊被" << obj1.sculpture << "跳躍躲避了" << std::endl;}else {obj2.Serial_Leg();//連環腿Blood_Loss = obj2.ATK + obj2.s_ATK - obj1.DEF;obj1.HP -= Blood_Loss;std::cout << obj1.sculpture << "使用連環腿對" << obj2.sculpture << "造成了" << Blood_Loss << "點傷害" << std::endl;}break;}}t++;}std::cout << std::endl<<"戰斗結束"<<std::endl;if (obj1.HP > 0) {int exp;exp = obj2.Lv * 10;obj1.EXP += exp;std::cout << obj2.sculpture << "死亡,";std::cout << obj1.sculpture << "勝利!獲得" << exp << "經驗,目前經驗值為:" << obj1.EXP << std::endl;}if (obj2.HP > 0) {int exp;exp = obj1.Lv * 5;obj2.EXP += exp;std::cout << obj1.sculpture << "死亡,";std::cout << obj2.sculpture << "勝利!獲得" << exp << "經驗,目前經驗值為:" << obj2.EXP << std::endl;} }"main.cpp"
// FightingGame.cpp : 此文件包含 "main" 函數。程序執行將在此處開始并結束。 //#include "pch.h" #include"game.h" #include <iostream> #include<string> #include<Windows.h> #include<time.h> int main() {//創建普通角色CRole Ordy_character1("村民1",100,3,5,1,0);CRole Ordy_character2("村民2",100,3,6,1,20);CRole Ordy_character3("村民3", 100,4,9,3,100);//創建英雄角色CHero Hero1("張飛", 100, 19, 5, 1, 0);CHero Hero2("劉備", 100, 18, 5, 1, 0);CHero Hero3("關羽", 100, 20, 5, 1, 0);//創建敵人角色CEnemy enemy1("小黃巾1",100,16,3,3,100);CEnemy enemy2("小黃巾2",100,16,3,3,100);CEnemy enemy3("小黃巾3", 100, 16, 3, 3, 100);/*std::cout << "戰斗雙方" << std::endl;Ordy_character1.show();Hero1.show();*/CBattle battle1;battle1.ready(Hero1, enemy1);battle1.fighting(Hero1, enemy1); }運行結果:
轉載于:https://www.cnblogs.com/izzwhf/p/10707247.html
總結
以上是生活随笔為你收集整理的设计并编写代码自动格斗类游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 拼多多免费版 自动回复 关键词回复 提高
- 下一篇: 关于ubuntu的详细介绍