zoukankan      html  css  js  c++  java
  • 关灯游戏的实现

     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;
            }
        }
    }
    
    设置控制周围灯的亮暗,直到最后将所有的灯点灭为止.


  • 相关阅读:
    bzoj3160(FFT+回文自动机)
    bzoj2555(后缀自动机+LCT)
    luogu P4299 首都
    Annihilate(SA)
    [FJOI2016]神秘数(脑洞+可持久化)
    [ZJOI2016]大森林(LCT)
    bzoj3756pty的字符串(后缀自动机+计数)
    UVa 11582 Colossal Fibonacci Numbers! (斐波那契循环节 & 快速幂)
    UVa 12563 Jin Ge Jin Qu hao (0-1背包)
    UVa 437 The Tower of Babylon (DAG最长路)
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4086619.html
Copyright © 2011-2022 走看看