#import "RootViewController.h"
#import "LightView.h"
#import "LightButton.h"
@interface RootViewController ()
{
NSInteger _lightOnNumber; //统计亮着灯泡的个数
NSInteger _passCount; //关卡的个数
}
@end
<pre name="code" class="objc">
@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization _lightOnNumber = 0; _passCount
= 0; } return self;}- (void)loadView{ LightView *lightView = [[LightView alloc] initWithFrame:CGRectZero]; lightView.backgroundColor = [UIColor grayColor]; self.view = lightView; [lightView release]; //给button添加点击事件 for (LightButton *btn in lightView.lightBtnArr)
{ [btn addTarget:self action:@selector(turnLightOff:) forControlEvents:UIControlEventTouchUpInside]; } [lightView.beginButton addTarget:self action:@selector(beginGame:) forControlEvents:UIControlEventTouchUpInside];}- (void)viewDidLoad{ [super viewDidLoad];
// Do any additional setup after loading the view.}//按钮触发事件- (void)turnLightOff:(LightButton *)btn{ [self turnLightButtonWithTag:btn.tag - 1]; //左边的灯泡 [self turnLightButtonWithTag:btn.tag + 1]; //右边的灯泡 [self turnLightButtonWithTag:btn.tag - 100]; //上边的灯泡 [self
turnLightButtonWithTag:btn.tag + 100]; //下边的灯泡 [self turnLightButtonWithTag:btn.tag]; //改变点击灯泡}//根据tag值获取对应的lightButton,然后修改button上的图片- (void)turnLightButtonWithTag:(NSInteger)tag{ LightView *lightView = (LightView *)self.view; LightButton *lightButton = (LightButton
*)[lightView viewWithTag:tag]; lightButton.selected = !lightButton.selected; if (lightButton) { _lightOnNumber += lightButton.selected ? 1 : -1; lightView.titleLabel.text = _lightOnNumber ? [NSString stringWithFormat:@"亮灯的数量为%d", _lightOnNumber] : @"恭喜你,胜利啦!即将开始下一局...";
if (!_lightOnNumber) { [self performSelector:@selector(beginGame:) withObject:nil afterDelay:2.0]; } }}- (void)beginGame:(UIButton *)btn{ btn.enabled = NO; //一旦开始游戏,就关掉开始游戏按钮的交互事件 //如果当前关没有完成,就不能到下一关. if (_lightOnNumber) { return; } _passCount++; LightView
*lightView = (LightView *)self.view; NSInteger count = [lightView.lightBtnArr count]; for (int i = 0; i < _passCount; i++) { NSInteger index = arc4random() % count; [self turnLightOff:lightView.lightBtnArr[index]]; }}@end
@interface LightView : UIView
@property (nonatomic, retain) NSMutableArray *lightBtnArr;//存储所有的button
@property (nonatomic, retain) UIButton *beginButton; //存储开始按钮
@property (nonatomic, retain) UILabel *titleLabel;
@end
#import "LightView.h"
#import "LightButton.h"
#define kRow_Number 8 //行数
#define kColumn_Number 7 //列数
#define kButton_Width 40 //button的宽度
#define kButton_Height 40 //button的高度
#define kMargin_Top 90 //距上边界的距离
#define kMargin_Left 20 //距左边界的距离
@implementation LightView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.lightBtnArr = [NSMutableArray array];
[self setupTitleView];
[self setupTitleLabel];
[self setupLightView];
[self setupStartButton];
}
return self;
}
//初始化titleView
- (void)setupTitleView
{
UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 20, 220, 30)];
aLabel.textColor = [UIColor redColor];
aLabel.text = @"关灯游戏无尽版";
aLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:aLabel];
[aLabel release];
}
//初始化titleLabel
- (void)setupTitleLabel
{
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 320, 40)];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.textColor = [UIColor redColor];
_titleLabel.font = [UIFont systemFontOfSize:14];
_titleLabel.text = @"亮灯的数量为0";
[self addSubview:_titleLabel];
[_titleLabel release];
}
//初始化关灯界面
- (void)setupLightView
{
CGFloat x = kMargin_Left;
CGFloat y = kMargin_Top;
for (int i = 0; i < kRow_Number; i++) {
for (int j = 0; j < kColumn_Number; j++) {
LightButton *lightBtn = [LightButton buttonWithType:UIButtonTypeCustom];
lightBtn.frame = CGRectMake(x, y, kButton_Width, kButton_Height);
[lightBtn setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
lightBtn.tag = 100 + 100 * (i + 1) + j;
[self addSubview:lightBtn];
x += kButton_Width;
lightBtn.selected = NO;
// lightBtn.isOff = YES; //标识开始状态为灭的
[self.lightBtnArr addObject:lightBtn];
}
x = 20;
y += kButton_Height;
}
}
- (void)setupStartButton
{
self.beginButton = [UIButton buttonWithType:UIButtonTypeSystem];
_beginButton.backgroundColor = [UIColor greenColor];
_beginButton.layer.cornerRadius= 5;
_beginButton.frame = CGRectMake(50, kMargin_Top + kButton_Height * kRow_Number + kButton_Height, 220, kButton_Height);
[_beginButton setTitle:@"开始游戏" forState:UIControlStateNormal];
[self addSubview:_beginButton];
}
- (void)dealloc
{
self.beginButton = nil;
self.lightBtnArr = nil;
[super dealloc];
}
@end
@interface LightButton : UIButton
////定义一个属性,用来标识button上灯泡的亮灭,如果为YES,则表示灭灯,如果为NO,则表示等亮.
//@property (nonatomic) BOOL isOff;
@end
#import "LightButton.h"
@implementation LightButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setImage:[UIImage imageNamed:@"2"] forState:UIControlStateSelected];
[self setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
}
return self;
}
@end