函数做游戏

这是一个C++编写的模拟游戏,包含了英雄、怪物、技能和装备的数据结构定义,以及选择英雄、更换武器、怪物出现和战斗的函数。玩家可以挑选英雄,更换武器,与怪物进行战斗,直到一方生命值归零为止。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <stdlib.h>

//技能结构体
typedef struct Skill{
    char *name;
    int  power;
}Skill;

//装备结构体
typedef struct Weapon{
    char *name;
    int  power;
}Weapon;

 //英雄结构体
typedef struct HeroStruct{
    char *name;
    int  blood;
    int  power;
    Weapon  weapon;
    Skill   skill;
}HeroStruct;

//怪物结构体
typedef struct MonsterStruct{
    char  *name;
    int   blood;
    int   power;
    Skill   skill;
}MonsterStruct;

//定义英雄的数组
 HeroStruct herolist[3];
//怪物的数组
 MonsterStruct monsterlist[3];
//英雄技能的数组
Skill heroSkillList[3];
//怪物的技能数组
Skill monsterSkillList[3];
//英雄的装备
Weapon weaponList[3];

#pragma mark      ------加载数据
void loadData(){
    char *heroNames[] = {"悟空","八戒","沙僧"};
    char *monsterNames[] = {"小骨","小杀","小画"};
    char *weaponNames[] = {"金箍棒","猪耙","铲子"};
    char *heroOfSkillNames[] = {"皮一呦","皮一啊","嘛尼嘛尼呗美哄"};
    char *monsterOfSkillNames[] = {"#¥#%","次无谓~啐~","五指抓"};
    int skillPower[] = {50,30,5};
    int heroBloods[] = {600,470,360};
    int monsterBloods[] = {400,370,260};
    int heroPowers[] = {60,30,10};
    int monsterPowers[] = {30,20,5};
    int weaponPowers[] = {30,20,10};
    
    //把装备放入数组
    for (int i=0; i<3; i++) {
        Weapon weapon={weaponNames[i],weaponPowers[i]};
        weaponList[i]=weapon;
    }
    
    //把英雄技能放入数组
    for (int i=0; i<3; i++) {
        Skill skill={heroOfSkillNames[i],skillPower[i]};
        heroSkillList[i]=skill;
    }
    
    //把怪物技能放入数组
    for (int i=0; i<3; i++) {
        Skill skill={monsterOfSkillNames[i],skillPower[i]};
        monsterSkillList[i]=skill;
    }
    
    //把英雄放入数组
    for (int i=0; i<3; i++) {
        HeroStruct hero;
        hero.name = heroNames[i];
        hero.blood = heroBloods[i];
        hero.power = heroPowers[i];
        hero.skill = heroSkillList[2];  //默认将初始技能设置为第三个
        hero.weapon = weaponList[2];
        
        herolist[i] = hero;
        //或者HeroStruct hero={heroNames[i],heroBloods[i],heroPowers[i]...};
    }
    
    //把怪物放入数组
    for (int i=0; i<3; i++) {
        MonsterStruct monster;
        monster.name = monsterNames[i];
        monster.blood = monsterBloods[i];
        monster.power = monsterPowers[i];
        monster.skill = monsterSkillList[2];
        
        monsterlist[i] = monster;
    }
}

#pragma mark   ----选择英雄函数
HeroStruct chooseHero(HeroStruct list[]){
    printf("壮士~请选择英雄\n");
    //遍历英雄数组,输出英雄名字
    for (int i=0; i<3; i++) {
        HeroStruct hero = list[i];
        printf("英雄编号:%d   %s\n",i+1,hero.name);
    }
    
    //选择英雄
    int whitchHeroNum=0;
    scanf(" %d",&whitchHeroNum);
    
    HeroStruct hero = herolist[whitchHeroNum-1];
    printf("你选择了装备了%s的%s,他拥有%d滴血,初始攻击力为%d\n",hero.weapon.name,hero.name,hero.blood,hero.power);
    return hero;
}

