UI, 关灯游戏的实现


游戏规则:

首先随机为每个按钮生成不同的状态(开灯或关灯), 然后点击按钮, 进行关灯; 其四周的灯, 如果是亮的, 那么这个灯将会关掉, 如果是关的, 那么该灯将会被打开, 直到所有的灯都被灭掉为止


#import "FootViewController.h"

//定义宏,表示按钮的边长

#define kSize 50

@interface FootViewController () {

    //定义全局变量

    NSInteger temp;

    UIButton *button1;

    UIButton *button2;

    UIButton *button3;

    UIButton *button4;

}


@end


@implementation FootViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColor yellowColor];

    

    //双层for循环,生成25个按钮

    for (NSInteger i = 1; i <= 5; i++) {

        for (NSInteger j = 1; j <= 5; j++) {

            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

            //用于随机生成按钮的背景图片的判断

            temp = 1;

            button.frame = CGRectMake(i * 60 - 25, j * 60 + 50, kSize, kSize);

            //当随机数为1,背景图片为guan(关灯)图片,当为0(不为1),背景图片为open(开灯)图片

            if (temp == arc4random() % 2) {

                [button setBackgroundImage:[UIImage imageNamed:@"guan.jpg"] forState:UIControlStateNormal];

            }else {

                [button setBackgroundImage:[UIImage imageNamed:@"open.jpg"] forState:UIControlStateNormal];

            }

            //给每个按钮设置tag,便于通过tag值找到按钮

            button.tag = 10 * j + i;

            

            //给按钮关联方法

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

            //将按钮加到父视图上,进行显示

            [self.view addSubview:button];

            //释放内存空间

            [button release];

            

        }

    }

    

}


//按钮四周按钮的开灯状态变化

- (void)changeLight:(UIButton *)button {

    

    NSInteger tag = button.tag;

    //通过buttontag,找到其上下左右的四个按钮

    button1 = (UIButton *)[self.view viewWithTag:tag + 1];//右按钮

    button2 = (UIButton *)[self.view viewWithTag:tag - 1];//左按钮

    button3 = (UIButton *)[self.view viewWithTag:tag + 10];//上按钮

    button4 = (UIButton *)[self.view viewWithTag:tag - 10];//下按钮

    

    //判断按钮右边按钮的亮灯状态

    if ([button1.currentBackgroundImage isEqual:[UIImage imageNamed:@"guan.jpg"]]) {

        

        [button1 setBackgroundImage:[UIImage imageNamed:@"open.jpg"] forState:UIControlStateNormal];

    }else {

        [button1 setBackgroundImage:[UIImage imageNamed:@"guan.jpg"] forState:UIControlStateNormal];

    }

    

    //判断按钮左边按钮的亮灯状态

    if ([button2.currentBackgroundImage isEqual:[UIImage imageNamed:@"guan.jpg"]]) {

        

        [button2 setBackgroundImage:[UIImage imageNamed:@"open.jpg"] forState:UIControlStateNormal];

    }else {

        [button2 setBackgroundImage:[UIImage imageNamed:@"guan.jpg"] forState:UIControlStateNormal];

    }

    

    //判断按钮上边按钮的亮灯状态

    if ([button3.currentBackgroundImage isEqual:[UIImage imageNamed:@"guan.jpg"]]) {

        

        [button3 setBackgroundImage:[UIImage imageNamed:@"open.jpg"] forState:UIControlStateNormal];

    }else {

        [button3 setBackgroundImage:[UIImage imageNamed:@"guan.jpg"] forState:UIControlStateNormal];

    }

    

    //判断按钮下边按钮的亮灯状态

    if ([button4.currentBackgroundImage isEqual:[UIImage imageNamed:@"guan.jpg"]]) {

        

        [button4 setBackgroundImage:[UIImage imageNamed:@"open.jpg"] forState:UIControlStateNormal];

    }else {

        [button4 setBackgroundImage:[UIImage imageNamed:@"guan.jpg"] forState:UIControlStateNormal];

    }


    

}


//button绑定的方法

- (void)press:(UIButton *)button {

    

    if ([button.currentBackgroundImage isEqual:[UIImage imageNamed:@"guan.jpg"]]) {//按下的按钮是 关灯

        

        //把按钮变为开灯

        [button setBackgroundImage:[UIImage imageNamed:@"open.jpg"] forState:UIControlStateNormal];

        [self changeLight:button];

        

    } else {//按下的按钮是开灯

        //把按钮变为关灯

        [button setBackgroundImage:[UIImage imageNamed:@"guan.jpg"] forState:UIControlStateNormal];

        [self changeLight:button];

    }

    

    

}

@end


效果图






注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: