关灯游戏的实现

 UIImage *ima = [UIImage imageNamed:@"1.png"];
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            ButtonSubview * button = [ButtonSubview buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(5 + 63 * j, 100 + 63 * i, 58, 58);
            [button setBackgroundImage:ima forState:UIControlStateNormal];
            [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 100 + i * 10 + j;
            button.flag = YES;  //0
            [self.view addSubview:button];
        }
    }
    ButtonSubview *btn = [ButtonSubview buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(100, 450, 120, 40);
    [btn setTitle:@"开始" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(touch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

设置背景,以及开始按键

- (void)click:(ButtonSubview *)btn
{
    UIImage *lamp = [UIImage imageNamed:@"2.png"];
    UIImage *ima = [UIImage imageNamed:@"1.png"];
    NSInteger a[5] = {btn.tag, btn.tag + 1,btn.tag - 1,btn.tag + 10, btn.tag - 10};
    for (int i = 0; i < 5; i++) {
        ButtonSubview *button = (ButtonSubview *)[self.view viewWithTag:a[i]];
        if (button.flag == YES) {
            [button setBackgroundImage:lamp forState:UIControlStateNormal];
            button.flag = NO;
        }
        else{
            [button setBackgroundImage:ima forState:UIControlStateNormal];
            button.flag = YES;
        }
    }
}
//设置开始时随机亮的灯
- (void)touch:(ButtonSubview *)bun
{
    UIImage *lamp = [UIImage imageNamed:@"2.png"];
    UIImage *ima = [UIImage imageNamed:@"1.png"];
    for (int i = 100; i < 145; i++) {
        ButtonSubview *button = (ButtonSubview *)[self.view viewWithTag:i];
        button.flag = arc4random() % 2;
        if (button.flag == 1) {
            [button setBackgroundImage:lamp forState:UIControlStateNormal];
            button.flag = 0;
        }
        else{
            [button setBackgroundImage:ima forState:UIControlStateNormal];
            button.flag = 1;
        }
    }
}
设置控制周围灯的亮暗,直到最后将全部的灯点灭为止.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值