#pragma mark    ------选择武器函数

Weapon changWeapon(){
    printf("你选择了更换武器\n");
    for (int i=0; i<3; i++) {
        Weapon weapon = weaponList[i];
        printf("%d:%s\n",i+1,weapon.name);
    }
    int chooseWeaponNum=0;
    scanf(" %d",&chooseWeaponNum);
    Weapon weapon = weaponList[chooseWeaponNum-1];
 
    printf("装备了攻击力是%d的%s\n",weapon.power,weapon.name);
    return weapon;
}

#pragma mark   -----怪物出现函数

MonsterStruct showMonster(MonsterStruct list[]){
    printf("\n夜幕降临~!龇牙咧嘴的怪物们将要出现\n");
    
    int rand=arc4random()%3;
    MonsterStruct monster=list[rand];
    printf("拥有%d滴血的%s带着宝宝们出现了,他拥有%d攻击力\n\n",monster.blood,monster.name,monster.power);
    return monster;
}


#pragma mark   -----战斗函数
void fire(HeroStruct hero,MonsterStruct monster){

    while (1) {
        printf("伟大的%s您可以使用这些技能\n",hero.name);
        for (int i=0; i<3; i++) {
            Skill heroSkill = heroSkillList[i];
            printf("%d  %s\n",i+1,heroSkill.name);
        }
        
        //英雄选择技能
        int chooseSkillNum = 0;
        scanf(" %d",&chooseSkillNum);
        chooseSkillNum = chooseSkillNum>0&&chooseSkillNum<=3?chooseSkillNum:3;
        Skill heroSkill = heroSkillList[chooseSkillNum-1];
        
        //给怪物一定的几率攻击英雄
        int rand = arc4random()%4;
        Skill monsterSkill;
        if (rand!=3) {
            monsterSkill = monsterSkillList[rand];
            monster.power += monsterSkill.power;
            printf("%s使用了%s技能\n",monster.name,monsterSkill.name);
                    }
        
        //怪物攻击
        hero.blood-=monster.power;
        hero.blood=hero.blood<=0?0:hero.blood;
        printf("对%s造成了%d伤害,%s还剩了%d滴血\n",hero.name,monster.power,hero.name,hero.blood);
        
        //怪物攻击之后,攻击力变为使用技能之前的状态
        if (rand!=3) {
            monster.power -= monsterSkill.power;

        }
     
        
        //英雄攻击
        hero.skill = heroSkill;
        hero.power+=heroSkill.power;
        monster.blood-=hero.power;
        monster.blood=monster.blood<=0?0:monster.blood;
        printf("%s使用了%s技能,对%s造成了%d伤害,还剩%d滴血\n",hero.name,hero.skill.name,monster.name,hero.power,monster.blood);
        hero.power-=heroSkill.power;
        
        
       
        
        if (hero.blood==0||monster.blood==0) {
            break;   //战斗结束  分晓胜负
        }
    }
    char *message = hero.blood == 0?"英雄被打死":"怪物被打死";
    printf("%s\n",message);
}


#pragma mark   ----游戏开始函数
void startGame(){
    printf("欢迎来到***游戏\n");
    //加载数据
    loadData();
    //选择英雄
     HeroStruct hero = chooseHero(herolist);
    //装备武器
    printf("是否需要更换武器?\n1.yes    2.no\n");
    int isChangeWeapon=0;
    scanf(" %d",&isChangeWeapon);
    if (isChangeWeapon==1) {
        Weapon weapon = changWeapon();
        hero.power += weapon.power;
        hero.weapon = weapon;
        printf("装备上%s后,英雄的战斗力增加到了%d\n",hero.weapon.name, hero.power);  //有问题
        
    }

    //怪物
    MonsterStruct monster = showMonster(monsterlist);
    
    //战斗
    
    fire(hero,monster);
}







int main(int argc, const char * argv[]) {
    
   
    
    startGame();
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值