iOS 小游戲項目——你話我猜升級版

級別: ★☆☆☆☆
標(biāo)簽:「iOS」「小游戲項目」「你話我猜」
作者: MrLiuQ
審校: QiShare團隊

前言:最近公司部門在組織團建,需要準(zhǔn)備兩個團建小游戲:
分別是“數(shù)字速算升級版”和“你話我猜升級版”。

小編琢磨了一下,發(fā)現(xiàn)這個兩個小項目很適合iOS入門學(xué)習(xí),故這篇文章誕生了。
本篇將介紹iOS 小游戲項目——你話我猜升級版。
希望通過這篇文章,幫助對iOS感興趣的同學(xué)快速入門iOS。

我們先來看看效果圖:

效果圖

一、項目需求:

  1. UI層面:
    比較基礎(chǔ),上面三個Label顯示數(shù)據(jù)(分別是:錯誤數(shù)、倒計時、正確數(shù)),中間的一個大Label顯示所猜詞條,下面三個Button分別對應(yīng)(答錯、開始/復(fù)位、答對)。

圖解:


UI
  1. 邏輯層面:

    • 點擊對/錯按鈕,對應(yīng)的Label的數(shù)字要+1。
    • 做一個計時器,游戲時長定為300秒,時間到0時,結(jié)束游戲,對/錯按鈕禁止點擊。
    • 點擊開始、對/錯按鈕,中間的詞條都需要更新。
    • 點擊開始按鈕,出現(xiàn)一個彈窗,點擊確定,開始倒計時。
  2. 詞庫搭建:

    • 詞庫難度分為5個等級,等級越高,抽到的概率越小。
    • 詞庫去重處理,抽到的詞條不再顯示。
    • 概率算法。

二、實現(xiàn)思路:

1. UI層面:

  • 方式一:storyboard(拖控件、加約束)。
  • 方式二:純代碼。

項目中,我選擇的storyboard。獨立開發(fā)時,使用storyboard比較高效。

@property (weak, nonatomic) IBOutlet UILabel *wordLabel;//!< 猜題Label
@property (weak, nonatomic) IBOutlet UILabel *secondsLabel;//!< 計時Label
@property (weak, nonatomic) IBOutlet UILabel *correctLabel;//!< 成功計數(shù)Label
@property (weak, nonatomic) IBOutlet UILabel *wrongLabel;//!< 失敗計數(shù)Label
@property (weak, nonatomic) IBOutlet UIButton *correctButton;//!< 成功按鈕
@property (weak, nonatomic) IBOutlet UIButton *wrongButton;//!< 失敗按鈕
@property (weak, nonatomic) IBOutlet UIButton *startButton;//!< 開始按鈕

2. 業(yè)務(wù)邏輯:

  • 所要保存的屬性:
@property (nonatomic, assign) NSUInteger seconds;//!< 剩余時間
@property (nonatomic, assign) NSInteger correctCount;//!< 答對題數(shù)
@property (nonatomic, assign) NSInteger wrongCount;//!< 答錯題數(shù)

@property (nonatomic, strong) QiGuessWords *guessWords;//!< 詞條(題目)
@property (nonatomic, strong) NSTimer *timer;//!< 計時器
  • 開始按鈕業(yè)務(wù)邏輯:
//! 開始按鈕點擊事件
- (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];
}
  • 成功/失敗按鈕業(yè)務(wù)邏輯:
//! 成功按鈕點擊事件
- (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;
}
  • 定時器相關(guān)代碼:
#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中,

  1. 先定義一個枚舉:表示題的難度系數(shù)。
typedef NS_ENUM(NSUInteger, QiGuessWordsType) {
    QiGuessWordsTypePrimary,
    QiGuessWordsTypeMiddle,
    QiGuessWordsTypeSenior,
    QiGuessWordsTypeComplex,
    QiGuessWordsTypeCustom
};
  1. 暴露一個屬性,直接出隨機詞條。并暴露了一個方法,直接返回一個指定“難度”、“數(shù)量”的隨機的詞條數(shù)組。
@property (nonatomic, copy) NSString *randomWord;

- (NSArray<NSString *> *)randomWordsWithType:(QiGuessWordsType)type count:(NSUInteger)count;

QiGuessWords.m中,

  1. init方法:
- (instancetype)init {
    
    self = [super init];
    
    if (self) {
        
        NSString *primaryWords = @"螃蟹,口紅...";
        NSString *middleWords = @"班主任,放風(fēng)箏...";
        NSString *seniorWords = @"落井下石,七上八下...";
        NSString *complexWords = @"低頭思故鄉(xiāng),處處聞啼鳥...";
        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;
}
  1. Getter方法:
    注意這里三元表達式的運用。

思想:系統(tǒng)算出一個0~9的隨機數(shù),

隨機數(shù) 詞條類型 概率
0,1 primaryWords(初級) 20%
2,3 middleWords(中等) 20%
4,5 seniorWords(高級) 20%
6 complexWords(復(fù)雜) 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;
    } //!< 所有數(shù)據(jù)取完后會造成死循環(huán)
    
    NSUInteger index = arc4random() % words.count;
    NSString *randomWord = words[index];
    [words removeObject:randomWord];
    
    return randomWord;
}

最后,游戲工程源碼:游戲源碼


了解更多iOS及相關(guān)新技術(shù),請關(guān)注我們的公眾號:

關(guān)注我們的途徑有:
QiShare(簡書)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公眾號)

推薦文章:
iOS 文件操作簡介
iOS 關(guān)鍵幀動畫
iOS 編寫高質(zhì)量Objective-C代碼(七)
奇舞周刊

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,204評論 3 119
  • 在成為一個更好的人之前,你可能先要成為一個更壞的人。同樣,在保持長久的親密之前,你可能先要學(xué)會保持距離。 可是,很...
    小娜迦閱讀 325評論 0 0
  • 記錄近期繪畫練習(xí),時間不足,都沒有做深入刻畫。
    小魚素素閱讀 200評論 0 2
  • 書籍信息 書名:《外婆的道歉信》閱讀時間:2017年6月書籍類型:暢銷小說作者:【瑞典】弗雷德里克·巴克曼 不知道...
    愛讀書的斷斷閱讀 2,760評論 1 6
  • PS基礎(chǔ) 快捷鍵: 標(biāo)尺: 選擇-移動: 某一個地方的選中,移動 切片:
    時修七年閱讀 284評論 0 0

友情鏈接更多精彩內(nèi)容