一個(gè)擲骰子的小游戲

#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];  //點(diǎn)擊添加事件

    [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];
  
}
界面顯示
//開始游戲按鈕點(diǎn)擊事件
    - (void)beginClick:(id)sender
    {
        //整數(shù) NSInterger    
        //取余 [a~b的范圍]  公式:arc4random()%(a-b+1)+a 
        NSInteger left = arc4random()%6 + 1;  // 1~6
        NSInteger right = arc4random()%6 +1;

        //賦給左右兩個(gè)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];

    }


//清除按鈕點(diǎn)擊事件
    - (void)clearClick:(id)sender
    {
        _leftLabel.text = [NSString stringWithFormat:@"0"];
        _rightLabel.text = [NSString stringWithFormat:@"0"];
    }
擲骰子小游戲.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容