今天忙里偷闲,写了个石头剪子布的小程序,给大家做个小参考
主要功能如图:1.未开始出拳时,双方不停的做动画,并伴随背景音乐。
2.出拳后,判断双方输赢,并给相应的一方加分,伴随相应音效。
3.点击继续按钮,重新开始游戏.
首先,在StroryBoard中,拖几个控件,包括:电脑的出拳ImageView,玩家出拳的ImageView,双方的得分label;玩家选择出拳对象的button;承载button的View;然后在.h文件中 进行连线。
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
//电脑出拳ImageView
@property (weak, nonatomic) IBOutletUIImageView *computerImageView;
//玩家出拳ImageView
@property (weak, nonatomic) IBOutletUIImageView *playerImageVIew;
//电脑得分Label
@property (weak, nonatomic) IBOutletUILabel *computerScoreLable;
//玩家得分Label
@property (weak, nonatomic) IBOutletUILabel *playerScoreLabel;
//操作视图
@property (weak, nonatomic) IBOutletUIView *actionVIew;
//消息提示Label
@property (weak, nonatomic) IBOutletUILabel *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.x, point.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.x, point.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=[[NSBundlemainBundle] pathForResource:@"背景音乐" ofType:@"caf"];
NSURL *url=[NSURLfileURLWithPath:path];
AVAudioPlayer *player=[[AVAudioPlayeralloc]initWithContentsOfURL:url error:nil];
[player setNumberOfLoops:-1];
[player prepareToPlay];
return player;
}