ゲームウォッチ風iPhoneアプリ

上からつぎつぎに落ちてくるタマゴをキャッチ!落として割れたら大変だよ!というゲームをレトロなゲームウォッチ風のiPhoneアプリとして書いてみます。


動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。

ポイント
動きはスムーズではなく、点滅しながらのほうがそれっぽいので、animationをかけずにタイマーの間隔を0.5秒くらいに設定して、そのつどタマゴとプレーヤーを表示するようにしています。スコアボードには、DBLCDTempBlackというフォントを利用しています。

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    int eggPosition[5];  // 横の位置5 高さ位置6

    int playerPosition;  // 横の位置6 高さ無し

    NSTimer *gameTimer;

    UIImageView *player;

    UIImage *egg;

    UIImage *humptydumpty;

    

    UIImageView *eggA, *eggB, *eggC, *eggD, *eggE;

    

    UILabel *score;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    [self createArrowKey];

    [self createPlayer];

    [self createEggs];

    [self createScore];

    

    [self start];

}

– (void)createPlayer

{

    UIImage *image = [UIImage imageNamed:@”player”];

    player = [[UIImageView alloc] initWithImage:image];

    player.frame= CGRectMake(0, 160, 80, 60);

    [self.view addSubview:player];

    

    playerPosition = 0;

    [self showPlayer];

}

– (void)showPlayer

{

    float x = playerPosition * 60 + 100;

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

}

– (void)createArrowKey

{

    CGRect rect = [self getFrame];

    

    UIView *right = [[UIView alloc] initWithFrame:CGRectMake(rect.size.width80, rect.size.height70, 40, 40)];

    right.backgroundColor = [UIColor blackColor];

    right.layer.cornerRadius = 5;

    right.alpha = 0.8;

    right.tag = 1;

    [self.view addSubview:right];

    

    UIView *left = [[UIView alloc] initWithFrame:CGRectMake(40, rect.size.height70, 40, 40)];

    left.backgroundColor = [UIColor blackColor];

    left.layer.cornerRadius = 5;

    left.alpha = 0.8;

    left.tag = 2;

    [self.view addSubview:left];

    

    NSArray *arrows = @[right, left];

    for (UIView *v in arrows) {

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

        [v addGestureRecognizer:tap];

    }

}

– (void)move:(UITapGestureRecognizer*)gr

{

    if (gr.view.tag == 1 && playerPosition < 6) {

        // right

        playerPosition ++;

    } else if (gr.view.tag == 2 && playerPosition > 0) {

        // left

        playerPosition –;        

    }

    [self showPlayer];

}

– (CGRect)getFrame

{

    float w = self.view.bounds.size.width;

    float h = self.view.bounds.size.height;

    if (w > h) {

        return CGRectMake(0, 0, w, h);

    }

    return CGRectMake(0, 0, h, w);

}

– (void)createEggs

{

    egg = [UIImage imageNamed:@”egg”];

    humptydumpty = [UIImage imageNamed:@”eggBroken”];

    

    eggA = [[UIImageView alloc] initWithImage:egg];

    eggB = [[UIImageView alloc] initWithImage:egg];

    eggC = [[UIImageView alloc] initWithImage:egg];

    eggD = [[UIImageView alloc] initWithImage:egg];

    eggE = [[UIImageView alloc] initWithImage:egg];

    

    NSArray *eggs = @[eggA, eggB, eggC, eggD, eggE];

    for (UIView *v in eggs) {

        v.frame = CGRectMake(0, 0, 20, 30);

        [self.view addSubview:v];

    }

    [self showEggs];

}

– (void)showEggs

{

    NSArray *eggs = @[eggA, eggB, eggC, eggD, eggE];

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

        UIImageView *v = [eggs objectAtIndex:i];

        float x = 110 + i * 60;

        float y = eggPosition[i] * 5050;

        v.center = CGPointMake(x, y);

        v.alpha = 1.0;

        

        if (eggPosition[i] > 4) {

            [self catchCheck:i view:v];

        }

    }

}

– (void)catchCheck:(int)eggNo view:(UIImageView*)v

{

    if (eggNo == playerPosition) {

        // ok! catch

        [self scoreup];

        

        [UIView animateWithDuration:0.4 animations:^{

            v.transform = CGAffineTransformMakeTranslation(0, –20);

            v.alpha = 0;

            eggPosition[eggNo] = 0;

        } completion:^(BOOL finished) {

            v.transform = CGAffineTransformIdentity;

        }];

        

        

    } else {

        // broken

        v.image = humptydumpty;

        [UIView animateWithDuration:0.7 animations:^{

            v.alpha = 0;

            eggPosition[eggNo] = 0;

        } completion:^(BOOL finished) {

            v.image = egg;

            v.alpha = 1;

        }];

    }

}

– (void)start

{

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

}

– (void)tick:(NSTimer*)sender

{

    // random egg

    int rand = arc4random() % 20;

    if (rand < 5 && eggPosition[rand] == 0) {

        eggPosition[rand] = 1;

    }

    

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

        if (eggPosition[i] > 0) {

            eggPosition[i]++;

        }

    }

    [self showEggs];

}

– (void)createScore

{

    score = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 30)];

    score.text = [NSString stringWithFormat:@”%04d”, 0];

    score.font = [UIFont fontWithName:@”DBLCDTempBlack” size:30];

    score.backgroundColor = [UIColor clearColor];

    [self.view addSubview:score];

}

– (void)scoreup

{

    int current = [score.text intValue];

    score.text = [NSString stringWithFormat:@”%04d”, current + 10];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end