实例一 find the monkey

UI比较基础的都过了一遍,就找了一个开源的简单项目运行了下然后看了一遍代码,虽然小游戏很简单,甚至都没有用到Cocoa2d之类的,但还是很高兴基本代码都看懂了,通过查官方文档也学习了一些新的用法。感觉通过小例子来学习是一个比较有意思的方式。
现在把自己注释过的代码贴上来,感觉非常适合看完教程后的第一个小游戏。

项目地址:https://github.com/rmd6502/monkey

monkeyViewController.h

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s6 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}span.s7 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.Apple-tab-span {white-space:pre}

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface monkeyViewController : UIViewController<AVAudioPlayerDelegate> { //音频播放协议
 NSUInteger hiddenLocation; //"藏猴子"的位置
 NSTimer *clock;            //计时用
 NSUInteger elapsed_seconds;
 AVAudioPlayer *avp;        //两种音频
 AVAudioPlayer *win;
}

@property (nonatomic, assign) IBOutlet UIButton *startButton;  //IBOutlet可视化编程自动生成变量
@property (nonatomic, assign) IBOutlet UITextField *timeRemaining;
@property (nonatomic, assign) IBOutlet UILabel *where;

- (IBAction)startGame:(id)sender;  //可视化编程自动生成事件函数
- (void)guess:(id)sender;          //传递猜的位置 并判断正误
- (void)tick:(NSTimer *)timer;     //计时
- (void)resetGame;                 //重新开始

@end

monkeyViewController.m

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #31595d}p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s3 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s7 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s8 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s9 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s10 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}span.s11 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s12 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s13 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s14 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s15 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.Apple-tab-span {white-space:pre}

#import <QuartzCore/QuartzCore.h>
#import "monkeyViewController.h"
//quartz 是主要的描画接口,支持基于路径的描画、抗锯齿渲染、渐变填充模式、图像、颜色、坐标空间变换、以及PDF 文档的创建、显示、和分析。UIKit 为Quartz 的图像和颜色操作提供了Objective-C 的封装。Core Animation 为很多UIKit 的视图属性声明的动画效果提供底层支持,也可以用于实现定制的动画。

@implementation monkeyViewController
@synthesize startButton;
@synthesize timeRemaining;
@synthesize where;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

- (void) viewWillAppear:(BOOL)animated {
 [super viewWillAppear:animated];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //CGColor主要用于CoreGaphics框架之
 CGColorSpaceRef csp = CGColorSpaceCreateDeviceRGB();
 CGColorRef col1 = [UIColor cyanColor].CGColor; //cyanColor RGB=011 青色
 CGFloat comps[4] = {0};
    //内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中。
 memcpy(comps, CGColorGetComponents(col1), 4 * sizeof(CGFloat));
 comps[0] = 0.75;
 CGColorRef col2 = CGColorCreate(csp, comps);
    comps[0] = 0;
    for (int i=0; i < 3; ++i) comps[i] *= .7;
    col1 = CGColorCreate(csp, comps); //在csp空间中通过CGColorCreate用RGB创建颜色
 //CGColorRef col2 = [UIColor whiteColor].CGColor;
    
 const CGColorRef cols[] = {col1, col2};
    
    
    
 CFArrayRef collist = CFArrayCreate(nil, (const void **)cols, sizeof(cols)/sizeof(CGColorRef), NULL);
 CGFloat locs[] = {.99,0.0}; //?
 
 UIButton *b1 = (UIButton *)[self.view viewWithTag:1001];
 CGContextRef ctx = CGBitmapContextCreate(nil, b1.bounds.size.width, b1.bounds.size.height, 8, 4 * b1.bounds.size.width, csp, kCGImageAlphaPremultipliedLast);
 CGGradientRef grad = CGGradientCreateWithColors(csp, collist, locs);
 CGContextDrawRadialGradient(ctx, grad, CGPointMake(b1.bounds.size.width/2,b1.bounds.size.height/2), 1, CGPointMake(b1.bounds.size.width/2,b1.bounds.size.height/2), b1.bounds.size.width/2, kCGGradientDrawsAfterEndLocation);
 CGImageRef bg = CGBitmapContextCreateImage(ctx);
 UIImage *im = [UIImage imageWithCGImage:bg];
    
    
    
    //猜的格子设置为UIButton
    //用guess事件函数判断猜对没有
    //传送过去的参数就是点击的那个UIButton对象
 for (int i=1001; i < 1010; ++i) {
  UIButton *b = (UIButton *)[self.view viewWithTag:i];
  [b addTarget:self action:@selector(guess:) forControlEvents:UIControlEventTouchUpInside];
  [b setBackgroundImage:im forState:UIControlStateNormal];
 }
    
    
    //内存释放
 CGImageRelease(bg);
 CFRelease(collist);
 CGColorRelease(col2);
    CGColorRelease(col1);
 CGGradientRelease(grad);
 CGColorSpaceRelease(csp);
 [self resetGame];
    
    
    
    
 
    //载入音频
 NSError *error = nil;
 NSURL *noiseURL = [[NSBundle mainBundle]    //猜错的音乐
        URLForResource:@"buzzer" 
        withExtension:@"mp3"];
 avp = [[AVAudioPlayer alloc] 
        initWithContentsOfURL:noiseURL error:&error];
 if (error) {
  NSLog(@"failed to load sound: %@", [error localizedDescription]);
  return;
 }
 noiseURL = [[NSBundle mainBundle]   //猜对的音乐
    URLForResource:@"win" 
    withExtension:@"m4a"];
 win = [[AVAudioPlayer alloc] 
     initWithContentsOfURL:noiseURL error:&error];
    //设置音频属性
 [avp setDelegate:self];
 [avp setVolume:1.0];
 [avp prepareToPlay];
 [win setDelegate:self];
 [win setVolume:1.0];
 [win prepareToPlay];
 
    [self becomeFirstResponder];//:第一响应对象是窗口中,应用程序认为最适合处理事件的对象

}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
 NSLog(@"motion %@", event);
 if (motion == UIEventSubtypeMotionShake) {
  [self startGame:nil];
 }
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
 NSLog(@"motion began %@", event);
 if (motion == UIEventSubtypeMotionShake) {
  [self startGame:nil];
 }
}

