美高梅官方网站3045-mgm6608美高梅app下载
iOS 小游戏项目——数字速算升级版

iOS 小游戏项目——数字速算升级版

作者:mgm6608美高梅app下载    来源:未知    发布时间:2020-01-19 08:40    浏览量:

等第: ★☆☆☆☆标签:「iOS」「小游戏项目」「数字快速计算」作者: MrLiuQ审阅核对: QiShare团队

序言:近日商家机关在集体团建,供给预备筹算五个团建小游戏,分别是“数字快速总结晋级版”和“你话作者猜进级版”。

作者研讨了风华正茂晃,发掘那个多个小品种很适合入门iOS,故那篇随笔诞生了。本篇将介绍 iOS 小游戏项目——数字快速计算晋级版。希望通过那篇文章,能够支持对iOS感兴趣的同桌火速入门iOS。

效用图如下:

图片 1效果图

生机勃勃、项目要求

  1. UI层面:8个Label,3个Button。

图解:

图片 2UI

  1. 逻辑层面:

    • 点击出题/初始开关,随机生成多个数和四个运算符。
    • 做二个电火花计时器,从0起来计时,直到游戏截至,查看游戏时间长度。
    • 点击出题,刷新标题。
    • 点击结果,总结出结果。
    • 点击带头按键,现身一个弹窗,点击明确,起始计时。
  2. 难题可能率:

    • 个位数现身的票房价值比两位数高。
    • 乘法现身的票房价值比加减法低。

二、达成思路

  1. UI层面:
  • 方式一:storyboard。
  • 方式二:纯代码。

种类中,笔者接受的storyboard。独立开垦时,用storyboard相比飞速。

@property (weak, nonatomic) IBOutlet UILabel *factorLabel1;//!< 数字Label1@property (weak, nonatomic) IBOutlet UILabel *factorLabel2;//!< 数字Label2@property (weak, nonatomic) IBOutlet UILabel *factorLabel3;//!< 数字Label3@property (weak, nonatomic) IBOutlet UILabel *operatorLabel1;//!< 运算符Label1@property (weak, nonatomic) IBOutlet UILabel *operatorLabel2;//!< 运算符Label2@property (weak, nonatomic) IBOutlet UILabel *resultLabel;//!< 结果Label@property (weak, nonatomic) IBOutlet UILabel *recordingLabel;//!< 计时Label@property (weak, nonatomic) IBOutlet UIButton *questionButton;//!< 出题Button@property (weak, nonatomic) IBOutlet UIButton *resultButton;//!< 结果Button@property (weak, nonatomic) IBOutlet UIButton *startButton;//!< 开始Button
  1. 事情逻辑:
  • 所要保存的品质:
@property (nonatomic, strong) NSTimer *timer;//!< 计时器
  • 最早按键业务逻辑:
- startButtonClicked:(UIButton *)sender { NSString *message = [NSString stringWithFormat:@"确定要 %@ 吗?", sender.currentTitle]; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:sender.currentTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { sender.selected = !sender.selected; self.resultButton.enabled = !self.resultButton.enabled; if (sender.selected) { [self resetElements]; [self startTimer]; } else { [self stopTimer]; } }]; [alertController addAction:cancelAction]; [alertController addAction:confirmAction]; [self.navigationController presentViewController:alertController animated:YES completion:nil];}
  • 出题开关业务逻辑:
- questionButtonClicked:sender { _questionButton.enabled = NO; _resultButton.enabled = YES; [self setQuestion]; if (_speechManager) { _recordingLabel.text = @""; _recordingLabel.layer.borderWidth = .0; [_speechManager startRecordingWithResponse:^(NSString * _Nonnull formatString) { self.recordingLabel.text = [formatString componentsSeparatedByString:@" "].lastObject; }]; }}
  • 结果开关业务逻辑:
- resultButtonClicked:sender { _questionButton.enabled = YES; _resultButton.enabled = NO; _resultLabel.text = @([self calculate]).stringValue; if (_speechManager) { [_speechManager stopRecording]; _recordingLabel.layer.borderWidth = 1.0; if ([_recordingLabel.text isEqualToString:_resultLabel.text]) { _recordingLabel.layer.borderColor = [UIColor greenColor].CGColor; } else { _recordingLabel.layer.borderColor = [UIColor redColor].CGColor; } }}
  • 出题业务逻辑:
