パイナップルのゲームをObjective-Cで書いてみた。
(XcodeのiOS6 iPhone Simulatorで動かしています。)

概要

とんでくる黄色に「シマシマ」と「葉っぱ」を
うまいこと当てて、パイナップルの絵を作っていく
というゲーム。

ポイント

タイマーの中で、衝突時のView, Layerの入れ替えを行う。
葉っぱが黄色い丸にちゃんと当たった場合、
UIViewを新規に作成して、黄色、葉っぱのViewを
そのSubViewとしてaddSubViewした後に、
Transformで回転させて、左上に飛ばしてみました。




サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    NSTimer *timer;

    NSMutableArray *lines, *apples, *leaves;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    [self createBackColor];

    

    [self createUI];

    

    [self startTimer];

}

– (void)createBackColor

{

    CAGradientLayer *layer = [CAGradientLayer layer];

    layer.frame = self.view.bounds;

    UIColor *c1 = [UIColor whiteColor];

    UIColor *c2 = [UIColor greenColor];

    layer.colors = [NSArray arrayWithObjects:(id)c1.CGColor, (id)c2.CGColor, nil];

    layer.startPoint = CGPointMake(0.5, 0.4);

    layer.endPoint = CGPointMake(0.5, 1.0);

    [self.view.layer addSublayer:layer];

}

– (void)createUI

{

    UIView *panelA = [[UIView alloc] initWithFrame:CGRectMake(300, 230, 50, 50)];

    panelA.layer.cornerRadius = 5.0;

    panelA.backgroundColor = [UIColor whiteColor];

    panelA.layer.borderWidth = 2.0;

    panelA.layer.borderColor = [UIColor darkGrayColor].CGColor;

    [self.view addSubview:panelA];

    

    CALayer *l = [self createLine];

    l.position = CGPointMake(5, 5);

    [panelA.layer addSublayer:l];

    

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

    [panelA addGestureRecognizer:tap];

    

    

    UIView *panelB = [[UIView alloc] initWithFrame:CGRectMake(200, 230, 50, 50)];

    panelB.layer.cornerRadius = 5.0;

    panelB.backgroundColor = [UIColor whiteColor];

    panelB.layer.borderWidth = 2.0;

    panelB.layer.borderColor = [UIColor darkGrayColor].CGColor;

    [self.view addSubview:panelB];

    

    CALayer *leaf = [self createLeaf];

    leaf.position = CGPointMake(5, 5);

    [panelB.layer addSublayer:leaf];

    

    UITapGestureRecognizer *tapLeaf = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLeaf:)];

    [panelB addGestureRecognizer:tapLeaf];

    

}

– (void)tap:(UIGestureRecognizer*)gr

{

    UIView *line = [[UIView alloc] init];

    line.frame = gr.view.frame;

    CALayer *l = [self createLine];

    [line.layer addSublayer:l];

    [self.view addSubview:line];

    

    if (!lines) {

        lines = [[NSMutableArray alloc] init];

    }

    

    l.transform = CATransform3DMakeRotation(M_PI*0.25, 0, 0, 1);

    l.transform = CATransform3DTranslate(l.transform, 5, –20, 0);

    

    [lines addObject:line];

}

– (void)tapLeaf:(UIGestureRecognizer*)gr

{

    UIView *leaf = [[UIView alloc] init];

    leaf.frame = gr.view.frame;

    [leaf.layer addSublayer:[self createLeaf]];

    [self.view addSubview:leaf];

    if (!leaves) {

        leaves = [[NSMutableArray alloc] init];

    }

    

    leaf.layer.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);

    [leaves addObject:leaf];

}

– (CALayer*)createLine

{

    UIBezierPath *path = [[UIBezierPath alloc] init];

    

    float d = 8;

    float s = 40;

    for (int i=1; i<5; i++) {

        [path moveToPoint:CGPointMake(d*i, 0)];

        [path addLineToPoint:CGPointMake(d*i, s)];

        [path moveToPoint:CGPointMake(0, d*i)];

        [path addLineToPoint:CGPointMake(s, d*i)];

    }

    

    CAShapeLayer *l = [CAShapeLayer layer];

    l.fillColor = [UIColor clearColor].CGColor;

    l.lineWidth = 2.0;

    l.strokeColor = [UIColor colorWithRed:0 green:0.8 blue:0 alpha:1.0].CGColor;

    l.path = path.CGPath;

        

    return l;

}

– (CALayer*)createLeaf

