关灯游戏的实现

 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;
        }
    }
}

设置控制周围灯的亮暗,直到最后将所有的灯点灭为止.

时间: 2024-08-07 12:28:15

关灯游戏的实现的相关文章

IOS 关灯游戏

一, 首先为window创建根视图控制器 在AppDelegate.h文件中 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions //为window创建根视图控制器 RootViewController *rootViewController = [[RootViewController alloc]init]; self.wind

睡觉请关灯 &nbsp; 游戏模拟实现

 首先,关于这个的基本讨论已过一阶段了.现在就是合成版  以后做mcf java等的界面. 留了 一个bug就是 电脑解决完后 会多打印几个矩阵. 改进的地方,1:增加了一种电脑解法,         2:记录用了自增栈   #include"c.h" #include"c++.h" #define  MAX  5 #define N   10 typedef int  elem_type;   class Stack { public:   Stack()  { 

[某鸥实训记][objective-c][第二天][作业]黑白棋+关灯游戏

自己写的..所以可能没什么逻辑性...可能特别水... 环境为ios SDK8.0 选择的Simulator是iPhone6 创建ios SingleViewApplication..然后再ViewController.m中的代码就是全部了 1 // 2 // ViewController.m 3 // 黑白棋0908 4 // 5 // Created by ******* on 15/9/8. 6 // Copyright (c) 2015年 *******. All rights rese

【算法设计与分析基础】关灯游戏

① R.java  用于存储一些全局变量 package lightoff; public class R { /* public static int lightCondition[][] = { {1,0,0,1,1}, {1,0,0,1,1}, {1,0,0,1,1}, {1,0,0,1,1}, }; */ public static int lightCondition[][] = { {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, }; //灯面

小代码 链表实现关灯游戏2

/* cout << " *****************game by  ring light**************************" << endl;     cout << " **** 一个全开的循环串联灯链   为节约资源等待你的关灯动作******" << endl;     cout << " *****提示: 当选择一盏灯时,该灯及旁边的灯状态均会发生变化****&

关灯游戏

ViewController.h @property(strong,nonatomic)UIButton *button; @property(assign,nonatomic)int number; ViewController.m //每一排的个数 int index=5; //按钮的宽度 CGFloat buttonWidth=60; //按钮的高度 CGFloat buttonHeigth=60; //按钮的起始位置,到边界Y的位置 CGFloat buttonTop=20; //按钮的

OC实现简单的关灯游戏

//将灯的图片在视图上布局,5行5列 for (int i = 0; i < 5; i++) { for (int j = 0 ; j < 5; j++) { UIButton *lightButton = [UIButton buttonWithType:UIButtonTypeSystem]; lightButton.frame = CGRectMake(i * 64, 120 + (j *64), 64, 64); [lightButton setBackgroundImage:[UII

简单的没有关卡的关灯游戏

有两个图片 新建一个CloseLight类 CloseLight.h import <UIKit/UIKit.h> @interface CloseLight : UIViewController @property (nonatomic , assign)NSInteger tag; @end CloseLight.m #import "CloseLight.h" @interface CloseLight () @end @implementation CloseLig

关灯游戏(iOS)

在.h中声明属性如下 @property (retain, nonatomic) UIWindow *window; @property (nonatomic, retain) UIView *aView; @property (nonatomic, retain) UIButton *button; 在.m中实现如下 @implementation MAYAppDelegate - (BOOL)application:(UIApplication *)application didFinish