猜拳小游戏(*^__^*) 嘻嘻……

利用核心动画和Quartz2D做的一个小游戏。逻辑十分简单(仅供参考)

#pragma mark - 开始动画
static int countTouch = 0;
- (void)click:(UITapGestureRecognizer*)tap {
    UIImage *image = [[UIImage alloc] init];
    countTouch++;
    if (countTouch == 1) {
        image = [UIImage imageNamed:@"paper"];
    } else if (countTouch == 2) {
        image = [UIImage imageNamed:@"rock"];
    } else if (countTouch == 3) {
        image = [UIImage imageNamed:@"scissors"];
    } else {
        [self.lauchImage removeGestureRecognizer:tap];
        self.lauchImage.hidden = YES;
        return;
    }
    //设置背景图片
    UIImage *backgroundImage = [UIImage imageNamed:@"background"];
    UIImage *newImage = [self imageFormbackgroundIamge:backgroundImage andSourceImage:image];
    //设置新图片
    self.lauchImage.image = newImage;
    //设置转场动画
    CATransition *animation = [CATransition animation];
    [animation setType:@"kCATransitionFade"];
    animation.duration = 0.5;
    //    animation.type = @"pageCurl";
    animation.subtype = kCATransitionFromTop;
    [self.lauchImage.layer addAnimation:animation forKey:nil];
}
- (UIImage*)imageFormbackgroundIamge:(UIImage*)backgroundImage andSourceImage:(UIImage*)sourceImage {
    //获取开启上下文
    UIGraphicsBeginImageContext(self.lauchImage.bounds.size);
    //绘制背景
    [backgroundImage drawInRect:self.lauchImage.bounds];
    //绘制图片
    [sourceImage drawInRect:self.lauchImage.bounds];
    //获取当前图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    //返回合成的图片
    return newImage;
}

#pragma mark -点击按钮调用方法
/**
 *  三个出拳点击的处理方法
 *
 *  @param sender 点了哪个按钮
 */
- (IBAction)playerChooseFist:(GYXFistBtn*)sender{
    //如果正在出拳,就不能再出拳了
    if ([self.playerFist isAnimating]) {
        return;
    }
    self.player.fist = sender.fistType;
    [self.robot showFist];
    [self judge];
}
#pragma mark - 判断谁胜谁负
- (void)judgee {
    NSString *str = nil;
    int res = self.player.fist - self.robot.fist;
    if (res == 1 || res ==-2) {
        str =  @"玩家赢了";
        self.player.score += 1;
        self.robot.score -= 1;
    }
    else if (res == -1 || res ==2) {
        str = @"电脑赢了" ;
        self.player.score -= 1;  //[player score];
        self.robot.score += 1;   //[robot score];
    } else {
        str = @"平局";
    }
    //根据出拳,选择图片
    NSString *robotFist = [NSString stringWithFormat:@"%d",self.robot.fist];
    NSString *palyerFist = [NSString stringWithFormat:@"%d%d",self.player.fist,self.player.fist];
    //先来一段准备动画压压惊
    [self perpareAnimation];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //设置玩家最终出拳
        self.playerFist.image = [UIImage imageNamed:palyerFist];
        //设置机器人最终出拳
        self.robotFist.image = [UIImage imageNamed:robotFist];
        //返回结果
        self.resultLabel.text = str;
        self.robotScore.text = [NSString stringWithFormat:@"得分:%d",self.robot.score];
        self.playerScore.text = [NSString stringWithFormat:@"得分:%d",self.player.score];
    });
}
#pragma mark - 出拳前的动画
/**
 *  出拳前的动画
 */
- (void)perpareAnimation {
    //设置动画图片
    UIImage *jian = [UIImage imageNamed:@"1"];
    UIImage *shitou = [UIImage imageNamed:@"2"];
    UIImage *bu = [UIImage imageNamed:@"3"];
    UIImage *playerShiTou = [UIImage imageNamed:@"22"];
    UIImage *playerBu = [UIImage imageNamed:@"33"];
    UIImage *playerJian = [UIImage imageNamed:@"11"];
    NSArray *robotImageArr = @[shitou,bu,jian];
    NSArray *playerImageArr = @[playerBu,playerShiTou,playerJian];
    [self.robotFist setAnimationImages:robotImageArr];
    [self.robotFist setAnimationDuration:0.5];
    [self.robotFist setAnimationRepeatCount:4];
    [self.playerFist setAnimationImages:playerImageArr];
    [self.playerFist setAnimationDuration:0.5];
    [self.playerFist setAnimationRepeatCount:4];
    [self.robotFist startAnimating];
    [self.playerFist startAnimating];
}

#pragma mark - 懒加载
#pragma mark 用户
- (GYXPlayer *)player {
    if (_player == nil) {
        _player = [[GYXPlayer alloc] init];
    }
    return _player;
}
#pragma mark 机器人
- (GYXRoot *)robot {
    if (_robot == nil) {
        _robot = [[GYXRoot alloc] init];
    }
    return _robot;
}

样图:

猜拳小游戏(*^__^*) 嘻嘻……_第1张图片
F872FB77-23A8-45A9-B42F-9EA4BD203761.png
猜拳小游戏(*^__^*) 嘻嘻……_第2张图片
20AF39A2-001A-4B9C-9CD1-1AC4BCC65000.png
猜拳小游戏(*^__^*) 嘻嘻……_第3张图片
2ACE2038-9523-4126-9979-902685A8003C.png
  • 注意点总结:
    •                      1.变量问题(当多次使用同一变量时,应该考虑是否该设成全局变量),
      
    •                      2.局数和赢的数的计算,
      
    •                      3.循环语句(注:if是判断,while是循环!!!)
      
    •                      4.思考:
      
    •                                       1)不要按照最后结果倒着思考,首先应该考虑变量问题,题中会出现多少变量,
      
    •                                      2)不要着急写,先在脑中或纸上有个大致的流程
      
    •                                      3)不要依靠ppt的流程分解,而是自己分解
      

这就是我今天分享的内容啦, 喜欢就点个赞吧!么么哒

你可能感兴趣的:(猜拳小游戏(*^__^*) 嘻嘻……)