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];

setupTitleView];

setupTitleLabel];

setupLightView];

setupStartButton];

setupRestartButton];

}

returnself;

}

//初始化titleView

– (void)setupTitleView

{

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

aLabel.textColor = [UIColorblackColor];

@"Lights the game Endless Edition";

NSTextAlignmentCenter;

[self addSubview:aLabel];

[aLabel release];

}

//初始化titleLabel

– (void)setupTitleLabel

{

alloc

NSTextAlignmentCenter;

_titleLabel.textColor = [UIColorblackColor];

systemFontOfSize

@"当前亮灯的数量为0";

_titleLabel];

}

//初始化关灯界面

– (void)setupLightView

{

kMargin_Left;

kMargin_Top;

for (int i = 0; i < kRow_Number; i++) {

for (int j =0; j <kColumn_Number; j++) {

buttonWithType:UIButtonTypeCustom];

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

imageNamedforState: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

{

buttonWithType:UIButtonTypeSystem];

backgroundColor

layer

kMargin_Top + kButton_Height

setTitleUIControlStateNormal];

setTitleColorblackColor]forState:UIControlStateNormal];

_beginButton];

}

– (void)setupRestartButton

{

buttonWithType:UIButtonTypeSystem];

backgroundColor

layer

kMargin_Top + kButton_Height

setTitleUIControlStateNormal];

setTitleColorblackColor]forState:UIControlStateNormal];

_restartButton];

}

– (void)dealloc

{

restartButton

beginButton

lightBtnArr

[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

setImageimageNamedforState:UIControlStateSelected];

setImageimageNamedforState:UIControlStateNormal];

}

returnself;

}

@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"

@interfaceRootViewController ()

{

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

NSInteger _passCount; //关卡的个数

}

@end

@implementation RootViewController

– (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

_passCount = 0;

}

returnself;

}

– (void)loadView

{

LightView *lightView = [[LightViewalloc]initWithFrame:CGRectZero];

lightView.::1];

self.view = lightView;

[lightView release];

//给button添加点击事件

for (LightButton *btnin lightView.lightBtnArr) {

actionforControlEvents:UIControlEventTouchUpInside];

}

addTargetactionforControlEvents:UIControlEventTouchUpInside];

addTargetactionforControlEvents:UIControlEventTouchUpInside];

}

– (void)viewDidLoad

{

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

turnLightButtonWithTag

}

//根据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;

_lightOnNumber_lightOnNumber] :

if (!_lightOnNumber) {

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

}

}

}

//开始游戏按钮

– (void)beginGame:(UIButton *)btn1

{

//一旦开始游戏,就关掉开始游戏按钮的交互事件

//如果当前关没有完成,就不能到下一关.

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

,也许叔本华是对的,人与人的距离太远会寂寞到寒冷,

UI 代码 关灯小游戏 demo

相关文章:

你感兴趣的文章:

标签云: