iOS小游戏打地鼠

时间:2023-03-10 00:35:31
iOS小游戏打地鼠
 #import "ViewController.h"

 #import <AudioToolbox/AudioToolbox.h>

 @interface ViewController ()

 @property (weak, nonatomic) IBOutlet UIImageView *imageview;

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

 @end

 @implementation ViewController 

 - (void)viewDidLoad {

     [super viewDidLoad];

     UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doaction:)];

     [self.view addGestureRecognizer:tap];

     [NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(show) userInfo:nil repeats:YES];

     // Do any additional setup after loading the view, typically from a nib.

 }

 -(void)doaction:(UITapGestureRecognizer *)tap

 {

     CGPoint aa=[tap locationInView:self.view];

     if (_imageview.frame.origin.x<=aa.x&&aa.x<=_imageview.frame.origin.x+&&_imageview.frame.origin.y<=aa.y&&aa.y<=_imageview.frame.origin.y+) {

         SystemSoundID systemsoundID;

         NSString *file=[[NSBundle mainBundle]pathForResource:@"" ofType:@"mp3"];

         AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:file], &systemsoundID);

         AudioServicesPlaySystemSound(systemsoundID);

         i++;

         _label.text=[NSString stringWithFormat:@"%i",i];

     }

 }

 -(void)show{

     int  j=arc4random()%; //产生随机值

     switch (j) {

         case :

             _imageview.frame=CGRectMake(, , , );

             break;

         case :

             _imageview.frame=CGRectMake(, , , );

             break;

         case :

             _imageview.frame=CGRectMake(, , , );

             break;

         case :

             _imageview.frame=CGRectMake(, , , );

             break;

         case :

             _imageview.frame=CGRectMake(, ,, );

             break;

         case :

             _imageview.frame=CGRectMake(, , , );

             break;

         case :

             _imageview.frame=CGRectMake(, , , );

             break;

         case :

             _imageview.frame=CGRectMake(, , , );

             break;

         default:

             _imageview.frame=CGRectMake(, , , );

             break;

     }    

 }

iOS小游戏打地鼠