一些简单的代码(自行复制使用,最好不复制哈,gan'xi...

2025-04-292525
AI 快速预览详细 收起
本文提供了一个简易的C++ RPG游戏代码示例,帮助初学者理解游戏开发的基础。代码中包含了角色创建、装备购买、任务完成和战斗系统等功能。通过购买不同类型的装备(如木剑、石剑、铁剑等),玩家可以逐步提升角色等级。此外,还实现了基于玩家等级的购买限制,确保游戏平衡性。
#include<bits\stdc++.h>
#include<conio.h>
#include <stdio.h>
#include<unistd.h>
#include <windows.h>
#include <time.h>
#include<cstdio>
using namespace std;
int key,lv,sword_lv,armet_lv,breastplate_lv,cuish_lv,boot_lv,evolution_stone,super_evolution_stone,glint_super_evolution_stone,oracle_super_evolution_stone,god_super_evolution_stone,money,reward_lv,job;
string name,sword,armet,breastplate,cuish,boot,reward;
string sword_name[15]={"木剑","石剑","铁剑","钻石剑","闪剑","天剑","圣剑","神剑","元素剑"};
string reward_list[105]={"拯救格亚",""};
string principal_line[105]={"帮助村长","前往历练","通过试练1","接一个悬赏","完成一个悬赏","抵御狼群","回村","通过试炼2","转职"};
string job_list[25]={"战士","剑士","狂战士","骑士","坦克","召唤师","弓箭手","骑射手","法师","巫师","刺客","忍者","牧师","祭祀"};
int l_monster,w_monster,direction;
void clear(){
         system("cls");
}
int safe(){
        ofstream ofile;
        ofile.open("zhmc.tq");
        ofile<<lv<<name<<sword<<sword_lv<<armet<<armet_lv<<breastplate<<breastplate_lv<<cuish<<cuish_lv<<boot<<boot_lv<<evolution_stone<<super_evolution_stone<<glint_super_evolution_stone<<oracle_super_evolution_stone<<god_super_evolution_stone<<money<<reward<<reward_lv<<job;
        ofile.close();
}
int wars(){
        if(lv<=10){
                cout<<"1.普攻";
        }
}
int buy_sword(){
        clear();
        cout << "1.木剑   2.石剑   3.铁剑   4.钻石剑  5.闪剑   6.天剑  7.圣剑  8.神剑  9.元素剑  0.离开";
        key=_getch();
        key=char(key)-'0';
        if(key>0&&key<10){
                if(money>=10*pow(10,key)){
                        sword=sword_name[key];
                        sword_lv=1+sword_lv/10;
                        cout << endl << "购买成功"<<endl;
                        system("pause");
                        buy_sword();       
                        return 0;
                }
                else{
                        cout << endl << "购买失败"<<endl;
                        system("pause");
                        buy_sword();
                        return 0;
                }               
        }
        else{
                safe();
                return 0;
        }
       
}

int shop(){
        clear();
        cout << "你要买什么?"<<endl;
        cout << "1.剑   2.经验丹   3.装备    4.石头(特殊) 5.保存  0.离开";
        key=_getch();
        if(char(key)=='1'){
                        buy_sword();
                        shop();
                        return 0;
        }
}
int reward_f(){
        clear();
        if(reward=="无"){
               
        }
       
}
int index(){
        clear();
        cout << "欢迎来到天启小镇";
        sleep(1);
        clear();
        cout << "你要干什么"<<endl;
        cout << "1.去商店 2.去悬赏台 3.回家 4.外出游历 5.去刷怪塔 6.到处逛逛(可获得主线任务或者交付主线任务)";
        key=_getch();
        if(char(key)=='1'){
                        shop();
                        index();
                        return 0;
        }
        if(char(key)=='2'){
                        shop();
                        index();
                        return 0;
        }
}
int main(){

        cout << "                                                        开始游戏" << endl;
        cout << "                                                      (按下A键开始)"<<endl;
        while(!(char(key) == 'a' || char(key) == 'A')){
                key = _getch();
        }
        clear();
       
        ifstream ifile;
        ifile.open("zhmc.tq");
        ifile>>lv>>name>>sword>>sword_lv>>armet>>armet_lv>>breastplate>>breastplate_lv>>cuish>>cuish_lv>>boot>>boot_lv>>evolution_stone>>super_evolution_stone>>glint_super_evolution_stone>>oracle_super_evolution_stone>>god_super_evolution_stone>>money>>reward>>reward_lv>>job;
        if(lv==0)
        {
                ofstream ofile;
                ofile.open("zhmc.tq");
                ofile<<1<<" "<<"无"<<" "<<"无"<<" "<<0<<" "<<"无"<<" "<<0<<" "<<"无"<<" "<<0<<" "<<"无"<<" "<<0<<" "<<"无"<<" "<<0<<" "<<0<<" "<<0<<" "<<0<<" "<<0<<" "<<0<<" "<<0<<" "<<"无"<<" "<<0<<" "<<0;
                ofile.close();
                return 0;
        }
        if(name=="无"){
                clear();
                cout << "请输入你的名字:" << endl;
                cin >> name;
                safe();
        }
        clear();
        index();
        return 0;
}


创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(0)
- 没有更多了 -