打僵尸代码

本文通过定义普通僵尸、路障僵尸及铁桶僵尸类,演示了如何在Objective-C中实现游戏对象的创建与交互。包括设定血量、失血量等属性,并模拟攻击过程直至僵尸死亡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import

import “NormalZombie.h”

import “MetalPailZombie.h”

import “RoadBlockZombie.h”

int main(int argc, const char * argv[]) {

@autoreleasepool {
    /*1、定义普通僵尸类:
    作业2
    实例变量:僵尸总血量、僵尸每次失血量。
    方法:初始化方法(总血量)、被打击失血、死亡。
    2、定义路障僵尸类: 实例变量:僵尸总血量、僵尸每次失血量,道具,弱点。
    方法:初始化方法(总血量)、被打击失血、失去装备、死亡。 3、定义铁桶僵尸类:
    实例变量:僵尸总血量、僵尸每次失血量,道具,弱点。
    方法:初始化方法(总血量)、被打击失血、失去装备、死亡。
    4、在main.m中创建普通僵尸对象,设置总血量50,每次失血量为 3,没有道具。 5、在main.m中创建路障僵尸对象,设置总血量80,每次失血量为 2,设置道具为路障。 6、在main.m中创建铁桶僵尸对象,设置总血量120,每次失血量为 1,设置道具为铁桶*/
    NormalZombie *n1 = [[NormalZombie alloc] initWithBlood:50 lostBlood:3];
    RoadBlockZombie *r1 = [[RoadBlockZombie alloc] initWithBlood:80 lostBlood:2 weakPoint:nil equipment:@"路障"];
    MetalPailZombie *m1 = [[MetalPailZombie alloc] initWithBlood:120 lostBlood:1 weakPoint:nil equipment:@"铁桶"];
    while ([n1 blood] != 0 || [r1 blood] != 0 || [m1 blood] != 0) {
        [n1 beated];
        [r1 beated];
        [m1 beated];
    }

return 0;

}

import

import “NormalZombie.h”

@implementation NormalZombie
- (void)setBlood:(NSInteger)blood
{
_blood = blood;
}
- (NSInteger)blood
{
return _blood;
}

  • (void)setLoseBlood:(NSInteger)loseBlood
    {
    _loseBlood = loseBlood;
    }
  • (NSInteger)loseBlood
    {
    return _loseBlood;
    }

  • (instancetype)initWithBlood:(NSInteger)blood lostBlood:(NSInteger)lostBlood
    {
    _blood = blood;
    _loseBlood = lostBlood;
    return self;
    }

  • (void)beated
    {
    if (_blood != 0) {
    _blood -= _loseBlood;
    if (_blood < 0) {
    _blood = 0;
    }
    if (_blood == 0)
    {
    NSLog(@”普通僵尸被打死了!”);
    }
    else
    {
    NSLog(@”普通僵尸还有 %ld 滴血”, _blood);
    }
    }
    else {
    NSLog(@”打不了了 普通僵尸已经屎了”);
    }
    }

@end

@interface MetalPailZombie : NSObject
{
NSInteger _blood;
NSInteger _loseBlood;
NSString *_weakPoint;
NSString *_equipment;
}
- (void)setBlood:(NSInteger)blood;
- (NSInteger)blood;

  • (void)setLoseBlood:(NSInteger)loseBlood;
  • (NSInteger)loseBlood;

  • (void)setWeakPoint:(NSString *)weakPoint;

  • (NSString *)weakPoint;

  • (void)setEquipment:(NSString *)equipment;

  • (NSString *)equipment;

  • (void)beated;

  • (instancetype)initWithBlood:(NSInteger)blood lostBlood:(NSInteger)lostBlood weakPoint:(NSString )weakPoint equipment:(NSString )equipment;
    @end

import “MetalPailZombie.h”

@implementation MetalPailZombie
- (void)setBlood:(NSInteger)blood
{
_blood = blood;
}
- (NSInteger)blood
{
return _blood;
}
- (void)setLoseBlood:(NSInteger)loseBlood
{
_loseBlood = loseBlood;
}
- (NSInteger)loseBlood
{
return _loseBlood;
}

  • (void)setWeakPoint:(NSString *)weakPoint
    {
    _weakPoint = weakPoint;
    }
  • (NSString *)weakPoint
    {
    return _weakPoint;
    }

  • (void)setEquipment:(NSString *)equipment
    {
    _equipment = equipment;

}
- (NSString *)equipment
{
return _equipment;
}

  • (void)beated
    {
    if (_blood != 0) {
    _blood -= _loseBlood;
    if (_blood <= 50 && [_equipment isEqual:@”铁桶”]) {
    _equipment = nil;
    _loseBlood = 3;

    }
    if (_blood < 0) {
        _blood = 0;
    }
    if (_blood == 0)
    {
        NSLog(@"铁桶僵尸被打死了..");
    }
    else
    {
        NSLog(@"铁桶僵尸装备: %@, 失血量: %ld, 还有 %ld 滴血", _equipment, _loseBlood, _blood);
    }
    

    }
    else{
    NSLog(@”别打了,铁桶僵尸已经死了”);
    }
    }

  • (instancetype)initWithBlood:(NSInteger)blood lostBlood:(NSInteger)lostBlood weakPoint:(NSString )weakPoint equipment:(NSString )equipment
    {
    _blood = blood;
    _loseBlood = lostBlood;
    _weakPoint = weakPoint;
    _equipment = equipment;
    return self;
    }

@end

import

import “RoadBlockZombie.h”

@implementation RoadBlockZombie
- (void)setBlood:(NSInteger)blood
{
_blood = blood;
}
- (NSInteger)blood
{
return _blood;
}
- (void)setLostBlood:(NSInteger)lostBlood
{
_loseBlood = lostBlood;

}
- (NSInteger)lostBlood
{
return _loseBlood;
}

  • (void)setWeakPoint:(NSString *)weakPoint
    {
    _weakPoint = weakPoint;
    }
  • (NSString *)weakPoint
    {
    return _weakPoint;
    }
  • (void)setEquipment:(NSString *)equipment
    {
    _equipment = equipment;
    }
  • (NSString *)equipment
    {
    return _equipment;
    }

  • (void)beated
    {
    if (_blood != 0)
    {
    _blood -= _loseBlood;
    if (_blood <= 50 && [_equipment isEqual: @”路障”]) {
    _equipment = nil;
    _loseBlood = 3;
    }
    if (_blood < 0) {
    _blood = 0;
    }
    if (_blood == 0) {
    NSLog(@”路障僵尸被打死了”);
    }
    else
    {
    NSLog(@”路障僵尸的装备是 %@, 失血量是 %ld, 还有 %ld 滴血”, _equipment, _loseBlood, _blood);
    }

    }
    else
    {
    NSLog(@”别打了, 路障僵尸已经死了”);
    }

}

  • (instancetype)initWithBlood:(NSInteger)blood lostBlood:(NSInteger)lostBlood weakPoint:(NSString )weakPoint equipment:(NSString )equipment
    {
    _equipment = equipment;
    _blood = blood;
    _loseBlood = lostBlood;
    _weakPoint = weakPoint;
    return self;
    }

@end

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值