一个掷骰子的小游戏

作者: 阿年同学 | 来源:发表于2016-08-02 16:27 被阅读306次
#import "ViewController.h"

@interface ViewController ()
{
    UILabel *_leftLabel;  //左边的按钮
    UILabel *_rightLabel;  //右边的按钮
    UIButton *_beginButton;  //开始游戏按钮
    UIButton *_clean;  //清除按钮
}
@end



@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor grayColor];

    [self interface];    
}



- (void)interface{

//左边按钮
    _leftLabel = [[UILabel alloc] init];
    _leftLabel.frame = CGRectMake(79, 150, 69, 50);
    _leftLabel.backgroundColor = [UIColor yellowColor];  //背景色
    _leftLabel.text = @"0";  //初始显示
    _leftLabel.textAlignment = NSTextAlignmentCenter;  //居中显示

    [self.view addSubview:_leftLabel];

//右边按钮    
    _rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(244, 150, 69, 50)];
    _rightLabel.text = @"0";
    _rightLabel.backgroundColor = [UIColor blueColor];
    _rightLabel.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:_rightLabel];


//开始游戏按钮    
    _beginButton = [[UIButton alloc] initWithFrame:CGRectMake(135, 290, 105, 30)];
//    _beginButton = [UIButton buttonWithType:UIButtonTypeCustom];
  #圆角不能显示,有疑问。
    _beginButton.backgroundColor = [UIColor redColor];
    [_beginButton setTitle:@"开始掷骰子" forState:UIControlStateNormal];  //字体
    [_beginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  //字体颜色
    [_beginButton addTarget:self action:@selector(beginClick:) forControlEvents:UIControlEventTouchUpInside];  //点击添加事件

    [self.view addSubview:_beginButton];

//清除按钮    
    _clean = [[UIButton alloc] initWithFrame:CGRectMake(157, 329, 60, 30)];
    _clean.backgroundColor = [UIColor purpleColor];
    [_clean setTitle:@"清除" forState:UIControlStateNormal];
    [_clean addTarget:self action:@selector(clearClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_clean];
  
}
界面显示
//开始游戏按钮点击事件
    - (void)beginClick:(id)sender
    {
        //整数 NSInterger    
        //取余 [a~b的范围]  公式:arc4random()%(a-b+1)+a 
        NSInteger left = arc4random()%6 + 1;  // 1~6
        NSInteger right = arc4random()%6 +1;

        //赋给左右两个label
        _leftLabel.text = [NSString stringWithFormat:@"%ld",left];
        _rightLabel.text = [NSString stringWithFormat:@"%ld",right];

        //    NSLog(@"left is %ld , right is %ld",left,right);

        //    NSString *message = [NSString stringWithFormat:@"left is %ld , right is %ld .",left,right];

        //比较大小显示 
        NSString *title;
        if (left > right) {
            title = @"left is win .";
        }else if (left < right){
            title = @"right is win .";
        }else{
            title = @"平局了";
        }

    //警告框上面部分
        UIAlertController *upper = [UIAlertController alertControllerWithTitle:@"荣耀" message:title preferredStyle:UIAlertControllerStyleAlert];
    //警告框button
        UIAlertAction *under = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {       
        }];

        [upper addAction:under];

        [self presentViewController:upper animated:YES completion:nil];

    }


//清除按钮点击事件
    - (void)clearClick:(id)sender
    {
        _leftLabel.text = [NSString stringWithFormat:@"0"];
        _rightLabel.text = [NSString stringWithFormat:@"0"];
    }
掷骰子小游戏.gif

相关文章

网友评论

    本文标题:一个掷骰子的小游戏

    本文链接:https://www.haomeiwen.com/subject/rcnfsttx.html