- (void) viewDidUnload {
 [avp release];
 [win release];
 [super viewDidUnload];
}

- (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];
 [self becomeFirstResponder];
}

//重新开始游戏
- (void)resetGame {
 CABasicAnimation *trans = [CABasicAnimation animation];
 trans.keyPath = @"transform.scale";
 trans.repeatCount = HUGE_VALF;
 trans.duration = 0.5;
 trans.autoreverses = YES;
 trans.removedOnCompletion = NO;
 trans.fillMode = kCAFillModeForwards;
 trans.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
 trans.fromValue = [NSNumber numberWithFloat:0.9];
 trans.toValue = [NSNumber numberWithFloat:1.1];
 [self.startButton.titleLabel.layer addAnimation:trans forKey:@"pulse"];
 for (int i=1001; i < 1010; ++i) [(UIButton *)[self.view viewWithTag:i] setEnabled:NO];
 elapsed_seconds = 0;
}

//开始游戏
- (IBAction)startGame:(id)sender {  //触发事件函数
 for (int i=1001; i < 1010; ++i) {
  UIButton *b = (UIButton *)[self.view viewWithTag:i]; //创建9个Button
  [b setImage:nil forState:UIControlStateNormal];
  [b setTitle:@"?" forState:UIControlStateNormal];   //显示问号
  b.enabled = YES;
 }
 [self.startButton.titleLabel.layer removeAllAnimations];
    
 NSUInteger isCorrect = 0;
 SecRandomCopyBytes(kSecRandomDefault, sizeof(NSUInteger), (void *)&isCorrect); //产生随机数
 hiddenLocation = isCorrect % 9;
 elapsed_seconds = 0;
 [timeRemaining setText:@"00:00:00"];
 if (clock) {
  [clock invalidate];
 }
 clock = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}

- (BOOL)canBecomeFirstResponder { return YES; }

//猜猴子判断是否猜中的事件函数
//由UIButton触发
- (void)guess:(id)sender {
 UIButton *guessed = (UIButton *)sender;
 guessed.enabled = NO;
 
 CATransition *trans = [[CATransition alloc] init]; //CATransition转场动画
 trans.duration = 0.25;
//类型:
//    NSString * const kCATransitionFade;//逐渐消失
//    NSString * const kCATransitionMoveIn;//移入
//    NSString * const kCATransitionPush;//平移(暂且这么称呼吧)
//    NSString * const kCATransitionReveal;//显露
 trans.type = kCATransitionFade;
 [guessed.layer addAnimation:trans forKey:@"Fade"];
 [trans release];
    
    
 [CATransaction begin];//CATransaction 用来修改图层树
 if (guessed.tag - 1001 == hiddenLocation) { //hiddenLocation 猜对的位置
  [guessed setTitle:@"" forState:UIControlStateNormal]; //去掉?的title
  [guessed setImage:[UIImage imageNamed:@"monkey_toy"] forState:UIControlStateNormal];
  [clock invalidate];                     //停止计时
  clock = nil;
  [self resetGame];                       //重新开始
  [win play];
 } else {                                      //猜错的位置
  [guessed setTitle:@"×" forState:UIControlStateNormal];
  [avp stop];
  [avp prepareToPlay];
  [avp play];
 }
 [CATransaction commit];
}

//计时
- (void)tick:(NSTimer *)timer {  //timeRemaining、elapsed_seconds都是自己定义的
 ++elapsed_seconds;
 [timeRemaining setText:[NSString stringWithFormat:@"%02d:%02d:%02d",
       elapsed_seconds / 3600, (elapsed_seconds % 3600) / 60, elapsed_seconds % 60]];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
 
 // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    [super dealloc];
}
       

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,051评论 25 707
  • 从小我在英语学习上算是比较有天赋的。只是我既不喜欢也不讨厌这门学科,准确点说,是这门语言。 它有许...
    Ladyjack5阅读 406评论 0 0
  • 体悟: 根据经济学人的语域,虽然将原文的隐喻翻出来能让读者体会到st 的风采,但还是以传递信息为target, 建...
    野生的萱阅读 189评论 0 0
  • 上一篇我们理解了什么是能力,那么当我们掌握了能力,接下来我们应该做什么呢,那就开始迁移你的能力吧。 能力迁移 能...
    秋月连江阅读 403评论 2 2
  • 6月初的一个早晨,半醒状态拿手机(iphone 5s)时,没拿稳,手机摔了下去。biang的一声过后,我习惯性的把...
    songkun阅读 323评论 0 1