Frogger - Adding Score Logic

From Arduboy Wiki
Revision as of 11:06, 17 June 2024 by Filmote (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Tutorial Instructions

This tutorial is designed to be completed in order and builds on the code from the previous sections. However, you can jump to any section and download the completed code to that point and continue from that section onwards.

The tutorial utilises two files which can be downloaded using the following links. To download the files, right click on the filename and select 'Save Link As …'

Throughout the tutorials, you will be prompted to complete sections of code utilising the knowledge you have just gained. If you are unable to complete the code you can simply download the starting code presented at the start of the next section as it will contain a valid solution.

Other sections in this tutorial:


Adding Score Logic

What is a game without a score?

In this section, we will add some simple logic to add scores as the player progresses up the screen and reaches the home positions. A bonus will also be added as the player fills all the home positions.

Search for the variable score in the code and note where it is declared, where the value is increased and where the score is reset. By now, this should all be self-explanatory.

You will also discover a block of code that renders the score to the screen. Due to the lack of real-estate on the screen to use the Arduboy libraries’ built-in print() function, I have used another trick – an image with multiple frames that represent the digits zero to nine in a small 3x5 pixel font.

In true video-game fashion, I wanted my scores to have leading zeroes so I have employed a little trick to determine what each digit of the score should be. As you can see below, each digit of the score is progressively evaluated by dividing it (initially) by 10,000 and rendering the result. Any value greater than 10,000 is then removed from the score and the process repeated for 1,000 then 100 and so on.

if (player.y < 59 || player.x > 47) {

    Sprites::drawOverwrite(0, 59, ScoreImg, 0);

    uint16_t scoreTmp = score;
    Sprites::drawOverwrite(25, 59, Numbers, scoreTmp / 10000);      scoreTmp = scoreTmp - ((scoreTmp / 10000) * 10000);
    Sprites::drawOverwrite(29, 59, Numbers, scoreTmp / 1000);       scoreTmp = scoreTmp - ((scoreTmp / 1000) * 1000);
    Sprites::drawOverwrite(33, 59, Numbers, scoreTmp / 100);        scoreTmp = scoreTmp - ((scoreTmp / 100) * 100);
    Sprites::drawOverwrite(33, 59, Numbers, scoreTmp % 100);        

}


Your Turn

Add the two remaining lines that handle the tens and units column of the score.


What does the test for the player’s location have to do with anything?

It allows the score to be hidden when the player is at the bottom of the screen and moves to the far left. Without this, the frog image would be rendered on top of the score which is a bit ugly!

In the next section, we will wrap the code up with a game over banner and a title screen.



Prev adding home logic, Next adding a title and game over screen