UIbutton实现打地鼠的小游戏

来源:互联网 发布:淘宝放心淘是什么意思 编辑:程序博客网 时间:2024/05/16 09:22

//


图片需要自己添加,主要使用了UIButton来实现打地鼠的操作.

1)button添加图片的时候最好使用setBackgroundImage:这个方法,这个图片可以根据按钮的大小不同的变化

2)button的命名,不是使用alloc 而是有自己的方法.

3)此游戏的逻辑不好想,不过一般人还是能看懂的.

//  AppDelegate.m

//  Move

//

//  Created by Apple on 15/6/18.

//  Copyright (c) 2015 zhanghao. All rights reserved.

//


#import "AppDelegate.h"


@interface AppDelegate ()

{


    NSInteger _index;//记录上一次的tag

    

    NSInteger _count;//记录打击地鼠次数

    BOOL _isLock;   //是否锁上

}

@end


@implementation AppDelegate

-(void)dealloc

{

    [_window  release];

    [super dealloc];

}


- (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];

    for (int i = 0; i<2; i++) {

        for (int j=0; j<3; j++) {

            //循环添加的控件都要加tag

            UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];

            button.frame = CGRectMake(10+(100+20)*j,20+(100+20)*i, 100, 100);

            [button setBackgroundImage:[UIImage imageNamed:@"0.png" ] forState:UIControlStateNormal];

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

            //tag

            button.tag = 100 +(i*3+j);

            [_window addSubview:button];

            }

        

    

        

    }

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(150, 400, 80, 40)];

    label.backgroundColor = [UIColor yellowColor];

    label.tag = 200;

    

    //text赋值

    label.text = [NSString stringWithFormat:@"%ld",_count];

    

    label.textAlignment =NSTextAlignmentCenter;

    

    

    

    

    

    [_window addSubview:label];

    

    //计时器

    [ NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];

    //赋初值

    _index = 100;

    

    

    return YES;

}



-(void)buttonAction:(UIButton *)sender

{

    

    if((sender.tag == _index)&&(_isLock == NO)){

    //换图片

    [sender setBackgroundImage:[UIImage imageNamed:@"4.png"] forState:(UIControlStateNormal)];

        

        //label

        

        UILabel *tempLabel = (UILabel *)[_window viewWithTag:200];

        

        //计数+1并显示

        tempLabel.text = [NSString stringWithFormat:@"%ld",++_count];

        //上锁

        _isLock = YES;

    

    }

    



}

-(void)timerAction:(NSTimer *)sender

{

    UIButton *lastbutton = (UIButton *)[_window viewWithTag:_index];

    [lastbutton setBackgroundImage:[UIImage imageNamed:@"0.png"] forState:UIControlStateNormal];

    

    

    //随机tag

    _index = arc4random()%6+100;

    //通过tag获取button

    UIButton *tempButton = (UIButton *)[_window viewWithTag:_index];

    

    //设置图片

    [tempButton setBackgroundImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateNormal];

    //解锁

    _isLock = NO;

    

}

- (void)applicationWillResignActive:(UIApplication *)application {


}


- (void)applicationDidEnterBackground:(UIApplication *)application {


}


- (void)applicationWillEnterForeground:(UIApplication *)application {


}


- (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


0 0
原创粉丝点击