博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【非凡程序员】 OC第十六节课 初识IOS开发二 (打地鼠的游戏)
阅读量:5824 次
发布时间:2019-06-18

本文共 3744 字,大约阅读时间需要 12 分钟。

hot3.png

这是一个打地鼠的游戏:一共拖了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

转载于:https://my.oschina.net/u/2366900/blog/465806

你可能感兴趣的文章
java程序
查看>>
java 继承还是组合
查看>>
dubbo框架原理
查看>>
《暗时间》
查看>>
微信 单页面 分享功能 问题 记录
查看>>
OpenWRT开发之——对C++的支持(解决库依赖问题)【转】
查看>>
RESTful 个人理解总结【转】
查看>>
Android 动态改变图片的颜色值
查看>>
RabbitMQ指南之一:"Hello World!"
查看>>
mybatis plus 代码生成器
查看>>
C++编译错误:multiple types in one declaration
查看>>
Eclipse常用快捷键大全
查看>>
3Dtouch 的实际应用详解(tableView中的应用)
查看>>
团队作业第二次—项目选题报告
查看>>
Linux 磁盘管理
查看>>
[洛谷P3413]SAC#1 - 萌数
查看>>
使用Nito.AsyncEx实现异步锁
查看>>
MySQL的分区、分表、集群
查看>>
iOS第三方解决键盘遮挡-IQKeyboardManager
查看>>
程序员必须知道的git托管平台
查看>>