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