赤いまるでめいろの中を探検しよう。
黄色い四角をいくつ集められるかな?
という感じの子供向けiPhoneアプリのサンプルコードを書いてみた。
ポイント
めいろは、上下左右に壁を付けられるブロックを
5×5の合計25個配置しています、
壁はbit演算で有無を書いてみました。
環境
このiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
NSMutableArray *blocks;
UIView *player;
int direction;
NSTimer *timer;
}
@end
#define xMax 5
#define yMax 5
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createMaze];
[self createApple];
[self createPlayer];
[self createKey];
}
– (void)createMaze
{
// flag : left up right down
int L = 1 << 2;
int R = 1 << 0;
int T = 1 << 1;
int B = 1 << 3;
int type[xMax][yMax] = {
{L|T, L, L, L|B, L|B},
{L|T, L|B, L, B, B},
{T|B, B, B, B, B},
{ T, L, B, B, B},
{T|R, L|R, L|R, R, R},
};
blocks = [[NSMutableArray alloc] init];
for (int i=0; i<xMax; i++) {
for (int j=0; j<yMax; j++) {
UIView *b = [self createBlock:type[i][j]];
b.center = CGPointMake(i * 50 + 50, j * 50 + 100);
b.backgroundColor = [UIColor blackColor];
[self.view addSubview:b];
[blocks addObject:[NSValue valueWithNonretainedObject:b]];
}
}
}
– (UIView*)createBlock:(int)type
{
int left = 1 << 2;
int top = 1 << 1;
int right = 1 << 0;
int bottom = 1 << 3;
UIView *block = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
if (type & left) {
UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 50)];
bar.backgroundColor = [UIColor purpleColor];
[block addSubview:bar];
}
if (type & top) {
UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 5)];
bar.backgroundColor = [UIColor purpleColor];
[block addSubview:bar];
}
if (type & right) {
UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(45, 0, 5, 50)];
bar.backgroundColor = [UIColor purpleColor];
[block addSubview:bar];
}
if (type & bottom) {
UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0, 45, 50, 5)];
bar.backgroundColor = [UIColor purpleColor];
[block addSubview:bar];
}
return block;
}
– (void)createApple
{
for (NSValue *b in blocks) {
UIView *block = [b nonretainedObjectValue];
if (arc4random() % 4) {
UIView *apple = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
apple.backgroundColor = [UIColor yellowColor];
apple.center = [block convertPoint:block.center fromView:self.view];
[block addSubview:apple];
}
}
}
– (void)createPlayer
{
player = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
player.layer.cornerRadius = 15;
player.center = CGPointMake(50, 100);
player.backgroundColor = [UIColor redColor];
[self.view addSubview:player];
}
– (void)createKey
{
CGPoint p[4] = {
CGPointMake(160, 380), // up key
CGPointMake(220, 405), // right key
CGPointMake(160, 430), // down key
CGPointMake(100, 405), // left key
};
for (int i=0; i<4; i++) {
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
v.backgroundColor = [UIColor darkGrayColor];
v.center = p[i];
v.tag = i;
[self.view addSubview:v];
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(press:)];
[v addGestureRecognizer:press];
}
}
– (void)press:(UILongPressGestureRecognizer*)gr
{
if (gr.state == UIGestureRecognizerStateBegan) {
direction = gr.view.tag;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(move) userInfo:nil repeats:YES];
} else {
[timer invalidate];
}
}
– (void)move
{
CGPoint d = CGPointZero;
switch (direction) {
case 0: d = CGPointMake(0, –1); break; // up
case 1: d = CGPointMake(1, 0); break; // left
case 2: d = CGPointMake(0, 1); break; // down
case 3: d = CGPointMake(-1, 0); break; // right
default:
break;
}
CGPoint next = CGPointMake(player.center.x + d.x, player.center.y + d.y);
for (NSValue *b in blocks) {
UIView *block = [b nonretainedObjectValue];
CGPoint nextInBlock = [self.view convertPoint:next toView:block];
nextInBlock = CGPointMake(nextInBlock.x + d.x * player.bounds.size.width/2.0, nextInBlock.y + d.y * player.bounds.size.height/2.0);
UIView *h = [block hitTest:nextInBlock withEvent:nil];
// stop wall
if (h.backgroundColor == [UIColor purpleColor]) {
return;
}
// eat apple
if (h.backgroundColor == [UIColor yellowColor] && h.tag == 0) {
[h bringSubviewToFront:player];
h.tag = 9;
[UIView animateWithDuration:0.5 animations:^{
h.transform = CGAffineTransformMakeScale(4, 4);
h.alpha = 0.5;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 animations:^{
h.transform = CGAffineTransformMakeScale(0.2, 0.2);
h.alpha = 0;
}];
}];
}
}
player.center = next;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end