- setQuestion { _resultLabel.text = @""; _factorLabel1.text = [self generateFactor]; _factorLabel2.text = [self generateFactor]; _factorLabel3.text = [self generateFactor]; _operatorLabel1.text = [self generateOperator]; _operatorLabel2.text = [self generateOperator];}//! 生成数字- (NSString *)generateFactor { NSUInteger r = arc4random() % 10; NSUInteger max = r < 4? 10: r < 7? 20: r < 9? 50: 100; NSUInteger factor = arc4random() % max; return @.stringValue;}//! 生成运算符- (NSString *)generateOperator { NSUInteger r = arc4random() % 5; NSString *operator = r < 2? @"+": r < 4? @"-": @"×"; return operator;}
  • 估测计算办法业务逻辑:
- (NSInteger)calculate { NSUInteger factor1 = _factorLabel1.text.integerValue; NSUInteger factor2 = _factorLabel2.text.integerValue; NSUInteger factor3 = _factorLabel3.text.integerValue; NSString *operator1 = _operatorLabel1.text; NSString *operator2 = _operatorLabel2.text; NSInteger result = [self calculateWithOperator:operator1 leftFactor:factor1 rightFactor:factor2]; if ([operator2 isEqualToString:@"×"]) { result = [self calculateWithOperator:operator2 leftFactor:factor2 rightFactor:factor3]; result = [self calculateWithOperator:operator1 leftFactor:factor1 rightFactor:result]; } else { result = [self calculateWithOperator:operator2 leftFactor:result rightFactor:factor3]; } return result;}- (NSUInteger)calculateWithOperator:(NSString *)operator leftFactor:(NSUInteger)leftFactor rightFactor:(NSUInteger)rightFactor { NSInteger result = leftFactor; if ([operator isEqualToString:@"+"]) { result += rightFactor; } else if ([operator isEqualToString:@"-"]) { result -= rightFactor; } else { result *= rightFactor; } return result;}
  • 重新恢复设置成分逻辑:
- resetElements { _factorLabel1.text = @"0"; _factorLabel2.text = @"0"; _factorLabel3.text = @"0"; _operatorLabel1.text = @"+"; _operatorLabel2.text = @"+"; _resultLabel.text = @"0"; _recordingLabel.text = @"0"; _questionButton.enabled = YES; _resultButton.enabled = YES;}
  • 放大计时器业务逻辑:
- startTimer { [self stopTimer]; _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector userInfo:nil repeats:YES];}- stopTimer { [_timer invalidate]; _timer = nil;}- countUp { NSInteger count = _recordingLabel.text.integerValue; _recordingLabel.text = @.stringValue;}
  • 数字生成可能率算法:
随机数 数字范围 概率
0,1,2,3 0~9 40%
4,5,6 0~20 30%
7,8 0~50 20%
9 0~100 10%
//! 生成数字- (NSString *)generateFactor { NSUInteger r = arc4random() % 10; NSUInteger max = r < 4? 10: r < 7? 20: r < 9? 50: 100; NSUInteger factor = arc4random() % max; return @.stringValue;}
  • 运算符生成概率算法:
随机数 运算符 概率
0,1 + 40%
2,3 - 40%
4 x 20%
//! 生成运算符- (NSString *)generateOperator { NSUInteger r = arc4random() % 5; NSString *operator = r < 2? @"+": r < 4? @"-": @"×"; return operator;}
  • 总付钱法:
- (NSInteger)calculate { NSUInteger factor1 = _factorLabel1.text.integerValue; NSUInteger factor2 = _factorLabel2.text.integerValue; NSUInteger factor3 = _factorLabel3.text.integerValue; NSString *operator1 = _operatorLabel1.text; NSString *operator2 = _operatorLabel2.text; NSInteger result = [self calculateWithOperator:operator1 leftFactor:factor1 rightFactor:factor2]; if ([operator2 isEqualToString:@"×"]) { result = [self calculateWithOperator:operator2 leftFactor:factor2 rightFactor:factor3]; result = [self calculateWithOperator:operator1 leftFactor:factor1 rightFactor:result]; } else { result = [self calculateWithOperator:operator2 leftFactor:result rightFactor:factor3]; } return result;}- (NSUInteger)calculateWithOperator:(NSString *)operator leftFactor:(NSUInteger)leftFactor rightFactor:(NSUInteger)rightFactor { NSInteger result = leftFactor; if ([operator isEqualToString:@"+"]) { result += rightFactor; } else if ([operator isEqualToString:@"-"]) { result -= rightFactor; } else { result *= rightFactor; } return result;}

最后,工程源码:游戏源码

关爱我们的门路有:QiShareQiShareQiShareQiShareQiShare(CocoaChinaState of QatarQiShare(StackOverflowState of QatarQiShare

推介作品:iOS 绘制渐变·根底篇iOS 绘制渐变·实例篇iOS 编写高素质Objective-C代码奇舞周刊

下一篇:没有了
友情链接: 网站地图
Copyright © 2015-2019 http://www.zen-40.com. mgm美高梅有限公司 版权所有