[QiShare team] [MrLiuQ, QiShare team] [QiShare team

Preface: the company department is organizing group to build recently, need to prepare two group to build small games, respectively is “digital quick calculation upgrade version” and “your words I guess upgrade version”.

After a bit of thinking, I found that these two small projects are very suitable for iOS learning, so this article was born. This article introduces the iOS mini game project — an updated version if you like. I hope this article can help students who are interested in iOS get a quick start on iOS.

Let’s take a look at the renderings first:

I. Project Requirements:

  1. UI level: Compare the basic, the top three labels display data (respectively: error number, countdown, correct number), a big Label in the middle shows the guess entry, the following three buttons corresponding to (wrong answer, start/reset, correct answer).

Illustration:

  1. Logical level:

    • Click the right/Wrong button, the corresponding Label number to+ 1.
    • Set a timer for the game300Seconds, time is up0“, end the game, the right/wrong button is forbidden to click.
    • Click the Start, right/Wrong buttons, and the entries in the middle need to be updated.
    • Click the Start button, a popup window appears, click OK, start the countdown.
  2. Thesaurus construction:

    • Thesaurus difficulty is divided into5Grade. The higher the grade, the lower the probability.
    • The thesaurus is reprocessed and the selected term is no longer displayed.
    • Probabilistic algorithms.

Two, implementation ideas:

1. UI level:

  • Method 1: Storyboard (drag controls, add constraints).
  • Method two: Pure code.

In the project, I chose the storyboard. Using storyboards is more efficient when developing independently.

@property (weak, nonatomic) IBOutlet UILabel *wordLabel; / /! < 小 题 label@property (weak, nonatomic) IBOutlet UILabel *secondsLabel; / /! < timer Label @property (weak, nonatomic) IBOutlet UILabel *correctLabel; / /! < success count label@property (weak, nonatomic) IBOutlet UILabel *wrongLabel; / /! < failure count Label @property (weak, nonatomic) IBOutlet UIButton *correctButton; / /! < success button @property (weak, nonatomic) IBOutlet UIButton *wrongButton; / /! @property (weak, nonatomic) IBOutlet UIButton *startButton; / /! < Start buttonCopy the code

2. Business logic:

  • Attributes to save:
@property (nonatomic, assign) NSUInteger seconds; / /! < remaining time @property (nonatomic, assign) NSInteger correctCount; / /! @property (Nonatomic, assign) NSInteger wrongCount; / /! < property (nonatomic, strong) QiGuessWords *guessWords; / /! < entry (title) @property (nonatomic, strong) NSTimer *timer; / /! The < timerCopy the code
  • Start button business logic:
/ /! - (IBAction)startButtonClicked (UIButton *) Sender {NSString *message = [NSString stringWithFormat:@" Do you want %@? ", sender.currentTitle]; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction * cancelAction = [UIAlertAction actionWithTitle: @ "cancel" 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]; }Copy the code
  • Success/failure button business logic:
/ /! - (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; }Copy the code
  • Timer related code:
#pragma mark - Private functions - (void)startTimer { [self stopTimer]; _timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @ the selector (countDown) the 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]; }}Copy the code
  • Reset element logic:
- (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];
}
Copy the code

3. Difficulties: Thesaurus tools

The difficulty of thesaurus lies in: reweighting, grading and selecting questions according to probability.

In QiGuessWords. H,

  1. Let’s define an enumeration: the difficulty factor of the question
typedef NS_ENUM(NSUInteger, QiGuessWordsType) {
    QiGuessWordsTypePrimary,
    QiGuessWordsTypeMiddle,
    QiGuessWordsTypeSenior,
    QiGuessWordsTypeComplex,
    QiGuessWordsTypeCustom
};
Copy the code
  1. Expose an attribute and play a random entry. And exposed a method that directly returns a random array of terms specifying “difficulty” and “quantity”.
@property (nonatomic, copy) NSString *randomWord;

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

In QiGuessWords. J m,

  1. The init method
- (instancetype)init {
    
    self = [super init];
    
    if (self) {
        
        NSString *primaryWords = @"Crab, lipstick...";
        NSString *middleWords = @"Teacher in charge, fly a kite...";
        NSString *seniorWords = @"Bring it down, bring it down...";
        NSString *complexWords = @"I bow my head to think of home, and hear birds everywhere...";
        NSString *customWords = @"TCP, 360 antivirus...";
        
        _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;
}
Copy the code
  1. Getter methods: Notice the use of ternary expressions here.

Idea: The system calculates a random number from 0 to 9,

The random number Entry type The probability of
0, 1 PrimaryWords (Primary) 20%
2, 3 MiddleWords (medium) 20%
4, 5 SeniorWords (Advanced) 20%
6 complexWords 10%
7,8,9 CustomWords (Custom) 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; }Copy the code

Finally, game engineering source code: game source code


QiShare(Simple book) QiShare(digging gold) QiShare(Zhihu) QiShare(GitHub) QiShare(CocoaChina) QiShare(StackOverflow) QiShare(wechat public account)

IOS Keyframe animation iOS writing high quality Objective-C code (seven)