前言:最近公司部门在组织团建,需要准备准备两个团建小游戏,
分别是“数字速算升级版”和“你话我猜升级版”。
小编琢磨了一下,发现这个两个小项目很适合入门iOS,故这篇文章诞生了。
本篇将介绍iOS 小游戏项目——数字速算升级版。
希望通过这篇文章,能够帮助对iOS感兴趣的同学快速入门iOS。
效果图如下:
效果图一、项目需求
- UI层面:
8个Label,3个Button。
图解:
UI
-
逻辑层面:
- 点击出题/开始按钮,随机生成三个数(两位数及以下)和两个运算符(加、减、乘)。
- 做一个计时器,从0开始计时,直到游戏结束,查看游戏时长。
- 点击出题,刷新题目。
- 点击结果,计算出结果。
- 点击开始按钮,出现一个弹窗,点击确定,开始计时。
-
难题概率:
- 个位数出现的概率比两位数高。
- 乘法出现的概率比加减法低。
二、实现思路
- UI层面:
- 方式一:storyboard(拖控件、加约束)
- 方式二:纯代码
项目中,我选择的storyboard。独立开发时,用storyboard比较高效。
日常还是多用纯代码比较好,毕竟代码是万能的~
@property (weak, nonatomic) IBOutlet UILabel *factorLabel1;
@property (weak, nonatomic) IBOutlet UILabel *factorLabel2;
@property (weak, nonatomic) IBOutlet UILabel *factorLabel3;
@property (weak, nonatomic) IBOutlet UILabel *operatorLabel1;
@property (weak, nonatomic) IBOutlet UILabel *operatorLabel2;
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;
@property (weak, nonatomic) IBOutlet UILabel *recordingLabel;
@property (weak, nonatomic) IBOutlet UIButton *questionButton;
@property (weak, nonatomic) IBOutlet UIButton *resultButton;
@property (weak, nonatomic) IBOutlet UIButton *startButton;
- 业务逻辑:
- 所要保存的属性:
@property (nonatomic, strong) NSTimer *timer;//!< 计时器
- 开始按钮业务逻辑:
- (IBAction)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];
}
- 出题按钮业务逻辑:
- (IBAction)questionButtonClicked:(id)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;
}];
}
}
- 结果按钮业务逻辑:
- (IBAction)resultButtonClicked:(id)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;
}
}
}
出题业务逻辑:
- (void)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 @(factor).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;
}
- 重置元素逻辑:
- (void)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;
}
- 定时器业务逻辑:
- (void)startTimer {
[self stopTimer];
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
}
- (void)stopTimer {
[_timer invalidate];
_timer = nil;
}
- (void)countUp {
NSInteger count = _recordingLabel.text.integerValue;
_recordingLabel.text = @(++count).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 @(factor).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;
}
最后,工程源码:游戏源码
网友评论