前言:最近公司部门在组织团建,须要准备两个团建小游戏, 分别是“数字速算升级版”和“你话我猜升级版”。git
小编琢磨了一下,发现这个两个小项目很适合iOS入门学习,故这篇文章诞生了。 本篇将介绍iOS 小游戏项目——你话我猜升级版。 但愿经过这篇文章,帮助对iOS感兴趣的同窗快速入门iOS。github
咱们先来看看效果图:算法
图解: 数组
逻辑层面:bash
+1
。300
秒,时间到0
时,结束游戏,对/错按钮禁止点击。词库搭建:微信
5
个等级,等级越高,抽到的几率越小。1. UI层面:dom
项目中,我选择的storyboard。独立开发时,使用storyboard比较高效。工具
@property (weak, nonatomic) IBOutlet UILabel *wordLabel;//!< 猜题Label
@property (weak, nonatomic) IBOutlet UILabel *secondsLabel;//!< 计时Label
@property (weak, nonatomic) IBOutlet UILabel *correctLabel;//!< 成功计数Label
@property (weak, nonatomic) IBOutlet UILabel *wrongLabel;//!< 失败计数Label
@property (weak, nonatomic) IBOutlet UIButton *correctButton;//!< 成功按钮
@property (weak, nonatomic) IBOutlet UIButton *wrongButton;//!< 失败按钮
@property (weak, nonatomic) IBOutlet UIButton *startButton;//!< 开始按钮
复制代码
2. 业务逻辑:学习
@property (nonatomic, assign) NSUInteger seconds;//!< 剩余时间
@property (nonatomic, assign) NSInteger correctCount;//!< 答对题数
@property (nonatomic, assign) NSInteger wrongCount;//!< 答错题数
@property (nonatomic, strong) QiGuessWords *guessWords;//!< 词条(题目)
@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.correctButton.enabled = !self.correctButton.enabled;
self.wrongButton.enabled = !self.wrongButton.enabled;
if (sender.selected) {
self.wordLabel.text = self.guessWords.randomWord;
[self startTimer];
} else {
[self resetElements];
}
}];
[alertController addAction:cancelAction];
[alertController addAction:confirmAction];
[self.navigationController presentViewController:alertController animated:YES completion:nil];
}
复制代码
//! 成功按钮点击事件
- (IBAction)correctButtonClicked:(id)sender {
_correctLabel.text = [NSString stringWithFormat:@"%li",(long)++_correctCount];
_wordLabel.text = _guessWords.randomWord;
}
//! 失败按钮点击事件
- (IBAction)wrongButtonClicked:(id)sender {
_wrongLabel.text = [NSString stringWithFormat:@"%li",(long)++_wrongCount];
_wordLabel.text = _guessWords.randomWord;
}
复制代码
#pragma mark - Private functions
- (void)startTimer {
[self stopTimer];
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
}
- (void)stopTimer {
[_timer invalidate];
_timer = nil;
}
- (void)countDown {
_secondsLabel.text = [NSString stringWithFormat:@"%li", (long)--_seconds];
if (_seconds <= 0) {
[self stopTimer];
_correctButton.enabled = NO;
_wrongButton.enabled = NO;
}
else if (_seconds < 30) {
_secondsLabel.textColor = [UIColor redColor];
}
}
复制代码
- (void)resetElements {
_wordLabel.text = @"";
_seconds = 300;
_wrongCount = 0;
_correctCount = 0;
_secondsLabel.text = [NSString stringWithFormat:@"%li", (long)_seconds];
_correctLabel.text = [NSString stringWithFormat:@"%li", (long)_correctCount];
_wrongLabel.text = [NSString stringWithFormat:@"%li", (long)_wrongCount];
_correctButton.enabled = NO;
_wrongButton.enabled = NO;
_startButton.enabled = YES;
[self stopTimer];
}
复制代码
词库的难点在于:去重、分级、按几率抽题。
在QiGuessWords.h
中,
typedef NS_ENUM(NSUInteger, QiGuessWordsType) {
QiGuessWordsTypePrimary,
QiGuessWordsTypeMiddle,
QiGuessWordsTypeSenior,
QiGuessWordsTypeComplex,
QiGuessWordsTypeCustom
};
复制代码
@property (nonatomic, copy) NSString *randomWord;
- (NSArray<NSString *> *)randomWordsWithType:(QiGuessWordsType)type count:(NSUInteger)count;
复制代码
在QiGuessWords.m
中,
- (instancetype)init {
self = [super init];
if (self) {
NSString *primaryWords = @"螃蟹,口红...";
NSString *middleWords = @"班主任,放风筝...";
NSString *seniorWords = @"乘人之危,七上八下...";
NSString *complexWords = @"低头思故乡,到处闻啼鸟...";
NSString *customWords = @"TCP,360杀毒...";
_primaryWords = [primaryWords componentsSeparatedByString:@","].mutableCopy;
_middleWords = [middleWords componentsSeparatedByString:@","].mutableCopy;
_seniorWords = [seniorWords componentsSeparatedByString:@","].mutableCopy;
_complexWords = [complexWords componentsSeparatedByString:@","].mutableCopy;
_customWords = [customWords componentsSeparatedByString:@","].mutableCopy;
_allWords = @[_primaryWords, _middleWords, _seniorWords, _complexWords, _customWords];
}
return self;
}
复制代码
思想:系统算出一个0~9的随机数,
随机数 | 词条类型 | 几率 |
---|---|---|
0,1 | primaryWords(初级) | 20% |
2,3 | middleWords(中等) | 20% |
4,5 | seniorWords(高级) | 20% |
6 | complexWords(复杂) | 10% |
7,8,9 | customWords(自定义) | 30% |
#pragma mark - Getters
- (NSString *)randomWord {
NSUInteger r = arc4random() % 10;
NSUInteger i = r < 2? 0: r < 4? 1: r < 6? 2: r < 7? 3: 4;
NSMutableArray<NSString *> *words = _allWords[i];
if (words.count == 0) {
return self.randomWord;
} //!< 全部数据取完后会形成死循环
NSUInteger index = arc4random() % words.count;
NSString *randomWord = words[index];
[words removeObject:randomWord];
return randomWord;
}
复制代码
最后,游戏工程源码:游戏源码
关注咱们的途径有:
QiShare(简书)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公众号)