簡易打地鼠遊戲

一.ViewController.h中代碼程序員

//
//  ViewController.h
//  打地鼠
//
//  Created by 非凡程序員 on 15/6/9.
//  Copyright (c) 2015年 wzhen. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (IBAction)play:(id)sender;
- (IBAction)hole1:(id)sender;
- (IBAction)hole2:(id)sender;
- (IBAction)hole3:(id)sender;
- (IBAction)hole4:(id)sender;
- (IBAction)hole5:(id)sender;
- (IBAction)hole6:(id)sender;
- (IBAction)hole7:(id)sender;
- (IBAction)hole8:(id)sender;
- (IBAction)hole9:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *mouse1;
@property (weak, nonatomic) IBOutlet UIButton *mouse2;
@property (weak, nonatomic) IBOutlet UIButton *mouse3;
@property (weak, nonatomic) IBOutlet UIButton *mouse4;
@property (weak, nonatomic) IBOutlet UIButton *mouse5;
@property (weak, nonatomic) IBOutlet UIButton *mouse6;
@property (weak, nonatomic) IBOutlet UIButton *mouse7;
@property (weak, nonatomic) IBOutlet UIButton *mouse8;
@property (weak, nonatomic) IBOutlet UIButton *mouse9;
@property (weak, nonatomic) IBOutlet UILabel *score;


@end

二.ViewController.m中代碼atom

//
//  ViewController.m
//  打地鼠
//
//  Created by 非凡程序員 on 15/6/9.
//  Copyright (c) 2015年 wzhen. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

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

 int randnumber;
- (IBAction)play:(id)sender {
//    NSMutableArray *arry=[NSMutableArray arrayWithObjects:@"_mouse1",@"_mouse2",@"_mouse3",@"_mouse4",@"_mouse5",@"_mouse6",@"_mouse7",@"_mouse8",@"_mouse9", nil];
    NSArray *arry=@[_mouse1,_mouse2,_mouse3,_mouse4,_mouse5,_mouse6,_mouse7,_mouse8,_mouse9];
    randnumber=rand() %9+1;
    for (int i=0; i<9; i++) {
        [arry[i] setTitle:@"洞" forState:UIControlStateNormal];
        [arry[randnumber-1] setTitle:@"地鼠" forState:UIControlStateNormal];
    }
}

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

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

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

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

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

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

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

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

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


@end
相關文章
相關標籤/搜索