内容简介:级别: ★☆☆☆☆标签:「iOS」「小游戏项目」「你话我猜」作者:MrLiuQ
级别: ★☆☆☆☆
标签:「iOS」「小游戏项目」「你话我猜」
作者:MrLiuQ
审校:QiShare团队
前言:最近公司部门在组织团建,需要准备两个团建小游戏, 分别是“数字速算升级版”和“你话我猜升级版”。
小编琢磨了一下,发现这个两个小项目很适合iOS入门学习,故这篇文章诞生了。 本篇将介绍iOS 小游戏项目——你话我猜升级版。 希望通过这篇文章,帮助对iOS感兴趣的同学快速入门iOS。
我们先来看看效果图:
一、项目需求:
- UI层面: 比较基础,上面三个Label显示数据(分别是:错误数、倒计时、正确数),中间的一个大Label显示所猜词条,下面三个Button分别对应(答错、开始/复位、答对)。
图解:
-
逻辑层面:
- 点击对/错按钮,对应的Label的数字要
+1。 - 做一个计时器,游戏时长定为
300秒,时间到0时,结束游戏,对/错按钮禁止点击。 - 点击开始、对/错按钮,中间的词条都需要更新。
- 点击开始按钮,出现一个弹窗,点击确定,开始倒计时。
- 点击对/错按钮,对应的Label的数字要
-
词库搭建:
二、实现思路:
1. UI层面:
- 方式一:storyboard(拖控件、加约束)。
- 方式二:纯代码。
项目中,我选择的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 中,
- init方法
- (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;
}
复制代码
- Getter方法: 注意这里三元表达式的运用。
思想:系统算出一个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(微信公众号)
以上所述就是小编给大家介绍的《iOS 小游戏项目——你话我猜升级版》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- bms 前后端分离升级版
- ECharts 打造在线个人简历 【升级版】
- ViewPager2:官方Viewpager升级版来临
- kkFileView 文件预览 v3.5 性能升级版发布
- 程序员为什么要持续学习(升级版)
- 郭明錤:2019年iPhone将引入升级版Face ID
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
首席产品官2 从白领到金领
车马 / 机械工业出版社 / 79元
《首席产品官》共2册,旨在为产品新人成长为产品行家,产品白领成长为产品金领,最后成长为首席产品官(CPO)提供产品认知、能力体系、成长方法三个维度的全方位指导。 作者在互联网领域从业近20年,是中国早期的互联网产品经理,曾是周鸿祎旗下“3721”的产品经理,担任CPO和CEO多年。作者将自己多年来的产品经验体系化,锤炼出了“产品人的能力杠铃模型”(简称“杠铃模型”),简洁、直观、兼容性好、实......一起来看看 《首席产品官2 从白领到金领》 这本书的介绍吧!