UI游戏实现 关灯游戏

#import "DYLAppDelegate.h"

#import "RootViewController.h"

@implementation DYLAppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    RootViewController *rootVC = [[RootViewController alloc] init];

    self.window.rootViewController = rootVC;

    [rootVC release];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}


- (void)applicationWillResignActive:(UIApplication *)application

{

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


- (void)applicationDidEnterBackground:(UIApplication *)application

{

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


- (void)applicationWillEnterForeground:(UIApplication *)application

{

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


- (void)applicationDidBecomeActive:(UIApplication *)application

{

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


- (void)applicationWillTerminate:(UIApplication *)application

{

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

- (void)dealloc

{

    self.window = nil;

    [super dealloc];

}

@end

#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 = [[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


#import <UIKit/UIKit.h>


@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


#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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值