1
定义游戏相关类: 1. 怪物类:当前生命值,原始生命值,当前位置,原始位置,攻击力,防御力,移动行为,攻击英雄行为,逃跑行为... ...。 2. 关卡类:关卡名称,关卡编号,关卡时间,关卡怪物们。 3. 英雄类:角色名称,等级,经验,当前生命值,原始生命值,当前位置,原始位置,移动行为,攻击怪物行为... ...。 4. 游戏类:游戏名称,游戏描述,游戏关卡,当前英雄。
实体类中的一些数据变量封装成属性。并做正确性检测。
Monster.h
@interface Monster : NSObject { int _HP; //当前hp int _oriHP; //原始hp CGPoint _currentLoc; //当前位置 CGPoint _oriLoc; //原始位置 int _attack; //攻击 int _defense; //防御 } @property(nonatomic,assign) int HP; @property(nonatomic,assign,readonly) int oriHP; @property(nonatomic,assign) CGPoint currentLoc; @property(nonatomic,assign,readonly) CGPoint oriLoc; @property(nonatomic,assign) int attack; @property(nonatomic,assign) int defense; -(Monster *) initWithHP:(int)aHP Loc:(CGPoint)aPoint Attack:(int)aAttack Defense:(int)aDefense; -(void) move; -(void) attack:(id)aHero; -(void) escape; @end
Monster.m
#import "Monster.h" #import "Hero.h" @implementation Monster @synthesize HP = _HP,oriHP = _oriHP,oriLoc = _oriLoc,currentLoc = _currentLoc,attack = _attack,defense = _defense; -(Monster *) initWithHP:(int)aHP Loc:(CGPoint)aPoint Attack:(int)aAttack Defense:(int)aDefense; { self = [super init]; if (self) {
//当前的值不用变,因此这样赋值 _oriHP = 100; CGPoint p = {100,100}; //CGPoint的实质是float结构体,结构的的赋值方式是这样。 _oriLoc = p; self.HP = aHP; self.currentLoc = aPoint; self.attack = aAttack; self.defense = aDefense; } return self; } -(void) move { CGPoint p = self.currentLoc; p.x++; p.y++; self.currentLoc = p; } -(void) attack:(Hero *)aHero { aHero.HP -= self.attack; } -(void) escape { NSLog(@"逃跑"); } @end
Hero.h
@interface Hero : NSObject { NSString *_name; int _HP; //当前 int _oriHP; CGPoint _currentLoc; CGPoint _oriLoc; int _attack; int _defense; } @property(nonatomic,retain) NSString *name; @property(nonatomic,assign) int HP; @property(nonatomic,assign,readonly) int oriHP; @property(nonatomic,assign) CGPoint currentLoc; @property(nonatomic,assign,readonly) CGPoint oriLoc; @property(nonatomic,assign) int attack; @property(nonatomic,assign) int defense; -(Hero *) initWithHP:(int)aHP Loc:(CGPoint)aPoint Attack:(int)aAttack Defense:(int)aDefense HeroName:(NSString *)aName; -(void) move; -(void) attack:(id)aMonster; -(void) escape; @end
Level.m
#import "Hero.h" #import "Monster.h" @implementation Hero @synthesize name = _name,HP = _HP,oriHP = _oriHP, oriLoc = _oriLoc,currentLoc = _currentLoc, attack = _attack,defense = _defense; -(Hero *) initWithHP:(int)aHP Loc:(CGPoint)aPoint Attack:(int)aAttack Defense:(int)aDefense HeroName:(NSString *)aName; { self = [super init]; if (self) { _oriHP = 100; CGPoint p = {100,100}; _oriLoc = p; self.HP = aHP; self.currentLoc = aPoint; self.attack = aAttack; self.defense = aDefense; } return self; } -(void) move { CGPoint p = self.currentLoc; p.x++; p.y++; self.currentLoc = p; } -(void) attack:(Monster *)aMonster { aMonster.HP -= self.attack; } -(void) escape { NSLog(@"逃跑"); } @end
Level.h
@interface Level : NSObject { } @property(nonatomic,retain)NSString *name; @property(nonatomic,retain)NSString *code; @property(nonatomic,retain)NSString *time; -(Level *)initWithNum:(int)aNum; @end
Game.m
#import "Level.h" #import "Monster.h" @implementation Level @synthesize code = _code,name = _name,time = _time; -(Level *)initWithNum:(int)aNum { self = [super init]; if (self) { //默认妖怪 Monster *mons[aNum]; for (int i = 0; i < aNum; i++) { Monster *m = [[Monster alloc]init]; mons[i] = m; } } return self; } @end
Game.h
#import "Hero.h" #import "Level.h" #import "Monster.h" @interface Game : NSObject { Hero *_hero; Level *_level; } @property(nonatomic,retain)Level *level; @property(nonatomic,retain)Hero *hero; @end
AppDelegate.h(定义全局变量)
#import "Monster.h" #import "Hero.h" @interface AppDelegate : UIResponder <UIApplicationDelegate> { Monster *m; Hero *h;
NSTimer *t;
} @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
#import "AppDelegate.h" #import "Hero.h" #import "Monster.h" #import "Level.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; //................................................................. CGPoint p1 = {100,100}; m = [[Monster alloc]initWithHP:100 Loc:p1 Attack:50 Defense:50]; h = [[Hero alloc]initWithHP:100 Loc:p1 Attack:100 Defense:100 HeroName:@"超人"]; [h attack:m]; NSLog(@"妖怪的生命:%d",m.HP); //.................................................................
t = [NSTimerscheduledTimerWithTimeInterval:1
target:self
selector:@selector(MoveAndAttack)
userInfo:nil//方法间传递参数用
repeats:YES];
//........................................................................
[self.window makeKeyAndVisible]; return YES; }
-(void)MoveAndAttack
{
[h move];
[h attack:m];
NSLog(@"loc %@",NSStringFromCGPoint(h.currentLoc));
NSLog(@"怪兽生命值为%d",m.hp);
if (m.hp == 90)
{
[t invalidate];
}
}