关灯游戏无限关



思路:在一个view上建两层button 然后改变最上面一层的背景颜色

效果图


这里用了button 分类


//声明

@interface UIButton (CategoryButton)
+ (UIButton *)roundRectButtonWithFrame:(CGRect)frame title:(NSString *)title target:(id)target action:(SEL)action;

+ (UIButton *)buttonWithType:(UIButtonType)type frame:(CGRect)frame title:(NSString *)title target:(id)target action:(SEL)action;
@end


//实现

@implementation UIButton (CategoryButton)
+ (UIButton *)roundRectButtonWithFrame:(CGRect)frame title:(NSString *)title target:(id)target action:(SEL)action
{
    
    return [UIButton buttonWithType:UIButtonTypeRoundedRect frame:frame title:title target:target action:action];
    
}

+ (UIButton *)buttonWithType:(UIButtonType)type frame:(CGRect)frame title:(NSString *)title target:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:type];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    button.layer.borderWidth=4;
    button.layer.contentsScale=5;
    return button;
    
}
@end


//root.h文件

@interface RootViewController : UIViewController
@property(assign,nonatomic)int state;// 灯的状态
@property(assign,nonatomic)int RandomClick;//随机点击次数
@end




//实现


- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    添加背景图片
//    UIView *registeredView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
//    UIImage *img=[UIImage imageNamed:@"2.png"];
//    [registeredView setBackgroundColor:[UIColor colorWithPatternImage:img]];
//    设置背景颜色
//    registeredView.backgroundColor=[UIColor redColor];
//    [self.view addSubview:registeredView];
    CGFloat x=0;
    CGFloat y=0;
    
    UIView *downView = [[UIView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:downView];
//    downView.tag = 10000;
    
    UIView *downView2 = [[UIView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:downView2];
    downView2.tag = 20000;
    
    for (int i=1; i<6; i++) {
        for (int j=1; j<6; j++) {
            UIButton *button1=[UIButton roundRectButtonWithFrame:CGRectMake(x, y, 64, 96) title:@"" target:self action:@selector(offLamp:)];
            button1.backgroundColor=[UIColor orangeColor];
            [downView addSubview:button1];
            
            UIButton *button=[UIButton roundRectButtonWithFrame:CGRectMake(x, y, 64, 96) title:@"" target:self action:@selector(offLamp:)];
            button.backgroundColor=[UIColor redColor];
            [downView2 addSubview:button];
            button.tag =i*100+j;
//            显示button上的tag值
//            NSString* str = [NSString stringWithFormat:@"%d",button1.tag];
//            [button1 setTitle:str forState:UIControlStateNormal];
//            NSLog(@"%d",button.tag);
//            添加图片,并改变背景的状态
//            [button setBackgroundImage:[UIImage imageNamed:@"imgage.png"] forState:UIControlStateSelected];
         
            x+=64;
        }
        x=0;
        y+=96;
        
    }
    [self initWith];

}
//初始化
-(void)initWith
{
    for (int i=0; i<1+self.RandomClick; i++) {
        UIButton * button1=(UIButton*)[self.view viewWithTag:(arc4random()%5+1)*100+arc4random()%5+1];
        [self offLamp:button1];
    }
    
}
//过关并进入下一关
-(void)win
{
    self.state = 0;
    UIView *view = [self.view viewWithTag:20000];
    for (UIButton *but in [view subviews]) {
        if (but.backgroundColor == [UIColor redColor]) {
            self.state++;
        }
        if (self.state==25) {
            UIAlertView *aView = [[UIAlertView alloc]initWithTitle:@"恭喜你" message:@"你过关了" delegate:self cancelButtonTitle:@"下一关" otherButtonTitles:nil];
            [aView show];
            self.RandomClick++;
            [self initWith];
        }
    }
}
//点击事件/这里如果用seleated还可以优化
-(void)offLamp:(UIButton *)button
{
    if (button.backgroundColor==[UIColor redColor]) {
        button.backgroundColor=[UIColor clearColor];
    }else{
        button.backgroundColor=[UIColor redColor];
    }
    UIView *button1=[self.view viewWithTag:button.tag+100];
    
    if (button1.backgroundColor==[UIColor redColor]) {
        button1.backgroundColor=[UIColor clearColor];
    }else{
        button1.backgroundColor=[UIColor redColor];
    }
    
    UIView *button2=[self.view viewWithTag:button.tag-100];
    if (button2.backgroundColor==[UIColor redColor]) {
        button2.backgroundColor=[UIColor clearColor];
    }else{
        button2.backgroundColor=[UIColor redColor];
    }

    UIView *button3=[self.view viewWithTag:button.tag+1];
    if (button3.backgroundColor==[UIColor redColor]) {
        button3.backgroundColor=[UIColor clearColor];
    }else{
        button3.backgroundColor=[UIColor redColor];
    }

    UIView *button4=[self.view viewWithTag:button.tag-1];
    if (button4.backgroundColor==[UIColor redColor]) {
        button4.backgroundColor=[UIColor clearColor];
    }else{
        button4.backgroundColor=[UIColor redColor];
    }
    [self win];
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值