{

    UIBezierPath *path = [[UIBezierPath alloc] init];

    float s = 40;

    

    [path moveToPoint:CGPointMake(s*0.4, s)];

    [path addLineToPoint:CGPointMake(0, s*0.8)];

    

    [path addLineToPoint:CGPointMake(s*0.3, s*0.8)];

    [path addLineToPoint:CGPointMake(0, s*0.5)];

    

    [path addLineToPoint:CGPointMake(s*0.3, s*0.6)];

    [path addLineToPoint:CGPointMake(s*0.2, s*0.1)];

    

    [path addLineToPoint:CGPointMake(s*0.5, s*0.5)];

    

    [path addLineToPoint:CGPointMake(s*0.8, s*0.1)];

    [path addLineToPoint:CGPointMake(s*0.7, s*0.6)];

    

    [path addLineToPoint:CGPointMake(s, s*0.5)];

    [path addLineToPoint:CGPointMake(s*0.7, s*0.8)];

    

    [path addLineToPoint:CGPointMake(s, s*0.8)];

    [path addLineToPoint:CGPointMake(s*0.6, s)];

    

    CAShapeLayer *l = [CAShapeLayer layer];

    l.fillColor = [UIColor colorWithRed:0 green:0.8 blue:0 alpha:1.0].CGColor;

    l.path = path.CGPath;

    return l;

}

– (void)startTimer

{

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(tick:) userInfo:nil repeats:YES];

}

– (void)tick:(NSTimer*)sender

{

    // create pine base

    static int counter;

    if (counter > 120) {

        UIView *pine = [[UIView alloc] initWithFrame:CGRectMake(500, 80, 35, 40)];

        pine.layer.cornerRadius = 12;

        pine.backgroundColor = [UIColor yellowColor];

        pine.layer.masksToBounds = YES;

        [self.view addSubview:pine];

        counter = 0;

        

        if (!apples) {

            apples = [[NSMutableArray alloc] init];

        }

        [apples addObject:pine];

    }

    counter++;

    

    // pine base

    for (UIView *v in apples) {

        v.center = CGPointMake(v.center.x2, v.center.y);

        if (v.center.x < –30) {

            v.tag = 101;

        }

    }

    

    // pine line

    for (UIView *v in lines) {

        v.center = CGPointMake(v.center.x, v.center.y4);

        

        if (v.center.y < –10) {

            v.tag = 101;

        }

    }

    

    // pine leaf

    for (UIView *v in leaves) {

        v.center = CGPointMake(v.center.x, v.center.y4);

        

        if (v.center.y < –10) {

            v.tag = 101;

        }

    }

    

    

    // collision check

    

    for (int i=0; i<[apples count]; i++) {

        UIView *apple = [apples objectAtIndex:i];

        

        // check line

        for (UIView *line in lines) {

            if (CGRectIntersectsRect(apple.frame, line.frame)) {

                [apple.layer addSublayer:[line.layer.sublayers objectAtIndex:0]];

            }

        }

        

        // check leaf

        for (int i=0; i<[leaves count]; i++) {

            UIView *leaf = [leaves objectAtIndex:i];

            CGRect rect = CGRectMake(leaf.frame.origin.x + 15, leaf.frame.origin.y + 10, 10, 10);

            if (CGRectIntersectsRect(apple.frame, rect)) {

                [leaves removeObject:leaf];

                [apples removeObject:apple];

                

                UIView *pineApple = [[UIView alloc] initWithFrame:CGRectMake(apple.frame.origin.x, apple.frame.origin.y, 50, 100)];

                pineApple.backgroundColor = [UIColor clearColor];

                [pineApple addSubview:apple];

                [pineApple addSubview:leaf];

                [self.view addSubview:pineApple];

                

                apple.center = [pineApple convertPoint:apple.center fromView:self.view];

                leaf.center = [pineApple convertPoint:leaf.center fromView:self.view];

                

                [UIView animateWithDuration:0.5 animations:^{

                    pineApple.transform = CGAffineTransformMakeRotation(M_PI);

                } completion:^(BOOL finished) {

                    [UIView animateWithDuration:0.5 animations:^{

                        pineApple.center = CGPointMake(-50, –50);

                    } completion:^(BOOL finished) {

                        [pineApple removeFromSuperview];

                    }];

                }];

            }

        }

    }

    

    // clean

    for (UIView *v in self.view.subviews) {

        if (v.tag == 101) {

            [v removeFromSuperview];

            [apples removeObject:v];

            [lines removeObject:v];

        }

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end