编写了一个在Xcode控制台上运行的简单小游戏,本来打算写到100关(⊙o⊙)…后来发现没有好的idea,就先写了2关,目的在于练练手,理清类与类之间的关系。有好的想法和有空的话会慢慢写到100关。游戏并不复杂,但有很多细节需要处理好。
本游戏包括主函数、Game类、Hero类、Monster类和Pass类 全都附带注释
下面直接上代码
主函数
main.h
#import <Foundation/Foundation.h>
#import "Monster.h"
#import "Pass.h"
#import "Hero.h"
#import "Game.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Game *g1 = [Game new]; //分别创建游戏 英雄 关卡 怪兽对象
Hero *h1 = [Hero new];
Pass *p1 = [Pass new];
Monster *m1 = [Monster new];
[g1 showGame]; //游戏介绍
[NSThread sleepForTimeInterval:2]; //延时2秒
NSLog(@"请选择您喜欢的英雄:1.德玛西亚 2.赵信 3.亚索 4.奥巴马");
int a;
scanf("%d",&a);
h1.nowHP = 100; //设置英雄当前HP值
h1.heroLV = 1; //设置英雄初始等级
h1.upexp = 100; //设置英雄升级所需的经验
h1.ATK = 50; //英雄攻击力
h1.DEF = 10; //英雄防御力
m1.nowHP=100; //怪兽当前的HP值
m1.ATK = 30; //怪兽攻击力
m1.DEF =10; //怪兽防御力
[h1 showHeroName:a]; //显示选中的英雄名字
NSString *str1 = [h1 showJN:a]; //显示该英雄拥有的技能
NSLog(@"%@",str1);
NSLog(@"Ready~");
for(int b = 3 ; b>0 ; b--)
{
NSLog(@"%d",b);
[NSThread sleepForTimeInterval:1];
}
NSLog(@"游戏开始");
printf("\n");
p1.passNum = 1; //设置当前关卡数为1
[p1 showPassWithPassName:@"《幽暗密林》" andMonsterName:@"哥布林" andPassLV:@"★☆☆☆☆" andThePassStory:"1000年前,一场突如其来的异变,让原本平静和谐的阿拉德大陆卷入血雨腥风之中,动物植物在一夕之间纷纷魔化成邪恶的怪物,它们凶残嗜血,肆意吞噬人类的生命,死亡和恐惧迅速在阿拉德大陆蔓延" andTheMonster:m1]; //显示关卡名字、关卡怪兽、关卡难度、及关卡背景故事
printf("\n");
printf("\n");
[m1 shout]; //怪兽叫吼 表示怪兽登场
[NSThread sleepForTimeInterval:1];
[h1 shout]; //英雄回应
[NSThread sleepForTimeInterval:1];
[h1 fightMonster:m1]; //英雄开始打怪兽
[NSThread sleepForTimeInterval:2];
printf("\n");
// -----------------------第二关------------------------
[NSThread sleepForTimeInterval:3];
NSLog(@"按任意数字继续,按0退出"); //继续游戏的请求
int s;
scanf("%d",&s);
if(s!=0) //输入非0的数字 继续游戏
{
Monster *m2 = [Monster new]; //创建第二关的怪兽
[p1 showPassWithPassName:@"《丛林丧尸》" andMonsterName:@"僵尸" andPassLV:@"★★☆☆☆" andThePassStory:"在遥远的北方丛林,有一个叫做诺斯玛尔的村庄。在毫无征兆的情况下突然爆发黑死病,村里的人全部死亡,无一幸免。帝国派遣特使前往调查,却找不到死者的尸体..." andTheMonster:m2];
//显示关卡名字、关卡怪兽、关卡难度、及关卡背景故事
printf("\n");
int k = [m2 fistGame]; //与怪兽进行猜拳游戏 如果玩家此关卡游戏失败,则返回0
if( k == 0) //返回0代表游戏失败
{
exit(0); //直接结束程序 游戏不再执行下去
}
else //否则代表游戏胜利
{
[h1 fightWinRewardAndThePassLV:@"★★☆☆☆"]; //调用战斗胜利的奖励方法 获得相应奖励
}
}
else //输入0 则退出游戏
{
NSLog(@"下次再玩~");
exit(0); //结束程序
}