【非凡程序员】 OC第十六节课 初识IOS开发二 (打地鼠的游戏)
【非凡程序员】 OC第十六节课 初识IOS开发二 (打地鼠的游戏)
七月流火zsp 发表于3年前
【非凡程序员】 OC第十六节课 初识IOS开发二 (打地鼠的游戏)
  • 发表于 3年前
  • 阅读 1007
  • 收藏 1
  • 点赞 1
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 非凡程序员,感谢 黄杨超 IOS指导。总结:张岁平学习笔记

这是一个打地鼠的游戏:一共拖了9个Buttor按钮,设置地鼠间隔一定时间随机出现,鼠标点一次加一分。

 

ViewController.h文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *name9;
@property (weak, nonatomic) IBOutlet UIButton *name8;
@property (weak, nonatomic) IBOutlet UIButton *name7;
@property (weak, nonatomic) IBOutlet UIButton *name6;
@property (weak, nonatomic) IBOutlet UIButton *name5;
@property (weak, nonatomic) IBOutlet UIButton *name4;
@property (weak, nonatomic) IBOutlet UIButton *name3;
@property (weak, nonatomic) IBOutlet UIButton *name2;
@property (weak, nonatomic) IBOutlet UIButton *name1;

-(void)play;

- (IBAction)DiDong9:(id)sender;
- (IBAction)DiDong8:(id)sender;
- (IBAction)DiDong7:(id)sender;
- (IBAction)DiDong6:(id)sender;
- (IBAction)DiDong5:(id)sender;
- (IBAction)DiDong4:(id)sender;
- (IBAction)DiDong3:(id)sender;
- (IBAction)DiDong2:(id)sender;
- (IBAction)DiDong1:(id)sender;
@property (nonatomic,strong) NSTimer * time;

@property (weak, nonatomic) IBOutlet UILabel *score;

@end

ViewController.m文件:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _time =[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(play) userInfo:nil repeats:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

int saveInt;
-(void)play
{
    int suiji = arc4random()%9;
    saveInt = suiji;    

    //将9个地洞放在一个数组里,每次随机一个地鼠出来
    NSArray *array = @[_name1, _name2, _name3, _name4, _name5, _name6,_name7, _name8, _name9];
    for (int i = 0; i < 9; i++) {
        [array[i] setTitle:@"" forState:UIControlStateNormal];
        [array[suiji] setTitle:@"地鼠" forState:UIControlStateNormal];
        }
}

- (IBAction)DiDong9:(id)sender {
    if (saveInt == 8) {

     //判断每次鼠标点击的与随机出现的地鼠是否一致,如果一致加一分
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] + 1];
    }

}

- (IBAction)DiDong8:(id)sender {
    if (saveInt == 7) {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] + 1];
    }
}

- (IBAction)DiDong7:(id)sender {
    if (saveInt == 6) {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] + 1];
    }else
    {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] - 1];
    }
}

- (IBAction)DiDong6:(id)sender {
    if (saveInt == 5) {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] + 1];
    }else
    {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] - 1];
    }
}

- (IBAction)DiDong5:(id)sender {
    if (saveInt == 4) {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] + 1];
    }else
    {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] - 1];
    }
}

- (IBAction)DiDong4:(id)sender {
    if (saveInt == 3) {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] + 1];
    }else
    {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] - 1];
    }
}

- (IBAction)DiDong3:(id)sender {
    if (saveInt == 2) {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] + 1];
    }else
    {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] - 1];
    }
}

- (IBAction)DiDong2:(id)sender {
    if (saveInt == 1) {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] + 1];
    }else
    {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] - 1];
    }
}

- (IBAction)DiDong1:(id)sender {
    NSLog(@"%d", saveInt);
    if (saveInt == 0) {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] + 1];
    }else
    {
        _score.text = [NSString stringWithFormat:@"%d", [_score.text intValue] - 1];
    }
    
}
@end

标签: 非凡程序员
共有 人打赏支持
粉丝 10
博文 29
码字总数 11273
×
七月流火zsp
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: