パネルを全部消したらクリアというゲームです。まず、パネルを一つタッチしたら、それとおなじ色のパネルを全てタッチ。全てタッチすると、その色のパネルが消えるので、次の色のパネルをタッチしてまたその色のパネルを全部タッチしていきましょう。というiPhoneゲームの作り方を書いてみます。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
ポイント
タイマーを画面上の中央部分に表示、フォントは、DBLCDTempBlackを使ってデジタルっぽくしています。パネルにはTapGestureRecognizerを設定して、中でその色のパネルを全部タッチしたか、違うパネルをタッチしたか、クリアかという判定をしています。
サンプルコード
#import “ViewController.h”
#define UIColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@interface ViewController () {
NSTimer *timer;
UILabel *timerLabel;
UIColor *selectedColor;
NSMutableArray *selectedBlock;
int counter;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self createRect];
[self createTimer];
}
– (void)createRect
{
float w = 320.0 / 6.0;
float h = w;
for (int i=0; i<36; i++) {
float x = (i % 6) * w;
float y = (i / 6) * h + 150;
UIView *block = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
block.backgroundColor = [self randomcolor];
[self.view addSubview:block];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[block addGestureRecognizer:tap];
}
}
– (void)createTimer
{
timerLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 50, 200, 50)];
timerLabel.backgroundColor = [UIColor blackColor];
timerLabel.font = [UIFont fontWithName:@”DBLCDTempBlack” size:50];
timerLabel.text = @”00.00″;
timerLabel.textColor = [UIColor whiteColor];
timerLabel.textAlignment = 1;
[self.view addSubview:timerLabel];
}
– (void)tap:(UITapGestureRecognizer*)gr
{
if (!timer) {
timerLabel.text = @”00.00″;
timer = [NSTimer scheduledTimerWithTimeInterval:5.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
if (!selectedColor) {
selectedColor = gr.view.backgroundColor;
}
if (!selectedBlock) {
selectedBlock = [[NSMutableArray alloc] init];
}
if ([selectedColor isEqual:gr.view.backgroundColor]) {
gr.view.transform = CGAffineTransformMakeScale(0.3, 0.3);
[selectedBlock addObject:gr.view];
BOOL completeColor = YES;
for (UIView *v in self.view.subviews) {
if ([v.backgroundColor isEqual:selectedColor]
&& CGAffineTransformIsIdentity(v.transform)) {
completeColor = NO;
}
}
if (completeColor) {
for (UIView *v in selectedBlock) {
[UIView animateWithDuration:0.5 animations:^{
v.transform = CGAffineTransformMakeScale(0.0, 0.0);
} completion:^(BOOL finished) {
[v removeFromSuperview];
}];
}
selectedColor = nil;
// clear check
counter++;
if (counter == 5) {
[self finish];
}
}
} else {
for (UIView *v in selectedBlock) {
[UIView animateWithDuration:0.5 animations:^{
v.transform = CGAffineTransformIdentity;
}];
}
[selectedBlock removeAllObjects];
selectedColor = nil;
}
}
– (void)tick:(NSTimer*)sender
{
float t = [timerLabel.text floatValue] + sender.timeInterval;
timerLabel.text = [NSString stringWithFormat:@”%05.2f”, t];
}
– (void)finish
{
[timer invalidate];
timer = nil;
selectedBlock = nil;
selectedColor = nil;
counter = 0;
[self createRect];
}
– (UIColor*)randomcolor
{
int i = arc4random() % 5;
switch (i) {
case 0:
return UIColorHex(0xE46F3D);
case 1:
return UIColorHex(0xF6CB28);
case 2:
return UIColorHex(0x26C281);
case 3:
return UIColorHex(0xC9475A);
case 4:
return UIColorHex(0x3C488C);
default:
break;
}
return nil;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end