在这个游戏里要理自己的思路,例如我从下向上写起, 如果你想要最终的结果,首先要考虑如何实现视图,那么就在LightView.m 开始布局视图
#import "LightView.h"
#import "LightButton.h"
#define kRow_Number 9 //行数
#define kColumn_Number 8 //列数
#define kButton_Width 40 //button的宽度
#define kButton_Height 40 //button的高度
#define kMargin_Top 100 //距上边界的距离
#define kMargin_Left 25 //距左边界的距离
@implementation LightView
//重写init
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.lightBtnArr = [NSMutableArray array];
[self setupTitleView];
[self setupTitleLabel];
[self setupLightView];
[self setupStartButton];
[self setupRestartButton];
}
return self;
}
//初始化titleView
- (void)setupTitleView
{
UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(30,20,260, 30)];
aLabel.textColor = [UIColor blackColor];
aLabel.text =@"Lights the game Endless Edition";
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 blackColor];
_titleLabel.font = [UIFont systemFontOfSize:14];
_titleLabel.text =@"当前亮灯的数量为0";
_titleLabel.textColor = [UIColor redColor];
[self addSubview:_titleLabel];
[_titleLabel release];
}
//初始化关灯界面
- (void)setupLightView
{
static CGFloat x =kMargin_Left;
CGFloat y =kMargin_Top;
for (int i = 0; i < kRow_Number; i++) {
for (int j =0; j <kColumn_Number; j++) {
UIButton *lightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
lightBtn.frame = CGRectMake(x, y, kButton_Width, kButton_Height);
[lightBtn setImage:[UIImage imageNamed:@"1.png"]forState:UIControlStateNormal];
// [lightBtn setImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateSelected];
lightBtn.tag = 100 + 100 * (i + 1) + j;
[self addSubview:lightBtn];
x += kButton_Width;
lightBtn.selected = NO;
[self.lightBtnArr addObject:lightBtn];
}
x = 25;
y += kButton_Height;
}
}
- (void)setupStartButton
{ self.beginButton = [UIButton buttonWithType:UIButtonTypeSystem];
_beginButton.backgroundColor = [UIColor greenColor];
_beginButton.layer.cornerRadius=50;
_beginButton.frame =CGRectMake(40,kMargin_Top + kButton_Height * kRow_Number +30,100,100);
[_beginButton setTitle:@"开始游戏" forState:UIControlStateNormal];
[_beginButton setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
[self addSubview:_beginButton];
}
- (void)setupRestartButton
{
self.restartButton = [UIButton buttonWithType:UIButtonTypeSystem];
_restartButton.backgroundColor = [UIColor orangeColor];
_restartButton.layer.cornerRadius=50;
_restartButton.frame =CGRectMake(180,kMargin_Top + kButton_Height * kRow_Number +30,100,100);
[_restartButton setTitle:@"重新开始" forState:UIControlStateNormal];
[_restartButton setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
[self addSubview:_restartButton];
}
- (void)dealloc
{
self.restartButton =nil;
self.beginButton =nil;
self.lightBtnArr =nil;
[super dealloc];
}
@end
这里VIew里只负责布局视图
然后在LightViewController.m中去进行实现,首先初始化一个视图与屏幕等大,并定义为根视图。
在根视图中为灯泡添加点击事件。注意这里我之前是把所有的button放在了数组中,这里我们要去数组中去取
//为灯泡添加点击事件
for (id n in lv.lightBtnArr) {
if ([n isKindOfClass:[UIButton class]] ) {
[n addTarget:self action:@selector(turnOff:) forControlEvents:UIControlEventTouchUpInside];
}
}
取到数组中的button之后,对button进行定义点击事件,并实现
@interface ViewController ()
{
NSInteger _lightOnNumber;
NSInteger _PassCount;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
LightView *lv = [[LightView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
self.view = lv;
//为灯泡添加点击事件
for (id n in lv.lightBtnArr) {
if ([n isKindOfClass:[UIButton class]] ) {
[n addTarget:self action:@selector(turnOff:) forControlEvents:UIControlEventTouchUpInside];
}
}
//
// [lv.lightBtn addTarget:self action:@selector(turnOff:) forControlEvents:UIControlEventTouchUpInside];
[lv.beginButton addTarget:self action:@selector(beginGame:)forControlEvents:UIControlEventTouchUpInside];
[lv.restartButton addTarget:self action:@selector(restartGame:)forControlEvents:UIControlEventTouchUpInside];
[lv release];
// Do any additional setup after loading the view.
}
//实现turnOff方法
- (void)turnOff:(UIButton *)lightBtn {
[self turnLightButtonWithTag: lightBtn.tag - 1];
[self turnLightButtonWithTag: lightBtn.tag + 1];
[self turnLightButtonWithTag: lightBtn.tag - 100];
[self turnLightButtonWithTag :lightBtn.tag +100];
[self turnLightButtonWithTag:lightBtn.tag]; //改变点击灯泡
}
//根据tag 值获取对应的灯泡,修改灯泡的状态
- (void)turnLightButtonWithTag:(NSInteger)tag {
LightView *lv = (LightView *)self.view;//
UIButton *btn = [self.view viewWithTag:tag];
btn.selected = !btn.selected;
[btn setImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateSelected];//在实现中更改灯泡的图片
if (btn) {
_lightOnNumber += btn.selected ?1 : -1;
lv.titleLabel.text =_lightOnNumber ? [NSString stringWithFormat:@"亮灯的数量为%ld",_lightOnNumber] : @"恭喜你,胜利啦!即将开始下一局...";
if (!_lightOnNumber) {
[self performSelector:@selector(beginGame:) withObject:nil afterDelay:2.0];//delay 延迟
}
}
}
//开始游戏按钮
- (void)beginGame:(UIButton *)btn1
{
btn1.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 turnOff:lightView.lightBtnArr[index]];
}
}
//重新开始按钮
- (void)restartGame:(UIButton *)btn
{
_lightOnNumber = 0;
[self viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end