服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 利用C++编写简易宝可梦对战小游戏

利用C++编写简易宝可梦对战小游戏

2022-12-26 15:58agmissle C/C++

本文利用C++语言编写了一个小时候课间嘴上玩的那种宝可梦游戏,只有早期的三个宝可梦和基础招式,感兴趣的朋友快跟随小编一起学习学习吧

最近想到了用C++写个小时候课间嘴上玩的那种宝可梦游戏,先试写了个demo,只有早期的三个宝可梦和基础招式,感兴趣的朋友可以再自己添加,也没有各种物防特攻数值啥的,等以后会搞图形化界面了再做个复杂的,功能完善的。

玩法就是选择宝可梦和电脑对战,除了攻击还有三次防御的机会,可以完全抵消对面这轮的攻击,增加了一点博弈的感觉(其实和电脑也没啥好博弈的)

上代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include<iostream>
#include<windows.h>
#include<ctime>
enum Type {normal,fire,grass,water};//定义招式与pokemon属性
struct Atk//招式结构体
{
    char name[20];//招式名
    int damage;//伤害
    Type type;//属性
    int speed;//招式速度
    int pp; //招式技能点数量
};
Atk def{"def",0,normal,10,3};//防御
struct pokemon//pokemon结构体
{
    char name[30];//名
    int hp;//初始血量
    int speed;//pokemon本身速度
    Type type;//属性
    Atk atk1;//招式1到4
    Atk atk2;
    Atk atk3;
    Atk atk4;
    int def;//防御点数
};
void initial();
int choose(int);
int AIchoose(int);
int p_choose(int,pokemon);
int AI_choose(int,pokemon);
void duel(pokemon p_pokemon,pokemon AI_pokemmon);
double damage_coeff(Type type1,Type type2);
 
int main()
{
    using namespace std;
    int cont=1;//设定循环游戏的flag
    while(cont==1)
    {
        system("cls");//新游戏开始时清屏
        //初始化各个招式与pokemon
        Atk watergun{"water gun",40,water,2,2};//水枪
        Atk quickattack{"quick attack",40,normal,5,3};//电光石火
        Atk scratch{"scratch",40,normal,2,8};//抓
        Atk bubblebeam{"bubble beam",65,water,1,1};//泡沫光线
        Atk razorleaf{"razor leaf",55,grass,3,1};//飞叶快刀
        Atk vinewhip{"vine whip",35,grass,4,2};//藤鞭
        Atk tackle{"tackle",35,normal,2,10};//撞击
        Atk ember{"ember",40,fire,2,1};//火苗
        Atk firespin{"fire spin",35,fire,4,2};//火焰旋涡
        pokemon bulbasaur{"bulbasaur",200,3,grass,tackle,quickattack,vinewhip,razorleaf,3};//妙蛙种子
        pokemon charmander{"charmander",200,2,fire,scratch,quickattack,ember,firespin,3};//小火龙
        pokemon squirtle{"squirtle",200,1,water,tackle,quickattack,watergun,bubblebeam,3};//杰尼龟
        initial();
        pokemon p_pokemon;
        pokemon AI_pokemon;
        int Num1,Num2;
        Num1=choose(Num1);
        Num2=AIchoose(Num2);
        //确定双方选择的pokemon
        switch(Num1)
        {
            case 1:
                p_pokemon=bulbasaur;
                break;
            case 2:
                p_pokemon=charmander;
                break;
            case 3:
                p_pokemon=squirtle;
                break;
        }
        switch(Num2)
        {
            case 0:
                AI_pokemon=bulbasaur;
                break;
            case 1:
                AI_pokemon=charmander;
                break;
            case 2:
                AI_pokemon=squirtle;
                break;
        }
        system("cls");
        Sleep(500);
        cout <<"You choose "<<p_pokemon.name<<" and your opponent choose "<<AI_pokemon.name<<endl;
        duel(p_pokemon,AI_pokemon);
        cout <<"If you want to play another round,enter number 1,or enter others number to quit:"<<endl;
        cin >>cont;
    }
    cin.get();
    return 0;
}
 
