zoukankan      html  css  js  c++  java
  • 拖拖看小游戏

    拖拖看小游戏

    本项目使用了 storyboardxib

    需求整理


    •  一个滑动条

    •  生成随机的目标数字

    •  一个完成按钮

    •  结果显示

    •  积分累积 + 显示

    •  轮数累积 + 显示

    •  一个重新来过按钮

    •  一个显示介绍的按钮

    详细设计


      1. 拖动滑动条吧,让靶心尽可能接近目标

      2. 滑块控件;

      3. 重新来过按钮控件、分数文本控件、轮数文本控件、介绍按钮控件

    1. 生成随机整数(( 0,100 ]

    2. 根据算法[1]得到成绩

    代码实现


      1. Xcode 中点击项目名称,会打开默认界面(General),在 Deployment Info 中,把 Device Orientation 属性的值:Portrait 前的复选框取消选中

      2. main.storyboard,在中间界面的顶部选中 View Controller,然后在右边的面板选择 Attributes Inspector 面板。在该面板中,设置 Orientation 属性为 Landscape。这时,你会看到中间界面的虚拟手机屏幕已经由竖向变为横向了

      1. UILabel 控件,把它摆放到界面的顶部。双击这个控件,输入文字:拖动滑动条吧,让靶心尽可能接近目标

      2. UISlider 控件,把它摆放在上一个控件的下方、屏幕中心的位置

      3. UIButton 控件,把它摆放到上一个控件的下方,输入文字:猜猜看

      4. UIButtonUILabel 控件(两个 UIButton ,一个是 Info Light 类型;四个 UILabel 控件,分别用于显示分数和回合数),放置于屏幕底部,并为控件设置文本

    1. (0, 100] 的随机整数

    2. UISlider 设置值改变事件,为猜猜看按钮、重新来过按钮设置点击事件

    部分重点代码


    • ViewController.m

    1. #import "ViewController.h" 
    2. #import "AboutViewController.h" 
    3.  
    4. @interface ViewController () 
    5.  
    6. @property(nonatomic)int curVal; // 滑块当前的值 
    7. @property(nonatomic)int targetVal; // 目标值(随机生成的数) 
    8. @property(nonatomic)int score; // 得分 
    9. @property(nonatomic)int count; // 回合数 
    10.  
    11. @property (weak, nonatomic) IBOutlet UISlider *slider; 
    12. // 分数显示文本 
    13. @property (weak, nonatomic) IBOutlet UILabel *scoreLabel; 
    14. // 回合数显示文本 
    15. @property (weak, nonatomic) IBOutlet UILabel *countLabel; 
    16.  
    17. @end 
    18.  
    19. @implementation ViewController 
    20.  
    21. // 生成新的随机数,将滑块的值设置为50,并设置得分、回合数文本的显示值 
    22. - (void)startNewRound { 
    23. _targetVal = 1 + arc4random() % 100
    24. _curVal = 50
    25. _slider.value = _curVal; 
    26. _scoreLabel.text = [NSString stringWithFormat:@"%d", _score]; 
    27. _countLabel.text = [NSString stringWithFormat:@"%d", _count]; 

    28.  
    29. - (void)viewDidLoad { 
    30. [super viewDidLoad]; 
    31. // Do any additional setup after loading the view, typically from a nib. 
    32. [self startNewRound]; 

    33.  
    34. // 点击猜猜看按钮触发的事件 
    35. - (IBAction)showAlert:(id)sender { 
    36. int diff = abs(_curVal - _targetVal); 
    37. int points = 100 - diff; 
    38. NSString *title = nil
    39.  
    40. if (diff == 0) { 
    41. title = @"土豪你太牛B了!"
    42. points += 100
    43. } else if (diff < 5) { 
    44. title = @"土豪你太棒了,差一点!"
    45. if (diff == 1) { 
    46. points += 50

    47. } else if (diff < 10) { 
    48. title = @"好吧,勉强算个土豪!"
    49. } else
    50. title = @"不是土豪少来装!"

    51. _score += points; 
    52. _count++; 
    53.  
    54. NSString *msg = [NSString stringWithFormat:@"恭喜高富帅,你的得分是:%d", points]; 
    55. UIAlertController *alertCtr = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert]; 
    56. UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"朕已知晓,爱卿辛苦了" style:UIAlertActionStyleDefault handler: ^(UIAlertAction *action){ 
    57. [self startNewRound]; 
    58. }]; 
    59. [alertCtr addAction:alertAction]; 
    60. [self presentViewController:alertCtr animated:YES completion:nil]; 
    61.  

    62.  
    63. // 滑块值改变的事件 
    64. - (IBAction)slideMove:(id)sender { 
    65. UISlider *slider = (UISlider *)sender; 
    66. _curVal = (int)lroundf(slider.value); 

    67.  
    68. // 重新来过按钮点击事件 
    69. - (IBAction)redo:(id)sender { 
    70. _targetVal = 1 + arc4random() % 100
    71. _curVal = 50
    72. _score = 0
    73. _count = 0
    74. _slider.value = _curVal; 
    75. _scoreLabel.text = [NSString stringWithFormat:@"%d", _score]; 
    76. _countLabel.text = [NSString stringWithFormat:@"%d", _count]; 

    77.  
    78. // 显示信息事件 
    79. - (IBAction)showInfo:(id)sender { 
    80. AboutViewController *controller = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil]; 
    81. controller.modalPresentationStyle = UIModalTransitionStyleFlipHorizontal
    82. [self presentViewController:controller animated:YES completion:nil]; 

    83. @end 
    • AboutViewController.m

    1. #import "AboutViewController.h"

    2. @interface AboutViewController ()

    3. @end

    4. @implementation AboutViewController

    5. - (void)viewDidLoad { 
    6. [super viewDidLoad]; 
    7. // Do any additional setup after loading the view from its nib.


    8. - (void)didReceiveMemoryWarning { 
    9. [super didReceiveMemoryWarning]; 
    10. // Dispose of any resources that can be recreated.


    11. - (IBAction)close:(id)sender { 
    12. [self.presentingViewController dismissViewControllerAnimated:YES completion:nil]; 


    13. @end


    项目下载地址:点我下载

    结束语


      技术的道路,漫漫其修远兮。学习一门新的技术,总是伴随着各种并发症。但只要熬过前期,并坚持在中期,相信,后期一定是辉煌的。


    1. 使用滑块的值减去随机数,取结果的绝对值,绝对值加1就是最终结果。

  • 相关阅读:
    关于位运算(转)
    计蒜客第三场
    数组与指针
    计蒜客第二场
    指针概念
    爬楼梯(动态规划)
    线性表基本操作的实现(合并)
    4123=喵帕斯之天才少女
    3889=神奇的函数
    1586=计算组合数
  • 原文地址:https://www.cnblogs.com/wchhuangya/p/5709679.html
Copyright © 2011-2022 走看看