别彩白块游戏的实现

本文介绍了如何实现别彩白块游戏,通过创建4*4的按钮矩阵,随机设置黑色块,点击黑色块使最下层消失并下移,点击白色块则游戏结束。详细代码展示了游戏逻辑和事件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,首先创建4*4的16个按钮,先把所有按钮设置为白色,再在每层产生一个0-3的随机数,将对应的按钮设置为黑色显示出来,如图


代码如下:

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

        int arc = arc4random() % 4;

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

            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

            button.frame = CGRectMake(80 * i, 120 * j, 80, 120);

            button.tag = 101 + i + j * 10;

            button.layer.borderWidth = 0.5;

            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

            button.backgroundColor = [UIColor whiteColor];

            if (button.tag == 101 + j * 10 + arc) {

                button.backgroundColor = [UIColor blackColor];

            }

            [self.view addSubview:button];

        }

}

2,每当点击按钮时,通过button的tag值判断,只有点击最下层按钮才会有反应,当点击到黑色块时最下层消失,集体下移一层,当点击到白色块时游戏结束,如图:


代码如下:

- (void)buttonClick:(UIButton *)button

{

    if (button.tag == 131 || button.tag == 132 || button.tag == 133 || button.tag == 134) {

        if (button.backgroundColor == [UIColor whiteColor]) {

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你输了!" delegate:nil cancelButtonTitle:@"再来一次" otherButtonTitles:@"确定", nil];

            [alertView show];

            [alertView release];

        } else {

            NSMutableArray *arr = [NSMutableArray array];

            // 取出上面一排颜色为黑色的按钮的tag

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

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

                    UIButton *button1 = (UIButton *)[self.view viewWithTag:101 + i + j * 10];

                    if (button1.backgroundColor == [UIColor blackColor]) {

                        [arr addObject:[NSString stringWithFormat:@"%d",button1.tag]];

                    }

                }

            }

            int arc = arc4random() % 4;

            int tag = 101 + arc;

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

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

                    UIButton *button3 = (UIButton *)[self.view viewWithTag:101 + i + j * 10];

                    button3.backgroundColor = [UIColor whiteColor];

                    if (button3.tag == tag) {

                        button3.backgroundColor = [UIColor blackColor];

                    }

                }

            }

            for (int i = 0; i < arr.count; i++) {

                int tag = [[arr objectAtIndex:i]integerValue];

                UIButton *button2 = (UIButton *)[self.view viewWithTag:tag + 10];

                button2.backgroundColor = [UIColor blackColor];

            }

            

        }

    }

}


这样就实现了看似复杂的别踩白块游戏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值