在前面, 我们学会了许多空间, 也利用了控件做了某些事情, 现在我们来用控件和电脑玩个小游戏, 也就是我们以前经常玩的游戏, 剪刀石头布:
搭建storyboard界面:
这里有一个注意点, 就是石头剪刀布的下面还有一个按钮:
记得要添加进去, 添加进去之后, 就把View恢复到第一张图的样子:
然后我们来关联控件:
在这里, 我们需要导入两个框架, 一个是AVFoundation, 另一个是AudioToolbox, 下面让我们来看看怎么实现:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
@interface ViewController ()
{
NSArray *_imageList;
AVAudioPlayer *_backMuiscPlayer;
SystemSoundID _winSound;
SystemSoundID _faildSound;
SystemSoundID _drewSound;
SystemSoundID _clickSound;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
// 1.设置数组
_imageList = @[[UIImage imageNamed:@"石头"],
[UIImage imageNamed:@"剪刀"],
[UIImage imageNamed:@"布"]
];
// 2.设置图像的动画数组
[_computerImageView setAnimationImages:_imageList];
[_playerImageView setAnimationImages:_imageList];
// 3.设置图像的动画时长
[_computerImageView setAnimationDuration:1.0f];
[_playerImageView setAnimationDuration:1.0f];
// 4.开始动画
[_computerImageView startAnimating];
[_playerImageView startAnimating];
_backMuiscPlayer = [self loadPlayer];
[_backMuiscPlayer setVolume:0.3f];
[_backMuiscPlayer play];
_winSound = [self loadSound:@"胜利.aiff"];
_faildSound = [self loadSound:@"失败.aiff"];
_drewSound = [self loadSound:@"和局.aiff"];
_clickSound = [self loadSound:@"点击按钮.aiff"];
}
#pragma mark 初始化播放器
- (AVAudioPlayer *)loadPlayer
{
// 5.初始化音乐播放器
// 5.1初始化播放器需要指定音乐文件的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"背景音乐" ofType:@"caf"];
// 5.2将路径转换为URL
NSURL *url = [NSURL fileURLWithPath:path];
// 5.3初始化音乐播放器
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
// 6.设置播放器属性
// 设置播放次数
[player setNumberOfLoops:-1];
[player prepareToPlay];
[player play];
return player;
}
#pragma mark 加载音效
- (SystemSoundID)loadSound:(NSString *)soundFileName
{
// 1.需要指定声音的文件路径.
NSString *path = [[NSBundle mainBundle] pathForResource:soundFileName ofType:nil];
// 2.将路径字符串转换成URL
NSURL *url = [NSURL fileURLWithPath:path];
// 3.初始化音效
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
return soundID;
}
- (IBAction)resumeGame:(UIButton *)sender {
[UIView animateWithDuration:0.5f animations:^{
[_actionView setCenter:CGPointMake(_actionView.center.x, _actionView.center.y - 120)];
}];
[_computerImageView startAnimating];
[_playerImageView startAnimating];
[_messageLabel setText:@""];
}
- (IBAction)playerAction:(UIButton *)sender
{
NSLog(@"player tag:%ld", sender.tag);
NSUInteger computerResult = arc4random_uniform(3);
NSUInteger playerResult = sender.tag;
NSUInteger result = playerResult - computerResult;
NSString *string;
// 判断胜负结果
if (result == 0) {
AudioServicesPlaySystemSound(_drewSound);
string = @"哎哟,平局了";
} else if (result == -1 || result == 2) {
AudioServicesPlaySystemSound(_winSound);
string = @"哦耶,你赢了";
NSUInteger playerScore = [_playerLabel.text integerValue];
playerScore++;
[_playerLabel setText:[NSString stringWithFormat:@"%ld", playerScore]];
} else {
AudioServicesPlaySystemSound(_faildSound);
string = @"太可惜了,再接再厉";
NSUInteger computerScore = [_computerLabel.text integerValue];
computerScore++;
[_computerLabel setText:[NSString stringWithFormat:@"%ld", computerScore]];
}
[_messageLabel setText:string];
// 停止动画
[_computerImageView stopAnimating];
[_playerImageView stopAnimating];
// 设置图片
[_computerImageView setImage:_imageList[computerResult]];
[_playerImageView setImage:_imageList[playerResult]];
[UIView animateWithDuration:0.5f animations:^{
[_actionView setCenter:CGPointMake(_actionView.center.x, _actionView.center.y + 120)];
}];
}
最终的效果就由大家自行去看了, 现在我们来解释一下:
arc4random_uniform(): 随机函数.
AVFoundation:音频框架
AudioToolbox:音频框架
编程思想:
1. 让计算机和玩家的图片播放序列帧动画
提示:序列帧动画的图像顺序,最好和界面上的TAG保持一致
2. 等待玩家出拳,判定游戏结果
播放声音的顺序
1. 引入AVFundation框架头文件
2. 定义声音播放器
3. 初始化声音播放器
4. 设置声音播放器属性
5. 开始播放