iOS小游戏打地鼠

  1 #import "ViewController.h"
  2 
  3 #import <AudioToolbox/AudioToolbox.h>
  4 
  5 @interface ViewController ()
  6 
  7 @property (weak, nonatomic) IBOutlet UIImageView *imageview;
  8 
  9 @property (weak, nonatomic) IBOutlet UILabel *label;
 10 
 11 @end
 12 
 13 @implementation ViewController 
 14 
 15 - (void)viewDidLoad {
 16 
 17     [super viewDidLoad];
 18 
 19     UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doaction:)];
 20 
 21     [self.view addGestureRecognizer:tap];
 22 
 23     [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(show) userInfo:nil repeats:YES];
 24 
 25     // Do any additional setup after loading the view, typically from a nib.
 26 
 27 }
 28 
 29 -(void)doaction:(UITapGestureRecognizer *)tap
 30 
 31 {
 32 
 33     CGPoint aa=[tap locationInView:self.view];
 34 
 35     if (_imageview.frame.origin.x<=aa.x&&aa.x<=_imageview.frame.origin.x+64&&_imageview.frame.origin.y<=aa.y&&aa.y<=_imageview.frame.origin.y+66) {
 36 
 37         SystemSoundID systemsoundID;
 38 
 39         NSString *file=[[NSBundle mainBundle]pathForResource:@"3" ofType:@"mp3"];
 40 
 41         AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:file], &systemsoundID);
 42 
 43         AudioServicesPlaySystemSound(systemsoundID);
 44 
 45         i++;
 46 
 47         _label.text=[NSString stringWithFormat:@"%i",i];
 48 
 49     }
 50 
 51     
 52 
 53 }
 54 
 55 -(void)show{
 56 
 57     int  j=arc4random()%9; //产生随机值
 58 
 59     switch (j) {
 60 
 61         case 0:
 62 
 63             _imageview.frame=CGRectMake(114, 157, 78, 86);
 64 
 65             break;
 66 
 67         case 1:
 68 
 69             _imageview.frame=CGRectMake(139, 80, 78, 86);
 70 
 71             break;
 72 
 73         case 2:
 74 
 75             _imageview.frame=CGRectMake(106, 247, 78, 86);
 76 
 77             break;
 78 
 79         case 3:
 80 
 81             _imageview.frame=CGRectMake(316, 250, 78, 86);
 82 
 83             break;
 84 
 85         case 4:
 86 
 87             _imageview.frame=CGRectMake(316, 164,78, 86);
 88 
 89             break;
 90 
 91         case 5:
 92 
 93             _imageview.frame=CGRectMake(311, 81, 78, 86);
 94 
 95             break;
 96 
 97         case 6:
 98 
 99             _imageview.frame=CGRectMake(492, 82, 78, 86);
100 
101             break;
102 
103         case 7:
104 
105             _imageview.frame=CGRectMake(493, 153, 78, 86);
106 
107             break;
108 
109         default:
110 
111             _imageview.frame=CGRectMake(511, 246, 78, 86);
112 
113             break;
114 
115         
116 
117     }    
118 
119 }

原文地址:https://www.cnblogs.com/iOSlearner/p/5277302.html