iPhone 風船アプリ

風船を増やして上昇、割って下降、ボタンをつかって上手に壁の穴をすり抜けていこう!というようなiPhoneゲームを作ってみます。


動作イメージ

XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

{

    UIView *player;

    UIView *needle;

    UIView *wall;

    BOOL needleReady;

    NSMutableArray *balloons;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];

    

    [self createPlayer];

    

    [self createNeedle];

    

    [self createBalloonButton];

    [self createNeedleButton];

    

    [self start];

}

– (void)createPlayer

{

    player = [[UIView alloc] initWithFrame:CGRectMake(50, 180, 80, 80)];

    player.backgroundColor = [UIColor clearColor];

    

    UIView *face = [[UIView alloc] initWithFrame:CGRectMake(25, 50, 30, 30)];

    face.backgroundColor = [UIColor blackColor];

    [player addSubview:face];

    [self.view addSubview:player];

    

    [self createBalloon];

    [self createBalloon];

    [self createBalloon];

}

-(void)createBalloon

{

    CGPoint p[] = {

        CGPointMake(20, 10), CGPointMake(40, 5), CGPointMake(60, 10),

        CGPointMake(20, 20), CGPointMake(40, 12), CGPointMake(60, 20),

    };

    

    CGPoint o = p[[balloons count]];

    

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path addArcWithCenter:o radius:10 startAngle:0 endAngle:2*M_PI clockwise:YES];

    [path moveToPoint:CGPointMake(o.x, o.y+10)];

    [path addLineToPoint:CGPointMake(40, 50)];

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.fillColor = [UIColor blackColor].CGColor;

    sl.strokeColor = [UIColor grayColor].CGColor;

    sl.lineWidth = 2;

    sl.path = path.CGPath;

    [player.layer addSublayer:sl];

    

    if (!balloons) {

        balloons = [[NSMutableArray alloc] init];

    }

    [balloons addObject:sl];

}

– (void)createNeedle

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(0, 0)];

    [path addLineToPoint:CGPointMake(10, 0)];

    [path addLineToPoint:CGPointMake(5, 20)];

    [path closePath];

    

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.fillColor = [UIColor blackColor].CGColor;

    sl.strokeColor = [UIColor grayColor].CGColor;

    sl.lineWidth = 2;

    sl.path = path.CGPath;

    needle = [[UIView alloc] initWithFrame:CGRectMake(80, 0, 5, 20)];

    [needle.layer addSublayer:sl];

    [self.view addSubview:needle];

    

    needleReady = YES;

}

– (void)createBalloonButton

{

    // balloon

    UIView *balloonBtn = [[UIView alloc] initWithFrame:CGRectMake(400, 230, 60, 60)];

    balloonBtn.backgroundColor = self.view.backgroundColor;

    balloonBtn.layer.cornerRadius = 30;

    balloonBtn.layer.borderWidth = 4;

    balloonBtn.layer.borderColor = [UIColor blackColor].CGColor;

    [self.view addSubview:balloonBtn];

    

    CGPoint o = CGPointMake(30, 20);

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path addArcWithCenter:o radius:10 startAngle:0 endAngle:2*M_PI clockwise:YES];

    [path moveToPoint:CGPointMake(o.x, o.y+10)];

    [path addLineToPoint:CGPointMake(25, 50)];

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.fillColor = [UIColor blackColor].CGColor;

    sl.strokeColor = [UIColor grayColor].CGColor;

    sl.lineWidth = 2;

    sl.path = path.CGPath;

    [balloonBtn.layer addSublayer:sl];

    

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

    [balloonBtn addGestureRecognizer:tap];

}

– (void)createNeedleButton

{

    // needle

    UIView *needleBtn = [[UIView alloc] initWithFrame:CGRectMake(480, 230, 60, 60)];

    needleBtn.backgroundColor = self.view.backgroundColor;

    needleBtn.layer.cornerRadius = 30;

    needleBtn.layer.borderWidth = 4;

    needleBtn.layer.borderColor = [UIColor blackColor].CGColor;

    [self.view addSubview:needleBtn];

    

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(15, 15)];

    [path addLineToPoint:CGPointMake(45, 15)];

    [path addLineToPoint:CGPointMake(30, 50)];

    [path closePath];

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.fillColor = [UIColor blackColor].CGColor;

    sl.strokeColor = [UIColor grayColor].CGColor;

    sl.lineWidth = 2;

    sl.path = path.CGPath;

    [needleBtn.layer addSublayer:sl];

    

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

    [needleBtn addGestureRecognizer:tap];

}

– (void)dropNeedle

{

    if (needle && needleReady) {

        needleReady = NO;

        

        float duration = player.center.y / 320.0;

        [UIView animateWithDuration:duration animations:^{

            needle.center = CGPointMake(player.center.x, player.center.y30);

        } completion:^(BOOL finished) {

            [[balloons lastObject] removeFromSuperlayer];

            [balloons removeLastObject];

            

            [needle removeFromSuperview];

            needle = nil;

            

            [self performSelector:@selector(createNeedle) withObject:nil afterDelay:0.5];

        }];

    }

}

– (void)start

{

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

}

– (void)tick:(NSTimer*)sender

{

    int count = [balloons count];

    float upForce = (count – 2.5) / 4.0;

    player.center = CGPointMake(player.center.x, player.center.y – upForce);

    

    if (!wall) {

        [self createWall];

    }

    

    wall.center = CGPointMake(wall.center.x1, wall.center.y);

    if (wall.center.x < 0) {

        [wall removeFromSuperview];

        wall = nil;

    }

}

– (void)createWall

{

    wall = [[UIView alloc] initWithFrame:CGRectMake(580, 0, 10, 320)];

    wall.backgroundColor= [UIColor blackColor];

    [self.view insertSubview:wall atIndex:0];

    

    float y = arc4random() % 220;

    UIView *hole = [[UIView alloc] initWithFrame:CGRectMake(0, y, 10, 100)];

    hole.backgroundColor = self.view.backgroundColor;

    [wall addSubview:hole];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end