iOS 简单的猜颜色游戏 界面设计的比较简单 - -

本文介绍了一个基于iOS平台的游戏计分系统实现方法,通过使用UIKit框架中的UI元素如UILabel和UIButton来创建游戏界面,并利用NSTimer进行倒计时及颜色切换等功能。文章详细展示了如何响应用户点击事件以增加或减少分数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@interface ViewController ()

{

    UILabel *jifen;

    UILabel *label;

    UIButton *button;

    UIButton *button1;

    NSTimer *timer;

    NSTimer *timer1;

    NSArray *color;

    int showTime;

    UILabel *NO1;

    UILabel *show;

    NSArray *anniu;

    int b;

    int index;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

//    开始按钮相关

    button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(150, 647, 80, 40);

    button.center = CGPointMake(187.5, 627);

    button.backgroundColor = [UIColor darkTextColor];

    button.alpha = 0.6;

    [button setTitle:@"开始" forState:UIControlStateNormal];

    button.layer.cornerRadius = 4;

    button.layer.masksToBounds = YES;

    [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateHighlighted];

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

    [self.view addSubview:button];

    

  // 颜色按钮

    anniu = @[@"橘红色",@"黑色",@"蓝色"];

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

        button1 = [UIButton buttonWithType:UIButtonTypeCustom];

        button1.frame =CGRectMake(61.5+(80+6)*i, 500, 80, 40);

        button1.backgroundColor = [UIColor blackColor];

        button1.alpha = 0.6;

        button1.tag = 1000+i;

        button1.layer.cornerRadius = 4;

        button1.layer.masksToBounds = YES;

        [button1 setTitle:anniu[i] forState:UIWindowLevelNormal];

        [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];

        [button1 addTarget:self action:@selector(jifen:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:button1];

    }

    color = @[

              [UIColor orangeColor],

               [UIColor blackColor],

               [UIColor blueColor]

              ];

//    颜色显示框

    NO1 =[[UILabel alloc]init];

    NO1.backgroundColor = [UIColor lightGrayColor];

    NO1.frame = CGRectMake(200, 400, 100, 40);

    NO1.center = CGPointMake(187.5, 320);

    NO1.layer.cornerRadius = 4;

    NO1.layer.masksToBounds = YES;

    [self.view addSubview:NO1];

 

    // 计数器相关

    showTime = 60;

    label = [[UILabel alloc]init];

    label.frame = CGRectMake(0, 0, 100, 100);

    label.textAlignment = NSTextAlignmentCenter;

    label.font =[UIFont systemFontOfSize:50];

    label.center = CGPointMake(187.5, 250); 

    label.text = [@(showTime)stringValue];

    [self.view addSubview:label];

//   定时器

   timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(change) userInfo:nil repeats:YES];

    timer.fireDate = [NSDate distantFuture];

//   定时器1

    timer1 = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(turn) userInfo:nil repeats:YES];

    timer1.fireDate = [NSDate distantFuture];


//   积分显示框

    jifen = [[UILabel alloc]init];

    jifen.frame = CGRectMake(0, 0, 120, 50);

    jifen.center = CGPointMake(187.5, 130);

    jifen.textAlignment = NSTextAlignmentCenter;

    jifen.font = [UIFont systemFontOfSize:40];

    jifen.text = [@(b)stringValue];

    jifen.layer.cornerRadius = 4;

    jifen.layer.masksToBounds = YES;

    jifen.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview:jifen];


    

}

// turn 方法

- (void)turn

{

    int i = arc4random()%color.count;

    

    index = i;

    

    NO1.text = @"颜色";

    NO1.textAlignment = NSTextAlignmentCenter;

    NO1.textColor = color[i];

    if (showTime == 0) {

        timer1.fireDate = [NSDate distantFuture];

        showTime = 60;

        b = 0;

        jifen.text = [@(b)stringValue];

        NO1.textColor = [UIColor blackColor];

    }

  }


// 开始按钮

- (void)start

{

    timer.fireDate = [NSDate distantPast];

    timer1.fireDate = [NSDate distantPast];

}


// change 方法

-(void)change

{

    showTime --;

    

    if (showTime == 0) {

        timer.fireDate = [NSDate distantFuture];

        

        UIAlertView *tishi = [[UIAlertView alloc]initWithTitle:@"提示" message:@"时间到" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        

                [tishi show];

    }

    

    label.text = [@(showTime)stringValue];

  

}


// 积分方法

- (void)jifen:(UIButton *)sender

{

    

    if (sender.tag-1000 ==index) {

        b += 5;

        jifen.text = [@(b)stringValue];

    }else{

        b -=5;

        jifen.text = [@(b)stringValue];

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值