3分でできるプログラミング

車を並べたいだけ並べて遊べるゲームです。
子供には、「並べたい」という子供の成長に重要な時期があるそうです。

くるまを並べよう

道具
 Xcode (これ書いている時点で使っているのは、ver4.3です) 
 iOS  (5.1 でためしました。)

Single View Application でプロジェクトを作成した後に、ViewController.m にこんな感じで実装を追加しましょう。(このアプリは、ViewController の実装のみで完了です。)

#import “ViewController.h”

@interfaceViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorgreenColor];

    // road

    // horizontal

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

        double x = 0;

        double y = 120 * i + 35;

        double width = self.view.frame.size.width;

        double height = 80;

        UIView *road = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];

        road.backgroundColor = [UIColordarkGrayColor];

        // center line

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

            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 5)];

            line.center = CGPointMake(j * 70, road.bounds.size.height / 2.0);

            line.backgroundColor = [UIColor whiteColor];

            [road addSubview:line];

            [self.view addSubview:road];

        }

    }

    // 車を並べる

    [selflineupCars];

    // 発車ボタン

    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(0,0, 50, 20);

    btn.center = CGPointMake(self.view.center.x, 15);

    [btn setTitle:@”Go!”forState:UIControlStateNormal];

    [btn addTarget:selfaction:@selector(go) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

}

– (void)lineupCars

{

    double carCenterY = self.view.bounds.size.height60;

    // clean old lineup

    for (UIView *v inself.view.subviews) {

        if (v.center.y == carCenterY) {

            [v removeFromSuperview];

        }

    }

    // 注意: mac じゃないと文字コードで化けるかも。

    // icons には使いたい絵文字を , 区切りで入れてください。

    NSArray *icons = [@”

← 過去の投稿へ

次の投稿へ →