這是一個打地鼠的遊戲:一共拖了9個Buttor按鈕,設置地鼠間隔必定時間隨機出現,鼠標點一次加一分。數組
ViewController.h文件:dom
#import <UIKit/UIKit.h>ui
@interface ViewController : UIViewControlleratom
@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;spa
-(void)play;orm
- (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;get
@endstring
ViewController.m文件:it
#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