UI 代码 关灯小游戏 demo

光灯小游戏

设计一个自己喜欢的行数列数的灯泡界面.

玩法:当你点击其中一盏灯泡时(如果是打开的状态,将要关闭,如果是关闭的状态,将要打开),被点击灯泡的上下左右四个灯泡(如果处于关闭状态,那么将要打开,如果处于打开状态,那么将要关闭)都会随着自己的状态而改变.直到全部关闭.获得胜利.

素材:

     

效果图:


知识点:

  tag值得运用

思路:

1.新建一个UIViewController视图控制器文件 (用来实现事件)

2.新建一个UIView 类文件(LightView) 用于对整个页面做一个布局

3.新建一个UIView 类文件(LightButton) 用来实现灯泡按钮事件(灯泡按钮的关闭打开状态)


1.布局视图:

//  LightView.m

//  LightOffDemo

//  Created by Summer on 14-8-30.

//  Copyright (c) 2014年 summer2014mht@sina.com. All rights reserved.


#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 = [NSMutableArrayarray];

        [selfsetupTitleView];

        [selfsetupTitleLabel];

        [selfsetupLightView];

        [selfsetupStartButton];

        [selfsetupRestartButton];

    }

    return self;

}

//初始化titleView

- (void)setupTitleView

{

    UILabel *aLabel = [[UILabelalloc]initWithFrame:CGRectMake(30,20,260, 30)];

    aLabel.textColor = [UIColorblackColor];

    aLabel.text =@"Lights the game Endless Edition";

    aLabel.textAlignment =NSTextAlignmentCenter;

    [self addSubview:aLabel];

    [aLabel release];

}

//初始化titleLabel

- (void)setupTitleLabel

{

    self.titleLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,50,320,40)];

    _titleLabel.textAlignment =NSTextAlignmentCenter;

    _titleLabel.textColor = [UIColorblackColor];

    _titleLabel.font = [UIFontsystemFontOfSize:14];

    _titleLabel.text =@"当前亮灯的数量为0";

    [selfaddSubview:_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 = [LightButtonbuttonWithType:UIButtonTypeCustom];

            lightBtn.frame = CGRectMake(x, y, kButton_Width, kButton_Height);

            [lightBtn setImage:[UIImageimageNamed:@"1.png"]forState:UIControlStateNormal];

            lightBtn.tag = 100 + 100 * (i + 1) + j;

            [self addSubview:lightBtn];

            x += kButton_Width;

            lightBtn.selected = NO;

            [self.lightBtnArraddObject:lightBtn];

        }

        x = 20;

        y += kButton_Height;

    }

}

- (void)setupStartButton

{

    self.beginButton = [UIButtonbuttonWithType:UIButtonTypeSystem];

    _beginButton.backgroundColor = [UIColorgreenColor];

    _beginButton.layer.cornerRadius=50;

    _beginButton.frame =CGRectMake(40,kMargin_Top + kButton_Height * kRow_Number +30,100,100);

    [_beginButtonsetTitle:@"开始游戏" forState:UIControlStateNormal];

    [_beginButtonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];

    [selfaddSubview:_beginButton];

}

- (void)setupRestartButton

{

    self.restartButton = [UIButtonbuttonWithType:UIButtonTypeSystem];

    _restartButton.backgroundColor = [UIColororangeColor];

    _restartButton.layer.cornerRadius=50;

    _restartButton.frame =CGRectMake(180,kMargin_Top + kButton_Height * kRow_Number +30,100,100);

    [_restartButtonsetTitle:@"重新开始" forState:UIControlStateNormal];

    [_restartButtonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];

    [selfaddSubview:_restartButton];

}

- (void)dealloc

{

    self.restartButton =nil;

    self.beginButton =nil;

    self.lightBtnArr =nil;

    [super dealloc];

}

@end

2.button的状态切换

//  LightButton.m

//  LightOffDemo

//  Created by Summer on 14-8-30.

//  Copyright (c) 2014年 summer2014mht@sina.com. All rights reserved.


#import "LightButton.h"

@implementation LightButton

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        [selfsetImage:[UIImageimageNamed:@"2"]forState:UIControlStateSelected];

        [selfsetImage:[UIImageimageNamed:@"1"]forState:UIControlStateNormal];

    }

    return self;

}

@end


3.事件的实现

//  RootViewController.m

//  LightOffDemo

//  Created by Summer on 14-8-30.

//  Copyright (c) 2014年 summer2014mht@sina.com. All rights reserved.


#import "RootViewController.h"

#import "LightView.h"

#import "LightButton.h"

@interface RootViewController ()

{

    NSInteger _lightOnNumber; //统计亮着灯泡的个数

    NSInteger _passCount; //关卡的个数

}

@end


@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 = [[LightViewalloc]initWithFrame:CGRectZero];

    lightView.backgroundColor = [UIColorcolorWithRed:1.0green:0.9 blue:1.0alpha:1];

    self.view = lightView;

    [lightView release];

    //button添加点击事件

    for (LightButton *btnin lightView.lightBtnArr) {

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

    }

    [lightView.beginButtonaddTarget:selfaction:@selector(beginGame:)forControlEvents:UIControlEventTouchUpInside];

    [lightView.restartButtonaddTarget:selfaction:@selector(restartGame:)forControlEvents:UIControlEventTouchUpInside];

}

- (void)viewDidLoad

{

    [superviewDidLoad];

    // 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]; //下边的灯泡

    [selfturnLightButtonWithTag:btn.tag]; //改变点击灯泡

}

//根据tag值获取对应的lightButton,然后修改button上的图片

- (void)turnLightButtonWithTag:(NSInteger)tag

{

    LightView *lightView = (LightView *)self.view;

    LightButton *lightButton = (LightButton *)[lightViewviewWithTag:tag];

    lightButton.selected = !lightButton.selected;

    if (lightButton) {

        _lightOnNumber += lightButton.selected ?1 : -1;

        lightView.titleLabel.text =_lightOnNumber ? [NSStringstringWithFormat:@"亮灯的数量为%d",_lightOnNumber] : @"恭喜你,胜利啦!即将开始下一局...";

        if (!_lightOnNumber) {

            [self performSelector:@selector(beginGame:) withObject:nil afterDelay:2.0];

        }

    }

}

//开始游戏按钮

- (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 turnLightOff:lightView.lightBtnArr[index]];

    }

}

//重新开始按钮

- (void)restartGame:(UIButton *)btn

{

    _lightOnNumber = 0;

    [self loadView];

}

@end



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值