void initial()//初始化界面
{
    using namespace std;
    cout <<"Welcome to the pokemon duel!"<<endl;
    cout <<"Your pokemon has 3 chances to defend.Once you decide to defend,you will not get damage this turn."<<endl;
    cout <<"But also,you can not attack this turn."<<endl;
    cout <<"Now choose your pokemon by enter the number:";
    cout <<"1.bulbasaur 2.charmander 3.squirtle";
}
int choose(int Num)//玩家选择pokemon,利用递归让玩家正确输入
{
    using namespace std;
    cin >>Num;
    if(Num!=1&&Num!=2&&Num!=3)
    {
        cout <<"You choose a wrong number, choose it again:";
        Num=choose(Num);
    }
    return Num;
}
int AIchoose(int Num)//电脑选择pokemon,利用随机数让电脑随机选择
{
    using namespace std;
    srand((unsigned)time(NULL));
    Num = rand() % 3;
    return Num;
}
int p_choose(int pchoose,pokemon p_pokemon)//玩家选择招式,利用递归让玩家正确输入
{
    using namespace std;
    cin >>pchoose;
    //判断玩家所选的招式PP是否足够
    if(pchoose==1&&p_pokemon.atk1.pp<=0)
    {
        cout <<"You don't have enough pp"<<endl;
        pchoose=p_choose(pchoose,p_pokemon);
    }
    if(pchoose==2&&p_pokemon.atk2.pp<=0)
    {
        cout <<"You don't have enough pp"<<endl;
        pchoose=p_choose(pchoose,p_pokemon);
    }
    if(pchoose==3&&p_pokemon.atk3.pp<=0)
    {
        cout <<"You don't have enough pp"<<endl;
        pchoose=p_choose(pchoose,p_pokemon);
    }
    if(pchoose==4&&p_pokemon.atk4.pp<=0)
    {
        cout <<"You don't have enough pp"<<endl;
       pchoose=p_choose(pchoose,p_pokemon);
    }
    if(pchoose==5&&p_pokemon.def<=0)
    {
        cout <<"You don't have enough pp"<<endl;
        pchoose=p_choose(pchoose,p_pokemon);
    }
    if(pchoose>5||pchoose<=0)
    {
        cout <<"You choose a wrong number, choose it again:";
        pchoose=p_choose(pchoose,p_pokemon);
    }
    return pchoose;
}
int AI_choose(int AIchoose,pokemon AI_pokemon)//利用随机数让电脑随机选择
{
    using namespace std;
    srand((unsigned)time(NULL));
    AIchoose = rand() % 5;
    //判断电脑选择招式的PP是否足够
    if(AIchoose==0&&AI_pokemon.atk1.pp<=0)
    {
        Sleep(1000);
        AIchoose=AI_choose(AIchoose,AI_pokemon);
    }
    if(AIchoose==1&&AI_pokemon.atk2.pp<=0)
    {
        Sleep(1000);
        AIchoose=AI_choose(AIchoose,AI_pokemon);
    }
    if(AIchoose==2&&AI_pokemon.atk3.pp<=0)
    {
        Sleep(1000);
        AIchoose=AI_choose(AIchoose,AI_pokemon);
    }
    if(AIchoose==3&&AI_pokemon.atk4.pp<=0)
    {
        Sleep(1000);
        AIchoose=AI_choose(AIchoose,AI_pokemon);
    }
    if(AIchoose==4&&AI_pokemon.def<=0)
    {
        Sleep(1000);
        AIchoose=AI_choose(AIchoose,AI_pokemon);
    }
    return AIchoose;
}
double damage_coeff(Type type1,Type type2)//计算攻击时伤害加成系数,type1为招式属性,type2为pokemon属性
{
    if(type1==normal)
    {
        return 1;
    }
    if(type1==grass)
    {
        switch(type2)
        {
            case grass:
                return 0.5;
                break;
            case fire:
                return 0.5;
                break;
            case water:
                return 2;
                break;
        }
    }
    if(type1==fire)
    {
        switch(type2)
        {
            case grass:
                return 2;
                break;
            case fire:
                return 0.5;
                break;
            case water:
                return 0.5;
                break;
        }
    }
    if(type1==water)
    {
        switch(type2)
        {
            case grass:
                return 0.5;
                break;
            case fire:
                return 2;
                break;
            case water:
                return 0.5;
                break;
        }
    }
}
void duel(pokemon p_pokemon,pokemon AI_pokemmon)//战斗流程函数
{
    using namespace std;
    while(p_pokemon.hp>0&&AI_pokemmon.hp>0)//双方HP均大于0时继续战斗
    {
        //显示双方相关信息
        cout <<"Your HP:"<<p_pokemon.hp<<" Your opponent's HP:"<<AI_pokemmon.hp<<endl<<endl;
        cout <<"Choose your action this turn by ernter the number:"<<endl;
        cout <<"1."<<p_pokemon.atk1.name<<" PP:"<<p_pokemon.atk1.pp<<endl;
        cout <<"2."<<p_pokemon.atk2.name<<" PP:"<<p_pokemon.atk2.pp<<endl;
        cout <<"3."<<p_pokemon.atk3.name<<" PP:"<<p_pokemon.atk3.pp<<endl;
        cout <<"4."<<p_pokemon.atk4.name<<" PP:"<<p_pokemon.atk4.pp<<endl;
        cout <<"5.deffense PP:"<<p_pokemon.def;
        Atk this_turn_p_atk;//定义玩家本轮所选招式
        Atk this_turn_AI_atk;//定义电脑本轮所选招式
        int pchoose;
        pchoose=p_choose(pchoose,p_pokemon);
        int AIchoose;
        AIchoose=AI_choose(AIchoose,AI_pokemmon);
        switch(pchoose)
        {
            case 1:
                p_pokemon.atk1.pp=p_pokemon.atk1.pp-1;
                this_turn_p_atk=p_pokemon.atk1;
                break;
            case 2:
                p_pokemon.atk2.pp=p_pokemon.atk2.pp-1;
                this_turn_p_atk=p_pokemon.atk2;
                break;
            case 3:
                p_pokemon.atk3.pp=p_pokemon.atk3.pp-1;
                this_turn_p_atk=p_pokemon.atk3;
                break;
            case 4:
                p_pokemon.atk4.pp=p_pokemon.atk4.pp-1;
                this_turn_p_atk=p_pokemon.atk4;
                break;
            case 5:
                p_pokemon.def=p_pokemon.def-1;
                this_turn_p_atk=def;
                break;
        }
        switch(AIchoose)
        {
            case 0:
                AI_pokemmon.atk1.pp=AI_pokemmon.atk1.pp-1;
                this_turn_AI_atk=AI_pokemmon.atk1;
                break;
            case 1:
                AI_pokemmon.atk2.pp=AI_pokemmon.atk2.pp-1;
                this_turn_AI_atk=AI_pokemmon.atk2;
                break;
            case 2:
                AI_pokemmon.atk3.pp=AI_pokemmon.atk3.pp-1;
                this_turn_AI_atk=AI_pokemmon.atk3;
                break;
            case 3:
                AI_pokemmon.atk4.pp=AI_pokemmon.atk4.pp-1;
                this_turn_AI_atk=AI_pokemmon.atk4;
                break;
            case 4:
                AI_pokemmon.def=AI_pokemmon.def-1;
                this_turn_AI_atk=def;
                break;
        }
        system("cls");
        //判断是否有玩家本轮采取防御
        if(AIchoose==4&&pchoose!=5)
        {
            cout <<"You use "<<this_turn_p_atk.name<<".";
            Sleep(500);
            cout <<"But your attack was defended!"<<endl;
        }
        else if(pchoose==5&&AIchoose!=4)
        {
            cout <<"Your opponent use "<<this_turn_AI_atk.name<<".";
            Sleep(500);
            cout <<"But you defend the attack from the opponent!"<<endl;
        }
        else if(pchoose==5&&AIchoose==4)
        {
            cout <<"You both defend this turn."<<endl;
            Sleep(250);
        }
        else//双方都没有防御的情况
        {
            int p_speed,AI_speed;
            double p_coeff,AI_coeff;
            //真正的速度为pokemon本身速度加招式速度
            p_speed=p_pokemon.speed+this_turn_p_atk.speed;//本轮玩家真正的速度
            AI_speed=AI_pokemmon.speed+this_turn_AI_atk.speed;//本轮电脑真正的速度
            //计算伤害系数
            p_coeff=damage_coeff(this_turn_p_atk.type,AI_pokemmon.type);
            AI_coeff=damage_coeff(this_turn_AI_atk.type,p_pokemon.type);
            //比较双方谁本轮速度快,判断先手
            if(AI_speed>p_speed)
            {
                cout <<"Your opponent use "<<this_turn_AI_atk.name<<" !"<<endl;
                Sleep(500);
                p_pokemon.hp=p_pokemon.hp-this_turn_AI_atk.damage*AI_coeff;//计算收到的伤害,伤害为招式伤害乘以伤害系数
                if(p_pokemon.hp<=0)
                {
                    cout <<"You have been defeated!"<<endl;
                }
                else if(p_pokemon.hp>0)
                {
                    cout <<"You use "<<this_turn_p_atk.name<<" !"<<endl;
                    Sleep(500);
                    AI_pokemmon.hp=AI_pokemmon.hp-this_turn_p_atk.damage*p_coeff;
                    if(AI_pokemmon.hp<=0)
                    {
                        cout <<"You win!"<<endl;
                    }   
                }
            }
            else if(AI_speed<p_speed)
            {
                cout <<"You use "<<this_turn_p_atk.name<<" !"<<endl;
                Sleep(500);
                AI_pokemmon.hp=AI_pokemmon.hp-this_turn_p_atk.damage*p_coeff;
                if(AI_pokemmon.hp<=0)
                {
                    cout <<"You win!"<<endl;
                }
                else
                {
                    cout <<"Your opponent use "<<this_turn_AI_atk.name<<" !"<<endl;
                    Sleep(500);
                    p_pokemon.hp=p_pokemon.hp-this_turn_AI_atk.damage*AI_coeff;
                    if(p_pokemon.hp<=0)
                    {
                        cout <<"You have been defeated!"<<endl;
                    }
                }
            }
            else if(AI_speed==p_speed)
            {
                cout <<"You both attack at the same time!"<<endl;
                Sleep(500);
                cout <<"You use "<<this_turn_p_atk.name<<" !"<<endl;
                Sleep(500);
                cout <<" Your opponent use "<<this_turn_AI_atk.name<<" !"<<endl;
                Sleep(500);
                p_pokemon.hp=p_pokemon.hp-this_turn_AI_atk.damage*AI_coeff;
                AI_pokemmon.hp=AI_pokemmon.hp-this_turn_p_atk.damage*p_coeff;
                if(AI_pokemmon.hp<=0&&p_pokemon.hp>0)
                {
                    cout <<"You win!"<<endl;
                }
                else if(AI_pokemmon.hp>0&&p_pokemon.hp<=0)
                {
                    cout <<"You have been defeated!"<<endl;
                }
                else if(AI_pokemmon.hp<=0&&p_pokemon.hp<=0)
                {
                    cout <<"Draw game!";
                }
            }
        }
    }
}

以上就是利用C++编写简易宝可梦对战小游戏的详细内容,更多关于C++宝可梦对战游戏的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/agmissle/article/details/125275117

延伸 · 阅读

精彩推荐
  • C/C++C++ 中CListCtrl的每个项都显示不同的提示信息

    C++ 中CListCtrl的每个项都显示不同的提示信息

    这篇文章主要介绍了C++ 中CListCtrl的每个项都显示不同的提示信息的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下...

    z02031530086772021-06-02
  • C/C++解读C++编程中派生类的构成和创建

    解读C++编程中派生类的构成和创建

    这篇文章主要介绍了解读C++编程中派生类的构成和创建,是C++入门学习中的基础知识,需要的朋友可以参考下...

    C++教程网7192021-03-14
  • C/C++C++实现LeetCode(118.杨辉三角)

    C++实现LeetCode(118.杨辉三角)

    这篇文章主要介绍了C++实现LeetCode(118.杨辉三角),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    Grandyang8342021-12-03
  • C/C++c 调用python出现异常的原因分析

    c 调用python出现异常的原因分析

    本篇文章是对使用c语言调用python出现异常的原因进行了详细的分析介绍,需要的朋友参考下...

    C语言教程网4052020-12-10
  • C/C++C语言实现经典24点纸牌益智游戏

    C语言实现经典24点纸牌益智游戏

    这篇文章主要为大家详细介绍了C语言实现经典24点纸牌益智游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Damao979392021-08-04
  • C/C++wxWidgets实现无标题栏窗口拖动效果

    wxWidgets实现无标题栏窗口拖动效果

    这篇文章主要为大家详细介绍了wxWidgets实现无标题栏窗口拖动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    zfwjsjzy6742021-07-22
  • C/C++详解c++ 静态成员变量

    详解c++ 静态成员变量

    这篇文章主要介绍了c++ 静态成员变量的相关资料,帮助大家更好的理解和学习c++,感兴趣的朋友可以了解下...

    tlanyan7392021-09-27
  • C/C++C语言实现单链表的快速排序算法

    C语言实现单链表的快速排序算法

    大家好,本篇文章主要讲的是C语言实现单链表的快速排序算法,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下...

    CUP-GYC10122022-08-31