关灯游戏

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;

//按钮的起始位置,到边界X的位置

CGFloat buttontoX=(self.view.frame.size.width-buttonWidth*index)/(index+1);

CGFloat buttontoY=buttontoX;

//设置计算按钮的个数,初始化赋值为1

self.number=1;

for (int i=0; i<45; i++)

{

int X=i%index;

int Y=i/index;

self.button=[UIButton buttonWithType:UIButtonTypeRoundedRect];

CGFloat buttonX=(buttontoX+buttonWidth)*X+buttontoX;

CGFloat buttonY=(buttontoY+buttonHeigth)*Y+buttontoY+buttonTop;

self.button.frame=CGRectMake(buttonX, buttonY, buttonWidth, buttonHeigth);

self.button.backgroundColor=[UIColor redColor];

self.button.layer.cornerRadius=30;

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

[self.view addSubview:self.button];

self.button.tag=self.number++;

NSLog(@"tag=%ld",(long)self.button.tag);

}

}

//实现位置方法

-(void)testposition:(UIButton *)Button

{

self.button=Button;

int num=(int)self.button.tag;

if (num==1)

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num+1] change];

[[self.view viewWithTag:num+5] change];

}

else if(num==5)

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num-1] change];

[[self.view viewWithTag:num+5] change];

}

else if(num==41)

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num-5] change];

[[self.view viewWithTag:num+1] change];

}

else if(num==45)

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num-1] change];

[[self.view viewWithTag:num-5] change];

}

else if(num%5==1)

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num-5] change];

[[self.view viewWithTag:num+1] change];

[[self.view viewWithTag:num+5] change];

}

else if(num%5==0)

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num-5] change];

[[self.view viewWithTag:num-1] change];

[[self.view viewWithTag:num+5] change];

}

else if(num%5==1)

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num-5] change];

[[self.view viewWithTag:num+1] change];

[[self.view viewWithTag:num-5] change];

}

else

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num-1] change];

[[self.view viewWithTag:num+1] change];

[[self.view viewWithTag:num-5] change];

[[self.view viewWithTag:num+5] change];

}

}

//改变颜色的方法

-(void)change

{

if (self.backgroundColor==[UIColor redColor])

{

self.backgroundColor=[UIColor blueColor];

}

else

{

self.backgroundColor=[UIColor redColor];

}

}

时间: 2024-10-19 22:12:08

关灯游戏的相关文章

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 << " *****提示: 当选择一盏灯时,该灯及旁边的灯状态均会发生变化****&

关灯游戏的实现

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, 5

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