改进版游戏代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了改进版游戏代码相关的知识,希望对你有一定的参考价值。
1 #include<stdlib.h> 2 #include<stdio.h> 3 #include<time.h> //suiji 4 #include<string.h> 5 #include<windows.h> //SLEEP函数 6 struct Player //玩家结构体,并初始化player 7 { 8 char name[21]; 9 int attack; 10 int defense; 11 int health; 12 long int max_health; 13 int level; 14 int exp; 15 int range_exp; 16 long int max_exp; 17 }player={"勇者",50,40,100,100,1,0,0,100}; 18 struct Enemy //怪的结构体,并初始化各种怪 19 { 20 char name[20]; 21 char wupin[12]; 22 int attack; 23 int defense; 24 int health; 25 int money; 26 long int exp; 27 int wupin_sign; 28 int wupinpro; 29 int double_attack; 30 int miss; 31 }strongman={"森林巨人","黄金圣衣",40,50,350,200,100,1,2,1,0}, 32 witch={"森林女巫","银甲",25,15,100,50,50,2,2,1,1}, 33 xiyi={"森林蜥蜴","铁甲",18,10,50,30,35,3,3,2,2}, 34 big_strongman={"森林巨人王","巨人之臂",40*5,50*5,350*5,200*5,100*5,4,4,2,0}, 35 lion={"草原雄狮","绝世好剑",60,30,280,200,100,5,2,1,0}, 36 horse={"草原野马","碧血剑",28,12,90,50,50,6,2,1,1}, 37 bee={"草原黄蜂","长剑",17,11,60,30,35,7,3,2,2}, 38 shitu={"使徒","\0",60*8,30*8,280*8,200*8,100*8,9,1,1,0}, 39 guai={"\0","\0",0,0,0,0,0,0,0,0,0}; 40 struct Place 41 { 42 int bar,hotel,forest1,forest2,forest3,grass1,grass2,grass3,shop; 43 }place={1,2,3,4,5,6,7,8,9}; 44 45 int max_exp=0; 46 int choose_number=0,s=0,strongman_arm=0,battle=0,money=500,place_sign=9; 47 int cao=3,jijiubao=2,baiyao=2,superbaiyao=1,boom=3,dubiao=2,atom_boom=1; 48 int fang=0,fang1=10,fang1n=0,fang2=20,fang2n=0,fang3=40,fang3n=0,fang4=100,fang4n=0; 49 int gong=0,gong1=8,gong1n=0,gong2=15,gong2n=0,gong3=25,gong3n=0,gong4=60,gong4n=0; 50 char gongname[20]="无",fangname[20]="无"; 51 char proof; 52 53 void AddWupin(int); 54 int AttackResult(); 55 void BattleAct(); 56 void ChooseWupin(); 57 void DisplayState(); 58 void OrdinaryAct(); 59 int SuiJi(); 60 int SuiJi100(); 61 void WhetherLevelUp(); 62 void SlowDisplay(char *); 63 64 int main() 65 { 66 67 68 int i=0,j=0,k=0; 69 char player_name[21]; 70 71 SlowDisplay("这是一个勇者的世界! 雅莉萨斯国的罗茜公主被陌生人绑架了!\n\n 伟大的勇者啊~拿起你们的武器,营救公主!\n\n\n输入你的名字: (20个字符)\n\n\n"); 72 73 // sndPlaySound("res\\F005.wav",SND_LOOP); 74 scanf("%s",player_name); 75 strncpy(player.name,player_name,20); 76 if(strcmp(player.name,"EVA初号机")==0) 77 { 78 SlowDisplay("\n 隐身多年的 EVA初号机 啊! 你终于出来了!雅莉萨斯国正需要你的帮助,快去吧!\n\n\n\n\n\n\n 这时,一道白光射出,射到了 EVA初号机 的身上\n\n\n\n\n\n\n ......\n\n\n\n\n\n\n EVA初号机 吸收能量ing... 随即... 能力觉醒!!!\n\n\n\n\n\n\n"); 79 player.attack=100000; 80 player.defense=100000; 81 player.health=100000; 82 player.max_health=100000; 83 } 84 else 85 { 86 SlowDisplay("\n雅莉萨斯国国王:\n\n\n 勇者,欢迎你来到我的王国!但是,由于我的女儿被抢走了,烦请勇士帮我们一下吧!会有丰厚的报酬哦~~\n\n\n"); 87 printf("%s",player.name); 88 SlowDisplay(" :\n\n\n 您放心好了,我一定帮您把公主救出来!\n\n\n\n 国王大手一挥,你落到了这个世界,开始了拯救公主的征程。\n\n\n\n\n\n\n"); 89 } 90 getchar(); 91 92 OrdinaryAct(); 93 return 0; 94 } 95 96 int SuiJi() 97 { 98 srand((unsigned)time(NULL)); 99 return rand()%10; 100 } 101 102 103 int SuiJi100() 104 { 105 srand((unsigned)time(NULL)); 106 return rand()%100; 107 } 108 109 110 void ChooseWupin() //选择物品 并使用 111 { 112 printf("物品: 1,止血草%d个 2,急救包%d个 3,云南白药%d个 4,超级云南白药%d个 5,手雷%d个 6,毒标%d个 7,手抛式原子弹%d个 0,返回\n\n\n",cao,jijiubao,baiyao,superbaiyao,boom,dubiao,atom_boom); 113 switch(scanf("%d",&choose_number),choose_number) 114 { 115 case 1: 116 if(cao>0) 117 { 118 SlowDisplay("使用止血草,HP增加60\n\n\n"); 119 cao--; 120 if(player.health+60>player.max_health)player.health=player.max_health; 121 else player.health+=60; 122 } 123 else SlowDisplay("没有止血草了\n\n\n"); 124 break; 125 case 2: 126 if(jijiubao>0) 127 { 128 SlowDisplay("使用急救包,HP增加80\n\n\n"); 129 jijiubao--; 130 if(player.health+80>player.max_health)player.health=player.max_health; 131 else player.health+=80; 132 } 133 else SlowDisplay("没有急救包了\n\n\n"); 134 break; 135 case 3: 136 if(baiyao>0) 137 { 138 SlowDisplay("使用云南白药,HP增加120\n\n\n"); 139 baiyao--; 140 if(player.health+120>player.max_health)player.health=player.max_health; 141 else player.health+=120; 142 } 143 else SlowDisplay("没有云南白药了\n\n\n"); 144 break; 145 case 4: 146 if(superbaiyao>0) 147 { 148 SlowDisplay("使用超级云南白药,HP增加200\n\n\n"); 149 superbaiyao--; 150 if(player.health+200>player.max_health)player.health=player.max_health; 151 else player.health+=200; 152 } 153 else SlowDisplay("没有超级云南白药了\n\n\n"); 154 break; 155 case 5: 156 if(battle) //在战斗中(battle=1),否则(battle=0)不能使用攻击性物品 157 { 158 if(boom>0) 159 { 160 SlowDisplay("使用手雷,敌人HP减少100\n\n\n"); 161 boom--; 162 guai.health-=100; 163 AttackResult(); 164 } 165 } 166 else SlowDisplay("非战斗状态,不能使用手雷!\n\n\n"); 167 break; 168 case 6: 169 if(battle) //在战斗中(battle=1),否则(battle=0)不能使用攻击性物品 170 { 171 if(dubiao>0) 172 { 173 SlowDisplay("使用毒标,敌人HP减少200\n\n\n"); 174 dubiao--; 175 guai.health-=200; 176 AttackResult(); 177 } 178 } 179 else SlowDisplay("非战斗状态,不能使用毒标!\n\n\n"); 180 break; 181 case 7: 182 if(battle) //在战斗中(battle=1),否则(battle=0)不能使用攻击性物品 183 { 184 if(atom_boom>0) 185 { 186 SlowDisplay("使用手抛式原子弹,敌人HP减少500\n\n\n"); 187 atom_boom--; 188 guai.health-=500; 189 AttackResult(); 190 } 191 } 192 else SlowDisplay("非战斗状态,不能使用手抛式原子弹!\n\n\n"); 193 break; 194 case 0:break; 195 default:printf("ChooseWupin error!\n\n\n"); 196 } 197 } 198 int AttackResult() //攻击结果:判断是否获胜 是否获得物品 和 是否升级 199 { 200 if(guai.health<=0) 201 { 202 battle=0; 203 printf("战斗胜利!获得金币%d,经验%d\n\n\n",guai.money,guai.exp); 204 player.exp+=guai.exp; 205 player.range_exp+=guai.exp; 206 money+=guai.money; 207 s=SuiJi(); 208 if(s<guai.wupinpro) 209 { 210 SlowDisplay("从敌人尸骸中发现"); 211 printf("%s\n\n\n",guai.wupin); 212 AddWupin(guai.wupin_sign); 213 } 214 WhetherLevelUp(); 215 if(strcmp(guai.name,"使徒")==0) 216 { 217 SlowDisplay("神秘老人(神色大变):\n\n\n 什么?你竟然打败了我的使徒?你到底是谁?怎么会这么厉害?\n\n\n"); 218 printf("%s",player.name); 219 SlowDisplay(" : \n\n\n 嘿嘿,反正你也快死了,我就告诉你一条消息吧。我就是传说中的勇者!你想打败我?没门!基于你的“良好”表现,我决定:杀了你,以绝后患!\n\n\n\n\n\n 这时,"); 220 printf("%s",player.name); 221 SlowDisplay("的手里发出一道白光,那白光直直的击中了老人,神秘老人和他手中的巨人之臂双双飞出,不过巨人之臂掉到了 "); 222 printf("%s",player.name); 223 SlowDisplay(" 的脚下,而神秘老人狠狠的撞到了岩石上,一口鲜血喷出......\n\n\n\n\n\n ......\n\n\n\n\n\n "); 224 printf("%s",player.name); 225 SlowDisplay("缓缓的走到神秘老人的面前,说:\n\n\n 现在,用你的生命来偿还吧!\n\n\n\n\n 老人惊恐的瞪着"); 226 printf("%s",player.name); 227 SlowDisplay(",然后化作一道白光,逃走了......\n\n\n\n\n 这时,一个铁笼出现在"); 228 printf("%s",player.name); 229 SlowDisplay("的面前,里面正关着泪流满面的公主。\n\n\n\n公主:\n\n\n 勇士,快来救我!\n\n\n"); 230 printf("%s",player.name); 231 SlowDisplay(" (皱眉头):\n\n\n 我该怎么救你呢?\n\n\n公主:\n\n\n 你把巨人之臂给我就行了。\n\n\n\n\n "); 232 printf("%s",player.name); 233 SlowDisplay("把巨人之臂通过铁笼的缝隙给了公主,公主用巨人之臂把锁的背面捅了两下,锁便开了,公主缓缓的走了出来,跟着你回到了皇宫^_^\n\n\n\n\n"); 234 puts("你完成了救出公主的任务,国王重赏了你!!!"); 235 getchar(); 236 getchar(); 237 exit(0); 238 } 239 return 1; //攻击有结果了返回1,否则返回0,用于判断是否继续做战斗行为 240 } 241 else 242 { 243 int s=SuiJi(); 244 245 246 if((guai.attack+s-player.defense/3)<0) 247 { 248 player.health-=1; 249 printf("%s反击,你的HP减少了 1\n\n",guai.name); 250 } 251 else 252 { 253 player.health-=guai.attack+s-player.defense/3; 254 printf("%s反击,你的HP减少了%d\n\n",guai.name,guai.attack+s-player.defense/3); 255 } 256 if(player.health<0) 257 { 258 if(strcmp(guai.name,"使徒")==0) 259 { 260 SlowDisplay("神秘老人(奸笑着):\n\n\n 嘿嘿,小子,想对付我的使徒你还嫩了点,现在,我要你的鲜血作为代价......哈哈哈哈哈哈...... \n\n\n"); 261 SlowDisplay("老人手一挥,使徒拿着钢刀冲过来......将刀对准了"); 262 printf("%s",player.name); 263 SlowDisplay("的脖子......然后......刀砍了下来......鲜血四溅......\n\n\n"); 264 battle=0; 265 printf("%s战死!金币掉落%d\n\n\n",player.name,player.level*500); 266 money-=player.level*500; 267 player.health=player.max_health/5; 268 SlowDisplay("\n\n\n\n\n\n\n 头好沉啊......怎么回事......我这是在哪里......\n 不对!我不是被使徒杀了吗?怎么复活了?不过把使徒杀了是我的任务!\n\n"); 269 SlowDisplay(" 你得到了国王的庇护,复活了!现在请重新开始任务吧!\n\n"); 270 } 271 else 272 { 273 battle=0; 274 printf("%s战死!金币掉落%d\n\n\n",player.name,player.level*500); 275 money-=player.level*500; 276 player.health=player.max_health/5; 277 } 278 OrdinaryAct();////////// 279 return 1; 280 } 281 } 282 return 0; 283 } 284 void AddWupin(int wupin_sign) 285 { 286 287 switch(wupin_sign) 288 { 289 case 1:fang4n++; break; 290 case 2:fang3n++; break; 291 case 3:fang2n++; break; 292 case 4:strongman_arm=1;break; 293 case 5:gong4n++; break; 294 case 6:gong3n++; break; 295 case 7:gong2n++; break; 296 default:printf("AddWupin error\n\n\n"); 297 } 298 299 } 300 void WhetherLevelUp() 301 { 302 int i=0,j=0; 303 int l1=player.range_exp/100; 304 int l2=player.range_exp/300; 305 int l3=player.range_exp/600; 306 if(player.level<=15&&l1>0) //15级以下,经验足够 都满足则升级 307 { 308 if(l1==1) 309 { 310 printf("%s",player.name); 311 SlowDisplay(" 升级!\n\n\n攻击力+3, 防御力+2, HP上限+20\n\n\n"); 312 player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100; 313 player.attack+=3; 314 player.defense+=2; 315 player.max_health+=20; 316 player.health=player.max_health; 317 player.level++; 318 player.range_exp=0; 319 player.exp=player.max_exp; 320 player.max_exp+=100; 321 } 322 else 323 { 324 printf("好厉害!连升%d级!",l1); 325 printf("攻击力+%d, 防御力+%d, HP上限+%d\n\n\n",3*l1,2*l1,20*l1); 326 player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100; 327 player.attack+=3*l1; 328 player.defense+=2*l1; 329 player.max_health+=20*l1; 330 player.health=player.max_health; 331 player.level+=l1; 332 player.range_exp=0; 333 player.exp=player.max_exp; 334 player.max_exp+=100*l1; 335 } 336 } 337 else if(player.level<=40&&l2>0) 338 { 339 if(l2==1) 340 { 341 printf("%s",player.name); 342 SlowDisplay(" 升级!\n\n\n攻击力+3, 防御力+2, HP上限+20\n\n\n"); 343 player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100; 344 player.attack+=3; 345 player.defense+=2; 346 player.max_health+=20; 347 player.health=player.max_health; 348 player.level++; 349 player.range_exp=0; 350 player.exp=player.max_exp; 351 player.max_exp+=300; 352 } 353 else 354 { 355 printf("好厉害!连升%d级!",l1); 356 printf("攻击力+%d, 防御力+%d, HP上限+%d\n\n\n",3*l2,2*l2,20*l2); 357 player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100; 358 player.attack+=3*l2; 359 player.defense+=2*l2; 360 player.max_health+=20*l2; 361 player.health=player.max_health; 362 player.level+=l2; 363 player.range_exp=0; 364 player.exp=player.max_exp; 365 player.max_exp+=300*l2; 366 } 367 } 368 else if(l3>0) 369 { 370 if(l3==1) 371 { 372 printf("%s",player.name); 373 SlowDisplay(" 升级!\n\n\n攻击力+3, 防御力+2, HP上限+20\n\n\n"); 374 player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100; 375 player.attack+=3; 376 player.defense+=2; 377 player.max_health+=20; 378 player.health=player.max_health; 379 player.level++; 380 player.range_exp=0; 381 player.exp=player.max_exp; 382 player.max_exp+=600; 383 } 384 else 385 { 386 printf("好厉害!连升%d级!",l1); 387 printf("攻击力+%d, 防御力+%d, HP上限+%d\n\n\n",3*l3,2*l3,20*l3); 388 player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100; 389 player.attack+=3*l3; 390 player.defense+=2*l3; 391 player.max_health+=20*l3; 392 player.health=player.max_health; 393 player.level+=l3; 394 player.range_exp=0; 395 player.exp=player.max_exp; 396 player.max_exp+=600*l3; 397 } 398 } 399 } 400 void OrdinaryAct() //正常行为菜单(移动,物品,对话,查看状态,装备,退出游戏) 401 { 402 403 while(1) 404 { 405 // \(1000); 406 // system("cls"); 407 puts("============================================================================="); 408 printf("要做什么?\n\n\n 1,移动 2,道具 3,对话 4,查看状态 5,装备 0,退出游戏\n\n\n"); 409 puts("============================================================================="); 410 switch(scanf("%d",&choose_number),choose_number) 411 { 412 case 1: //显示移动菜单 413 SlowDisplay("要去哪里?\n\n\n"); 414 printf("1,酒吧 2,旅馆 3,森林一层 4,森林二层 5,森林三层 6,草原一层 7,草原二层 8,草原三层\n9,商店\n\n\n"); 415 switch(scanf("%d",&choose_number),choose_number) 416 { 417 case 1:place_sign=place.bar; //记录目前位置-酒吧 418 // OrdinaryAct(); 419 break; 420 case 2:place_sign=place.hotel; //进入旅店 421 SlowDisplay("要住店吗? 200个金币 1,是 0,否\n\n\n"); 422 choose_number=1; 423 switch(scanf("%d",&choose_number),choose_number) 424 { 425 case 1:if(money-200<0) //判断钱是否够 426 { 427 SlowDisplay("Sorry,你的钱不够~\n\n\n"); 428 } 429 else 430 { 431 SlowDisplay("好好休息\n\tHP满\n\t\t第二天了\n\n"); 432 money-=200; //花费200住店费 433 player.health=player.max_health; //体力满 434 } 435 break; 436 case 0:SlowDisplay("下次再来!\n\n\n");break; 437 default:printf("hotel talk error!\n\n\n"); 438 } 439 place_sign=0; 440 break; 441 case 3: 442 place_sign=place.forest1; 443 s=SuiJi(); 444 if(s<7) 445 { 446 battle=1; 447 guai=xiyi; 448 printf("%s扑了过来!\n\n\n",guai.name); 449 BattleAct(); 450 } 451 else if(s<9) 452 { 453 battle=1; 454 guai=witch; 455 printf("%s扑了过来!\n\n\n",guai.name); 456 BattleAct(); 457 } 458 else 459 { 460 SlowDisplay("这里安全\n\n\n"); 461 //不用调用OAct函数,会自动执行OAct函数; 462 } 463 break; 464 case 4: 465 place_sign=place.forest2; 466 s=SuiJi(); 467 if(s<7) 468 { 469 battle=1; 470 guai=witch; 471 printf("%s扑了过来!\n\n\n",guai.name); 472 BattleAct(); 473 } 474 else if(s<9) 475 { 476 battle=1; 477 guai=strongman; 478 printf("%s扑了过来!\n\n\n",guai.name); 479 BattleAct(); 480 } 481 else 482 { 483 SlowDisplay("这里安全\n\n\n"); 484 } 485 break; 486 case 5: 487 place_sign=place.forest3; 488 s=SuiJi(); 489 if(s<7) 490 { 491 battle=1; 492 guai=strongman; 493 printf("%s扑了过来!\n\n\n",guai.name); 494 BattleAct(); 495 } 496 else if(s<9) 497 { 498 battle=1; 499 guai=big_strongman; 500 printf("%s扑了过来!\n\n\n",guai.name); 501 BattleAct(); 502 } 503 else 504 { 505 SlowDisplay("这里安全\n\n\n"); 506 } 507 break; 508 case 6: 509 place_sign=place.grass1; 510 s=SuiJi(); 511 if(s<7) 512 { 513 battle=1; 514 guai=bee; 515 printf("%s扑了过来!\n\n\n",guai.name); 516 BattleAct(); 517 } 518 else if(s<9) 519 { 520 battle=1; 521 guai=horse; 522 printf("%s扑了过来!\n\n\n",guai.name); 523 BattleAct(); 524 } 525 else 526 { 527 SlowDisplay("这里安全\n\n\n"); 528 } 529 break; 530 case 7: 531 place_sign=place.grass2; 532 s=SuiJi(); 533 if(s<7) 534 { 535 battle=1; 536 guai=horse; 537 printf("%s扑了过来!\n\n\n",guai.name); 538 BattleAct(); 539 } 540 else if(s<9) 541 { 542 battle=1; 543 guai=lion; 544 printf("%s扑了过来!\n\n\n",guai.name); 545 BattleAct(); 546 } 547 else 548 { 549 SlowDisplay("这里安全\n\n\n"); 550 } 551 break; 552 case 8: 553 place_sign=place.grass3; 554 s=SuiJi(); 555 if(s<7) 556 { 557 battle=1; 558 guai=lion; 559 printf("%s扑了过来!\n\n\n",guai.name); 560 BattleAct(); 561 } 562 else if(s<9) 563 { 564 battle=1; 565 if(strongman_arm) 566 { 567 SlowDisplay("神秘老人:\n\n\n 哈哈,年轻人,做的不错,不过...嘿嘿,你上当啦!巨人之臂我要了,公主你也别想带走!\n\n\n\n"); 568 printf("%s",player.name); 569 SlowDisplay(" :\n\n\n 你休想!我好不容易才拿到巨人之臂,你别想占我的便宜!\n\n\n神秘老人(脸色阴沉):\n\n\n 既然你这么不识相,那么也别怪我不客气了!使徒,上!\n\n\n\n"); 570 guai=shitu; 571 printf("%s扑了过来!\n\n\n",guai.name); 572 BattleAct(); 573 } 574 else SlowDisplay("神秘老人:\n\n\n 年轻人,你好啊.如果你有巨人之臂,我可以告诉你公主的下落哦~\n\n\n"); 575 } 576 else 577 { 578 SlowDisplay("这里安全\n\n\n"); 579 } 580 break; 581 case 9:place_sign=place.shop; 582 SlowDisplay("您好,请问您需要什么?\n\n 急救:1,止血草 500金币 2,急救包 600金币 3,云南白药 800金币 4,超级云南白药 1000金币\n 攻击:5,手雷 1000金币 6,毒标 2000金币 7,手抛式原子弹 5000金币\n 0,我只看看,不买\n\n\n"); 583 //choose_number=1; 584 switch(scanf("%d",&choose_number),choose_number) 585 { 586 case 1:if(money-500<0) //判断钱是否够 587 { 588 SlowDisplay(" 抱歉,你的钱好像不太够呢~\n\n去打怪吧\n\n\n"); 589 } 590 else 591 { 592 SlowDisplay("购买成功!\n\n谢谢光临!下次再来!~^_^~\n\n\n"); 593 money-=500; 594 cao++; 595 } 596 break; 597 case 2:if(money-600<0) //判断钱是否够 598 { 599 SlowDisplay(" 抱歉,你的钱好像不太够呢~\n\n去打怪吧\n\n\n"); 600 } 601 else 602 { 603 SlowDisplay("购买成功!\n\n谢谢光临!下次再来!~^_^~\n\n\n"); 604 money-=600; 605 jijiubao++; 606 } 607 break; 608 case 3:if(money-800<0) //判断钱是否够 609 { 610 SlowDisplay(" 抱歉,你的钱好像不太够呢~\n\n去打怪吧\n\n\n"); 611 } 612 else 613 { 614 SlowDisplay("购买成功!\n\n谢谢光临!下次再来!~^_^~\n\n\n"); 615 money-=800; 616 baiyao++; 617 } 618 break; 619 case 4:if(money-1000<0) //判断钱是否够 620 { 621 SlowDisplay(" 抱歉,你的钱好像不太够呢~\n\n去打怪吧\n\n\n"); 622 } 623 else 624 { 625 SlowDisplay("购买成功!\n\n谢谢光临!下次再来!~^_^~\n\n\n"); 626 money-=500; 627 superbaiyao++; 628 } 629 break; 630 case 5:if(money-1000<0) //判断钱是否够 631 { 632 SlowDisplay(" 抱歉,你的钱好像不太够呢~\n\n去打怪吧\n\n\n"); 633 } 634 else 635 { 636 SlowDisplay("购买成功!\n\n谢谢光临!下次再来!~^_^~\n\n\n"); 637 money-=500; 638 boom++; 639 } 640 break; 641 case 6:if(money-2000<0) //判断钱是否够 642 { 643 SlowDisplay(" 抱歉,你的钱好像不太够呢~\n\n去打怪吧\n\n\n"); 644 } 645 else 646 { 647 SlowDisplay("购买成功!\n\n谢谢光临!下次再来!~^_^~\n\n\n"); 648 money-=500; 649 dubiao++; 650 } 651 break; 652 case 7:if(money-5000<0) //判断钱是否够 653 { 654 SlowDisplay(" 抱歉,你的钱好像不太够呢~\n\n去打怪吧\n\n\n"); 655 } 656 else 657 { 658 SlowDisplay("购买成功!\n\n谢谢光临!下次再来!~^_^~\n\n\n"); 659 money-=500; 660 atom_boom++; 661 } 662 break; 663 case 0:SlowDisplay("谢谢光临!下次再来!~^_^~\n\n\n");break; 664 default:printf("好像没有这个货物呢\n\n\n"); 665 } 666 break; 667 default:printf("choose map error!\n\n\n"); 668 } 669 break; 670 case 2: ChooseWupin();break; //显示道具,并可以使用. 671 case 3: //对话选项 672 if(place_sign==place.bar) 673 { 674 SlowDisplay("要和谁说话?\n\n\n1,红发女郎 2,赏金猎人 3,酒吧老板\n\n\n"); //显示对话人物 675 switch(scanf("%d",&choose_number),choose_number) 676 { 677 case 1: 678 SlowDisplay("红发女郎:\n\n\n 吧台边那个Hunter好帅啊!(~脸红~)\n\n\n听说他经常外出打猎,外面的路他应该很熟悉的!\n\n\n"); 679 break; 680 case 2: 681 if(fang1n<1&&gong1n<1) 682 { 683 SlowDisplay("赏金猎人:\n\n\n 你要救公主啊!好胆量!\n\n\n 不过外面的丛林和草原很险恶,而且越深越危险,这是匕首和布衣,对你会有些帮助的,拿去吧!\n\n\n"); 684 printf("%s心想:(哇,这位大叔人真好啊!\n\n\n)",player.name); 685 gong1n++; 686 fang1n++; 687 } 688 else SlowDisplay("赏金猎人:\n\n\n 加油吧,年轻人!\n\n\n 不要被外面丛林和草原所吓倒!\n\n\n"); 689 break; 690 case 3: 691 printf("要喝点什么?\n\n\n 1,二锅头25金币 HP+20 2,XO酒80金币 HP+50 3,人头马面150金币 HP+100 0,返回\n\n\n"); 692 choose_number=1; 693 while(choose_number) 694 { 695 switch(scanf("%d",&choose_number),choose_number) 696 { 697 case 1:if(money<25) 698 { 699 SlowDisplay("钱不够!"); 700 } 701 else 702 { 703 if(player.health+20<=player.max_health) 704 { 705 SlowDisplay("HP+20."); 706 money-=25; 707 player.health+=20; 708 } 709 else 710 { 711 SlowDisplay("HP满了"); 712 player.health=player.max_health; 713 } 714 } 715 break; 716 case 2:if(money<80) 717 { 718 SlowDisplay("钱不够!"); 719 } 720 else 721 { 722 if(player.health+50<=player.max_health) 723 { 724 SlowDisplay("HP+50."); 725 money-=80; 726 player.health+=50; 727 } 728 else 729 { 730 SlowDisplay("HP满了"); 731 player.health=player.max_health; 732 } 733 } 734 break; 735 case 3:if(money<150) 736 { 737 SlowDisplay("钱不够!"); 738 } 739 else 740 { 741 if(player.health+100<=player.max_health) 742 { 743 SlowDisplay("HP+100."); 744 money-=150; 745 player.health+=100; 746 } 747 else 748 { 749 SlowDisplay("HP满了"); 750 player.health=player.max_health; 751 } 752 } 753 break; 754 case 0:SlowDisplay("下次再来!\n");break; 755 default:SlowDisplay("输入错误\n\n\n"); 756 } 757 } 758 } 759 } 760 else if(place_sign==place.hotel) 761 SlowDisplay("“老板娘!我...”\n\n\n“我忙着呢,没空理你~”\n\n\n"); 762 else SlowDisplay("这里好像没人可以聊天\n\n\n"); 763 break; 764 case 4: DisplayState(); break; //显示状态 765 case 5: //装备 766 printf("攻装: 1,匕首:%d个 2,长剑:%d个 3,碧血剑:%d个 4,绝世好剑:%d个\n\n\n",gong1n,gong2n,gong3n,gong4n); 767 printf("防装: 5,布衣:%d个 6,铁甲:%d个 7,银甲:%d个 8,黄金圣衣:%d个\t\t0,返回\n\n\n",fang1n,fang2n,fang3n,fang4n); 768 SlowDisplay("选择要装备的武器或防具:\n\n\n"); 769 switch(scanf("%d",&choose_number),choose_number) 770 { 771 case 1:if(gong1n>=1) 772 { 773 SlowDisplay("拿起了匕首\n\n\n"); 774 gong=gong1; 775 strcpy(gongname,"匕首"); 776 } 777 else SlowDisplay("你没有匕首可以装备\n\n\n"); 778 break; 779 case 2:if(gong2n>=1) 780 { 781 SlowDisplay("拿起了长剑\n\n\n"); 782 gong=gong2; 783 strcpy(gongname,"长剑"); 784 } 785 else SlowDisplay("你没有长剑可以装备\n\n\n"); 786 break; 787 case 3:if(gong3n>=1) 788 { 789 SlowDisplay("拿起了碧血剑\n\n\n"); 790 gong=gong3; 791 strcpy(gongname,"碧血剑"); 792 } 793 else SlowDisplay("你没有碧血剑可以装备\n\n\n"); 794 break; 795 case 4:if(gong4n>=1) 796 { 797 SlowDisplay("拿起了绝世好剑\n\n\n"); 798 gong=gong4; 799 strcpy(gongname,"绝世好剑"); 800 } 801 else SlowDisplay("你没有绝世好剑可以装备\n\n\n"); 802 break; 803 case 5:if(fang1n>=1) 804 { 805 SlowDisplay("穿上了布衣\n\n\n"); 806 fang=fang1; 807 strcpy(fangname,"布衣"); 808 } 809 else SlowDisplay("你没有布衣可以装备\n\n\n"); 810 break; 811 case 6:if(fang2>=1) 812 { 813 SlowDisplay("穿上了铁甲\n\n\n"); 814 fang=fang2; 815 strcpy(fangname,"铁甲"); 816 } 817 else SlowDisplay("你没有铁甲可以装备\n\n\n"); 818 break; 819 case 7:if(fang3n>=1) 820 { 821 SlowDisplay("穿上了银甲\n\n\n"); 822 fang=fang3; 823 strcpy(fangname,"银甲"); 824 } 825 else SlowDisplay("你没有银甲可以装备\n\n\n"); 826 break; 827 case 8:if(fang4n>=1) 828 { 829 SlowDisplay("穿上了黄金圣衣\n\n\n"); 830 fang=fang4; 831 strcpy(fangname,"黄金圣衣"); 832 } 833 else SlowDisplay("你没有黄金圣衣可以装备\n\n\n"); 834 break; 835 case 0:SlowDisplay("未更换装备\n\n\n");break; 836 default:printf("change error!"); 837 } 838 break; 839 case 0: 840 SlowDisplay("确定推出游戏?(Y/N)\n\n\n"); 841 getchar(); 842 proof=getchar(); 843 if(proof==‘y‘||proof==‘Y‘) 844 { 845 SlowDisplay("数据存储中..."); 846 //向文件中更新数据; 847 getchar(); 848 SlowDisplay("按回车退出"); 849 getchar(); 850 return; 851 } 852 else if(proof==‘n‘||proof==‘N‘)printf("继续游戏!\n\n\n"); 853 else SlowDisplay("继续!\n\n\n"); 854 break; 855 default:SlowDisplay("输入错误!\n\n\n"); 856 } 857 } 858 } 859 void DisplayState() 860 { 861 printf("%s 攻击力:%d+%d=%d 防御力:%d+%d=%d HP:%d/%d \n\n\n",player.name,player.attack,gong,player.attack+gong,player.defense,fang,player.defense+fang,player.health,player.max_health); 862 printf("武器: %s 防具: %s \n\n\n",gongname,fangname); 863 printf("等级:%d 经验:%d/%d 还需要%d经验升级 金币:%d \n\n\n",player.level,player.exp,player.max_exp,player.max_exp-player.exp,money); 864 } 865 void BattleAct() 866 { 867 while(1) 868 { 869 puts("============================================================================="); 870 printf("要怎么办?\n\n\n 1,攻击 2,物品 3,查看状态 4,逃跑\n\n\n"); 871 switch(scanf("%d",&choose_number),choose_number) 872 { 873 case 1: 874 s=SuiJi(); 875 printf("%s攻击! %sHP减少%d\n\n\n",player.name,guai.name,player.attack+s+gong-guai.defense/3); 876 guai.health-=player.attack+s+gong-guai.defense/3; 877 if(AttackResult())return; //如果攻击有结果(敌人或玩家战死)退出函数 878 else continue; 879 case 2: ChooseWupin();break; //选择物品,可以使用,战斗中允许使用攻击性物品 880 case 3:DisplayState();break; //显示状态 881 case 4: 882 s=SuiJi(); 883 if(s<4) //40%的概率可以逃跑 884 { 885 printf("%s逃跑了~\n\n\n",player.name); 886 battle=0; 887 return; 888 } 889 else printf("%s逃跑失败!\n\n\n",player.name); 890 break; 891 default:SlowDisplay("输入错误,重新输入!\n\n\n"); 892 } 893 } 894 } 895 void SlowDisplay(char *p) 896 { 897 while(1) 898 { 899 if(*p!=0) 900 printf("%c",*p++); 901 else 902 break; 903 Sleep(50); 904 } 905 }
以上是关于改进版游戏代码的主要内容,如果未能解决你的问题,请参考以下文章