| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 981 人关注过本帖
标题:角色扮演类游戏中添加随机函数
只看楼主 加入收藏
内谁别跑
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-6-24
结帖率:0
收藏
已结贴  问题点数:20 回复次数:7 
角色扮演类游戏中添加随机函数
使得游戏中随机出现三个设定好的角色做对手
程序代码:
//=======================
//        main.cpp
//=======================

// main function for the RPG style game
//主要功能为RPG游戏风格



#include <iostream>
#include <string>
using namespace std;

#include "swordsman.h"
#include"archer.h"
#include"mage.h"


int main()
{
    string tempName;
    bool success=0;        //flag for storing whether operation is successful
                        //标志用于存储操作是否成功


    cout <<"Please input player's name: ";
    cin>>tempName;        // get player's name from keyboard input
                       //从键盘输入得到玩家的名字


    player *human;        // use pointer of base class, convenience for polymorphism
                       //使用基类的指针,方便多态性


    int tempJob;        // temp choice for job selection
                       //临时工作选择的选择


    do
    {
        cout <<"Please choose a job: 1 Swordsman, 2 Archer, 3 Mage"<<endl;
        cin>>tempJob;                 //剑客         弓箭手    法师
        system("cls");        // clear the screen清除屏幕


        switch(tempJob)
        {
        case 1:
            human=new swordsman(1,tempName);    // create the character with user inputted name and job
                                                 //创建角色与用户输入的姓名和工作


            success=1;        // operation succeed操作成功


            break;
        case 2:
            human=new archer(1,tempName);    // create the character with user inputted name and job
                                                 //创建角色与用户输入的姓名和工作


            success=1;        // operation succeed操作成功


            break;
            case 3:
            human=new mage(1,tempName);    // create the character with user inputted name and job
                                                 //创建角色与用户输入的姓名和工作


            success=1;        // operation succeed操作成功


            break;

        default:
            break;                // In this case, success=0, character creation failed
                                //在这种情况下,成功= 0,人物创建失败了


        }
    }while(success!=1);        // so the loop will ask user to re-create a character
                            //因此,循环将要求用户重新创建一个角色



    int tempCom;            // temp command inputted by user临时命令由用户输入


    int nOpp=0;                // the Nth opponent第n个对手



    for(int i=1;nOpp<5;i+=2)    // i is opponent's level     是对手的水平


    {srand(0);
for( int i = 0; i < 10; i++)
{
cout<<rand()<<' ';
}
        nOpp++;
        system("cls");
        cout<<"STAGE" <<nOpp<<endl;
        cout<<"Your opponent, a Level "<<i<<" Swordsman."<<endl;
        system("pause");
        swordsman enemy(i, "Warrior");    // Initialise an opponent, level i, name "Junior"
                                        //初始化一个对手,i  水平,name“初级”



        human->reFill();                // get HP/MP refill before start fight战斗之前补充血量和蓝
        
        while(!human->death() && !enemy.death())    // no died没死
        {
            success=0;
            while (success!=1)
            {
                showinfo(*human,enemy);                // show fighter's information显示战士信息
                cout<<"Please give command: "<<endl;
                cout<<"1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game"<<endl;
                cin>>tempCom;
                switch(tempCom)
                {
                case 0:
                    cout<<"Are you sure to exit? Y/N"<<endl;
                    char temp;
                    cin>>temp;
                    if(temp=='Y'||temp=='y')
                        return 0;
                    else
                        break;
                case 1:
                    success=human->attack(enemy);
                    human->isLevelUp();
                    enemy.isDead();
                    break;
                case 2:
                    success=human->specialatt(enemy);
                    human->isLevelUp();
                    enemy.isDead();
                    break;
                case 3:
                    success=human->useHeal();
                    break;
                case 4:
                    success=human->useMW();
                    break;
                default:
                    break;
                }
            }
            if(!enemy.death())        // If AI still alive如果ai还活着
                enemy.AI(*human);
            else                            // AI died    死
            {
                cout<<"YOU WIN"<<endl;
                human->transfer(enemy);        // player got all AI's items得到所有玩家的物品
            }
            if (human->death())
            {
                system("cls");
                cout<<endl<<setw(50)<<"GAME OVER"<<endl;
                delete human;//6_???????????        // player is dead, program is getting to its end, what should we do here?玩家死了,结束程序,我们应该做些什么呢?
                system("pause");
                return 0;
            }
        }
    }
    delete human;//7_?????????            // You win, program is getting to its end, what should we do here?你赢了,程序结束,我们该怎么办呢?


    system("cls");
    cout<<"Congratulations! You defeated all opponents!!"<<endl;
    system("pause");
    return 0;
}
        

[local]1[/local]


game.rar (1.16 MB)
搜索更多相关主题的帖子: 角色扮演类 function style 游戏 
2015-06-24 22:27
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
你不就是问随机数相关的函数是哪些吗?干嘛贴出又长又无关的代码,谁看呀?!

在C++中可以用继承自C语言的 srand / rand
也可以用C++独有的随机函数库,比较对,自己搜索“C++11 随机数”
2015-06-25 09:11
内谁别跑
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-6-24
收藏
得分:0 
回复 2楼 rjsp
是不知道写在哪里。
 在网上找了很多随机函数的文章,还是不会。
2015-06-25 10:09
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:10 
你的游戏规则是什么呢

DO IT YOURSELF !
2015-06-25 10:57
内谁别跑
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-6-24
收藏
得分:0 
回复 4楼 wp231957
考核项目及要求
项目:RPG游戏设计开发
1.考核要点
使用面向对象思想,在现有的基础之上,设计完成一款角色扮演类的游戏,其中包含3个游戏角色:swordsman、archer、mage。要求:
    在带有“?”的7处填写相应的代码,使得程序能够正常编译。
    设计另外两个类:archer、mage,并对每个角色赋予不同的特殊技能。
    游戏过程中系统随机出现各种类别的敌人,战胜敌人可获得相应的财富、经验值。
    玩家拥有部分自我修复功能。
2.作品要求
设计流程规范详细、步骤高效合理、内容充分全面。充分运用所学的知识对系统所涉及到的类、函数进行正确设计编码,其中设计过程中所涉及到的部分显示信息(如用户角色、经验值、包裹信息等)需截图并进行简单阐述。
2015-06-25 11:00
tm1mc2
Rank: 2
等 级:论坛游民
帖 子:28
专家分:46
注 册:2014-8-21
收藏
得分:0 
读取系统时间做随机数比较简便。
2015-07-02 20:25
kiss96baibai
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2015-7-3
收藏
得分:0 
2015-07-03 11:55
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
回复 5楼 内谁别跑
srand(time(0));//得到随机数种子
int x;
x=(int)rand()%2;//rand()返回一个随机数,让它对2取模,可得到0~2的随机数,
                //转换为整数,就是0或1。
                //对不同的整数取模,可在更大的整数范围内得到随机值.
switch(x)//根据得到的随机数的具体值,执行不同操作。
{
case 0:...;break;
case 1:...;break;
}
2015-07-04 17:16
快速回复:角色扮演类游戏中添加随机函数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027569 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved