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

发布时间:2020-06-23 09:49:29 作者:Im刘亚芳
来源:网络 阅读:626

有两个图片

       


新建一个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 CloseLight
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(15, 50, 290, 310)];
    view.backgroundColor = [UIColor brownColor];
    view.layer.cornerRadius = 10;
    [self.view addSubview:view];
    [view release];
    
    _tag = 10000;
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j ++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setFrame:CGRectMake(10 + 10 * j + 45 * j, 14 + 14 * i + 45 * i, 50, 50)];
//            [button setBackgroundColor:[UIColor redColor]];
            [button setBackgroundImage:[UIImage p_w_picpathNamed:@"1"] forState:UIControlStateNormal];
            button.layer.cornerRadius = 5;
            [button addTarget:self action:@selector(gobutton:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = _tag +1000 *(i + 1) + (j + 1);
            [view addSubview:button];
        }
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"开始游戏" forState:UIControlStateNormal];
    button.frame = CGRectMake(120, 310, 60, 30);
    [button addTarget:self action:NULL forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
}
- (void)change:(UIButton *)button
{
//    UIColor *newColor = button.backgroundColor;
    UIImage *p_w_picpath = [button backgroundImageForState:1];
    if (p_w_picpath == [UIImage p_w_picpathNamed:@"1"]) {
//        [button setBackgroundColor:[UIColor blackColor]];
        [button setBackgroundImage:[UIImage p_w_picpathNamed:@"2"] forState:UIControlStateNormal];
    }else if(p_w_picpath == [UIImage p_w_picpathNamed:@"2"]){
//        [button setBackgroundColor:[UIColor redColor]];
        [button setBackgroundImage:[UIImage p_w_picpathNamed:@"1"] forState:UIControlStateNormal];
    }
}
- (void)gobutton:(id)sender
{
    UIButton *newbutton = (UIButton *)sender;
    [self change:(UIButton *)[self.view viewWithTag:newbutton.tag] ];
    [self change:(UIButton *)[self.view viewWithTag:newbutton.tag + 1]];
    [self change:(UIButton *)[self.view viewWithTag:newbutton.tag - 1]];
    [self change:(UIButton *)[self.view viewWithTag:newbutton.tag + 1000]];
    [self change:(UIButton *)[self.view viewWithTag:newbutton.tag - 1000]];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



AppDelegate.m

#import "AppDelegate.h"
#import "CloseLight.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    CloseLight *light = [[CloseLight alloc] init];
    self.window.rootViewController = light;
    
    
    [_window release];
    return YES;
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end




附件:http://down.51cto.com/data/2364827
推荐阅读:
  1. Android 实现开关灯效果
  2. 小代码 链表实现关灯游戏2

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

游戏 新建 interface

上一篇:微信小程序实现顶部选项卡的方法

下一篇:如何在Java中设置Socket超时时间

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》