IOS-石头剪子布小程序

     今天忙里偷闲,写了个石头剪子布的小程序,给大家做个小参考



主要功能如图:



1.未开始出拳时,双方不停的做动画,并伴随背景音乐。

                    2.出拳后,判断双方输赢,并给相应的一方加分,伴随相应音效。

                    3.点击继续按钮,重新开始游戏.

 

   首先,在StroryBoard中,拖几个控件,包括:电脑的出拳ImageView,玩家出拳的ImageView,双方的得分label;玩家选择出拳对象的button;承载button的View;然后在.h文件中  进行连线。

#import <UIKit/UIKit.h>

 

@interface ViewController : UIViewController

//电脑出拳ImageView

@property (weaknonatomicIBOutletUIImageView *computerImageView;

//玩家出拳ImageView

@property (weaknonatomicIBOutletUIImageView *playerImageVIew;

//电脑得分Label

@property (weaknonatomicIBOutletUILabel *computerScoreLable;

//玩家得分Label

@property (weaknonatomicIBOutletUILabel *playerScoreLabel;

//操作视图

@property (weaknonatomicIBOutletUIView *actionVIew;

//消息提示Label

@property (weaknonatomicIBOutletUILabel *messageLbael;

 

//继续游戏

- (IBAction)resumegame:(id)sender;

// 玩家出拳

- (IBAction)playAction:(UIButton *)sender;

 

@end


在.m文件中

#import <AVFoundation/AVFoundation.h>

#import <AudioToolbox/AudioToolbox.h>

#define kActionViewDuration 0.5f

 

@interfaceViewController ()

 

{

    NSArray *ImagesList;

    

    AVAudioPlayer   *_backMusicPlayer;

    

    // 胜利音效

    SystemSoundID   _winSound;

    // 失败音效

    SystemSoundID   _faildSound;

    // 和局音效

    SystemSoundID   _drewSound;

    // 单击音效

    SystemSoundID   _clickSound;

    

    CGPoint point;

    

}

 

在-(void)viewDidLoad方法中

  //设置序列动画  添加图片到数组

    ImagesList=@[[UIImageimageNamed:@"石头.png"],

                 [UIImage imageNamed:@"剪刀.png"],

                 [UIImage imageNamed:@".png"],

                 ];

    

    // 开始动画

    [_computerImageViewsetAnimationImages:ImagesList];

    [_computerImageViewsetAnimationDuration:1.0f];

    [_computerImageViewstartAnimating];

    

    [_playerImageVIewsetAnimationImages:ImagesList];

    [_playerImageVIewsetAnimationDuration:1.0f];

    [_playerImageVIewstartAnimating];

    

    _backMusicPlayer=[selfloadMusic];

    [_backMusicPlayersetVolume:0.5];

    [_backMusicPlayerplay];

    

    // 7. 初始化音效

    _winSound = [selfloadSound:@"胜利.aiff"];

    _faildSound = [selfloadSound:@"失败.aiff"];

    _drewSound = [selfloadSound:@"和局.aiff"];

    _clickSound = [selfloadSound:@"点击按钮.aiff"];

    

    point=[_actionVIewcenter];

 


然后开始游戏  点击相应的图片  出拳  

- (IBAction)playAction:(UIButton *)sender {

    NSLog(@"%ld",sender.tag);

    

    //当点击出拳的时候  停止动画

    [_computerImageViewstopAnimating];

    [_playerImageVIewstopAnimating];

    NSInteger computerResult=arc4random()%3;

    NSInteger playerResult=sender.tag;

    

    //玩家视图显示选择的图像  电脑随机选择

    [_computerImageViewsetImage:ImagesList[computerResult]];

    [_playerImageVIewsetImage:ImagesList[playerResult]];

    

    //判定结果

    NSInteger result=playerResult-computerResult;

    NSLog(@"result  %ld",result);

    

    if (result ==0)

    {

        [_messageLbael setText:@"和局"];

        

        AudioServicesPlaySystemSound(_drewSound);

        

        

    }

    else if (result == -2 ||result ==1)

        

    {

        AudioServicesPlaySystemSound(_faildSound);

        [_messageLbael setText:@"你输了"];

        NSInteger  score=[_computerScoreLable.textintegerValue];

        score ++;

        [_computerScoreLablesetText:[NSStringstringWithFormat:@"%ld",score]];

        

    }

    else

    {

        [_messageLbael setText:@"你赢了"];

        

        AudioServicesPlaySystemSound(_winSound);

        

        

        NSInteger  score=[_playerScoreLabel.textintegerValue];

        score ++;

        [_playerScoreLabelsetText:[NSStringstringWithFormat:@"%ld",score]];

    }

    

   NSLog(@"%f",[_actionVIewcenter].y);

 

    //操作视图下移,显示出继续游戏的按钮

    [UIViewanimateWithDuration:kActionViewDurationanimations:^{

        [_actionVIewsetCenter:CGPointMake(_actionVIew.center.x_actionVIew.center.y+100)];

        

    }];

 

    //判断距离;  如果不判断  操作视图会一直下移虽然在设备上已经移出便捷  但是作为一个程序  要有严谨的思路

    float Distance=[_actionVIew center].y-point.y;

 

    if (Distance >100)

    {

         [_actionVIewsetCenter:CGPointMake(_actionVIew.center.xpoint.y+100)];

 

    }

    

}

 

然后进行  继续游戏 按钮的操作

 

- (IBAction)resumegame:(id)sender {

    AudioServicesPlaySystemSound(_clickSound);

    //点击继续  开始动画

    [_computerImageViewsetAnimationImages:ImagesList];

    [_computerImageViewsetAnimationDuration:1.0f];

    [_computerImageViewstartAnimating];

    

    [_playerImageVIewsetAnimationImages:ImagesList];

    [_playerImageVIewsetAnimationDuration:1.0f];

    [_playerImageVIewstartAnimating];

    

    //让操作视图上移  覆盖继续游戏按钮

    [UIViewanimateWithDuration:kActionViewDurationanimations:^{

        [_actionVIewsetCenter:CGPointMake(_actionVIew.center.x_actionVIew.center.y-100)];

    }];

    

    //判断距离  以防点击多次继续游戏按钮  操作视图多次上次的现象

    float Distance=[_actionVIew center].y-point.y;

    

    if (Distance <100)

    {

        [_actionVIewsetCenter:CGPointMake(_actionVIew.center.xpoint.y)];

      }

    }

 最后  实现音效  要加入相应的框架才可以

// 加载音效

- (SystemSoundID)loadSound:(NSString *)soundFileName

{

    // 1. 需要指定声音的文件路径,这个方法需要加载不同的音效

    NSString *path = [[NSBundlemainBundle]pathForResource:soundFileName ofType:nil];

    // 2. 将路径字符串转换成url

    NSURL *url = [NSURLfileURLWithPath:path];

    

    // 3. 初始化音效

    // 3.1 url => CFURLRef

    // 3.2 SystemSoundID

    SystemSoundID soundId;

    // url先写个错的,然后让xcode帮我们智能修订,这里的方法不要硬记!

    AudioServicesCreateSystemSoundID((__bridgeCFURLRef)(url), &soundId);

    

    return soundId;

}

-(AVAudioPlayer *)loadMusic

{

    NSString *path=[[NSBundlemainBundlepathForResource:@"背景音乐" ofType:@"caf"];

    NSURL *url=[NSURLfileURLWithPath:path];

    AVAudioPlayer *player=[[AVAudioPlayeralloc]initWithContentsOfURL:url error:nil];

    

    [player setNumberOfLoops:-1];

    [player prepareToPlay];

    return player;

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值