iOS伪实现打地鼠游戏

 

打地鼠是一款可以用iOS知识来实现的一种游戏.其核心技术就是通过imageView来播放动画,点击button时来停止当前播放的动画开始击打地鼠的动画.话不多说直接上代码.

这是添加当前的背景图片,然后再背景图片上添加地鼠出现的imageView,并在每个imageView上添加个button来执行打击地鼠的事件.

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建底层显示的背景图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    imageView.image = [UIImage imageNamed:@"back.png"];
    //打开图片的交互(imageView默认是关闭的)
    imageView.userInteractionEnabled = YES;
    
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 7; j++) {
            //添加地鼠洞的图片
            _imageV = [[UIImageView alloc] initWithFrame:CGRectMake(20+50*i, 250+50*j, 50, 50)];
            _imageV.image = [UIImage imageNamed:@"emptyhole.png"];
            _imageV.tag = 149+i*7+j+1;
            //创建每个洞的点击事件
            _button = [UIButton buttonWithType:UIButtonTypeSystem];
            _button.frame = CGRectMake(0, 0, 50, 50);
            [_button addTarget:self action:@selector(hit:) forControlEvents:UIControlEventTouchUpInside];
            //给每个button添加tag值来确定你点击的是那个button
            _button.tag = 100+i*7+j+1;
            [_imageV addSubview:_button];
            _imageV.userInteractionEnabled = YES;
            [imageView addSubview:_imageV];
        }
    }
    //指定当前现实的背景图片
    self.view = imageView;
    //添加计时器
    [self addTimer];
    // Do any additional setup after loading the view, typically from a nib.
}

在确定完成后添加计时器来实现在不同的时间段里在不同位置上出现地鼠.

//添加计时器来启动图片的播放
- (void)addTimer
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(startGame) userInfo:nil repeats:YES];
}

实现计时器的方法来实现地鼠出洞的动画

//启动地鼠出洞的动画
- (void)startGame
{
    //定义一个可变数组来存放播放的图片
    NSMutableArray *arr = [NSMutableArray array];
    //地鼠出洞的图片
    for (int i = 1; i < 7; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"t%d.png",i]];
        [arr addObject:image];
    }
    //地鼠回洞的图片
    for (int i = 1; i < 7; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"t%d.png",7-i]];
        [arr addObject:image];
    }
    //获取随机值来让地鼠在随机位置出现
    NSInteger num = arc4random()%49+150;
    //根据tag值来获取随机的现实地鼠图片的imageView
    _imageV = (UIImageView *)[self.view viewWithTag:num];
    //指定imageView的不妨图片的数组
    _imageV.animationImages = arr;
    //指定播放次数
    _imageV.animationRepeatCount = 1;
    //指定播放时间
    _imageV.animationDuration = 1;
    //开启播放
    [_imageV startAnimating];
}

实现button的点击时间来执行地鼠被打击的效果.

- (void)hit:(UIButton *)sender
{
    //根据点击button的tag值来获取当前的imageV
    NSInteger num = sender.tag;
    //根据button的tag值来获取当前点击的是那个imageview
    _imageV = (UIImageView *)[self.view viewWithTag:num+49];
    //确定是哪个位置的button来执行点击事件
    _button = (UIButton *)[self.view viewWithTag:num];
    //判断当前点击的imageView是否在播放动画
    if (_imageV.isAnimating == YES) {
        //停止播放动画
        [_imageV stopAnimating];
        //定义一个可变数组来存储地鼠被击打时的图片
        NSMutableArray *imageArr = [NSMutableArray array];
        //地鼠被击打的图片
        UIImage *image = [UIImage imageNamed:@"hit.png"];
        //空洞的图片
        UIImage *image1 = [UIImage imageNamed:@"emptyhole.png"];
        //讲两张图片添加到可变数组中
        [imageArr addObject:image];
        [imageArr addObject:image1];
        //指定播放动画的数组
        _imageV.animationImages = imageArr;
        //制动播放的次数(0为无限循环播放)
        _imageV.animationRepeatCount = 1;
        //指定播放的时间
        _imageV.animationDuration = 1;
        //开始播放动画
        [_imageV startAnimating];
    }else{
       //如果点击的位置没有在播放动画执行的方法.
    }
}

在写完这些代码你就可以完成一个打地鼠的游戏.

最后附上需要的图片,图片上方为图片在工程中的名字(冒号前是图片名字各位亲不要把冒号也复制了么么哒!!).

back:

enptyhole:

hit:

t1:

t2:

t3:

t4:

t5:

t6:

 

posted @ 2015-10-12 17:01  想你知道  阅读(714)  评论(0编辑  收藏  举报