拖拖看小游戏
拖拖看小游戏
本项目使用了 storyboard 和 xib
需求整理
-
一个滑动条
-
生成随机的目标数字
-
一个完成按钮
-
结果显示
-
积分累积 + 显示
-
轮数累积 + 显示
-
一个重新来过按钮
-
一个显示介绍的按钮
详细设计
-
-
拖动滑动条吧,让靶心尽可能接近目标;
-
滑块控件;
-
重新来过按钮控件、分数文本控件、轮数文本控件、介绍按钮控件;
-
-
生成随机整数(( 0,100 ])
-
根据算法[1]得到成绩
代码实现
-
-
Xcode 中点击项目名称,会打开默认界面(General),在 Deployment Info 中,把 Device Orientation 属性的值:Portrait 前的复选框取消选中
-
main.storyboard,在中间界面的顶部选中 View Controller,然后在右边的面板选择 Attributes Inspector 面板。在该面板中,设置 Orientation 属性为 Landscape。这时,你会看到中间界面的虚拟手机屏幕已经由竖向变为横向了
-
-
-
UILabel 控件,把它摆放到界面的顶部。双击这个控件,输入文字:拖动滑动条吧,让靶心尽可能接近目标
-
UISlider 控件,把它摆放在上一个控件的下方、屏幕中心的位置
-
UIButton 控件,把它摆放到上一个控件的下方,输入文字:猜猜看
-
UIButton、UILabel 控件(两个 UIButton ,一个是 Info Light 类型;四个 UILabel 控件,分别用于显示分数和回合数),放置于屏幕底部,并为控件设置文本
-
-
(0, 100] 的随机整数
-
UISlider 设置值改变事件,为猜猜看按钮、重新来过按钮设置点击事件
部分重点代码
-
ViewController.m
- #import "ViewController.h"
- #import "AboutViewController.h"
-
- @interface ViewController ()
-
- @property(nonatomic)int curVal; // 滑块当前的值
- @property(nonatomic)int targetVal; // 目标值(随机生成的数)
- @property(nonatomic)int score; // 得分
- @property(nonatomic)int count; // 回合数
-
- @property (weak, nonatomic) IBOutlet UISlider *slider;
- // 分数显示文本
- @property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
- // 回合数显示文本
- @property (weak, nonatomic) IBOutlet UILabel *countLabel;
-
- @end
-
- @implementation ViewController
-
- // 生成新的随机数,将滑块的值设置为50,并设置得分、回合数文本的显示值
- - (void)startNewRound {
- _targetVal = 1 + arc4random() % 100;
- _curVal = 50;
- _slider.value = _curVal;
- _scoreLabel.text = [NSString stringWithFormat:@"%d", _score];
- _countLabel.text = [NSString stringWithFormat:@"%d", _count];
- }
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- [self startNewRound];
- }
-
- // 点击猜猜看按钮触发的事件
- - (IBAction)showAlert:(id)sender {
- int diff = abs(_curVal - _targetVal);
- int points = 100 - diff;
- NSString *title = nil;
-
- if (diff == 0) {
- title = @"土豪你太牛B了!";
- points += 100;
- } else if (diff < 5) {
- title = @"土豪你太棒了,差一点!";
- if (diff == 1) {
- points += 50;
- }
- } else if (diff < 10) {
- title = @"好吧,勉强算个土豪!";
- } else {
- title = @"不是土豪少来装!";
- }
- _score += points;
- _count++;
-
- NSString *msg = [NSString stringWithFormat:@"恭喜高富帅,你的得分是:%d", points];
- UIAlertController *alertCtr = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"朕已知晓,爱卿辛苦了" style:UIAlertActionStyleDefault handler: ^(UIAlertAction *action){
- [self startNewRound];
- }];
- [alertCtr addAction:alertAction];
- [self presentViewController:alertCtr animated:YES completion:nil];
-
- }
-
- // 滑块值改变的事件
- - (IBAction)slideMove:(id)sender {
- UISlider *slider = (UISlider *)sender;
- _curVal = (int)lroundf(slider.value);
- }
-
- // 重新来过按钮点击事件
- - (IBAction)redo:(id)sender {
- _targetVal = 1 + arc4random() % 100;
- _curVal = 50;
- _score = 0;
- _count = 0;
- _slider.value = _curVal;
- _scoreLabel.text = [NSString stringWithFormat:@"%d", _score];
- _countLabel.text = [NSString stringWithFormat:@"%d", _count];
- }
-
- // 显示信息事件
- - (IBAction)showInfo:(id)sender {
- AboutViewController *controller = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
- controller.modalPresentationStyle = UIModalTransitionStyleFlipHorizontal;
- [self presentViewController:controller animated:YES completion:nil];
- }
- @end
-
AboutViewController.m
- #import "AboutViewController.h"
- @interface AboutViewController ()
- @end
- @implementation AboutViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (IBAction)close:(id)sender {
- [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
- }
- @end
项目下载地址:点我下载
结束语
技术的道路,漫漫其修远兮。学习一门新的技术,总是伴随着各种并发症。但只要熬过前期,并坚持在中期,相信,后期一定是辉煌的。
-
使用滑块的值减去随机数,取结果的绝对值,绝对值加1就是最终结果。 ↩