Difference between revisions of "Development/Tutorials/Games/Highscores"

Jump to: navigation, search
(Fix constructor. Add image.)
m (Text replace - "<code cppqt n>" to "<syntaxhighlight lang="cpp-qt" line>")
 
(13 intermediate revisions by 4 users not shown)
Line 5: Line 5:
 
name=High Scores|
 
name=High Scores|
  
pre=[[Development/Tutorials/Programming_Tutorial_KDE_4|Introduction to KDE4 programming]], [[Projects/Games/KStandardGameAction|KStandardGameAction Tutorial]]|
+
pre=[[Development/Tutorials/Programming_Tutorial_KDE_4|Introduction to KDE4 programming]], [[Development/Tutorials/Games/KStandardGameAction|KStandardGameAction Tutorial]]|
  
 
next=|  
 
next=|  
Line 25: Line 25:
 
==Displaying Scores==
 
==Displaying Scores==
 
To display the high score table, it's as simple as two lines of code.  
 
To display the high score table, it's as simple as two lines of code.  
<code cppqt n>
+
<syntaxhighlight lang="cpp-qt" line>
 
#include <KScoreDialog>
 
#include <KScoreDialog>
  
Line 33: Line 33:
 
   ksdialog.exec();
 
   ksdialog.exec();
 
}
 
}
</code>
+
</syntaxhighlight>
 
This will display the highscore table showing, by default, the 'Name' and 'Score' columns.
 
This will display the highscore table showing, by default, the 'Name' and 'Score' columns.
  
In order to comply with the kdegames standards, you should use a {{class|KStandardGameAction}} to launch this function. This will set up the icons and toolbars correctly. See the [[Projects/Games/KStandardGameAction|KStandardGameAction tutorial]] for more information on this.
+
In order to comply with the kdegames standards, you should use a {{class|KStandardGameAction}} to launch this function. This will set up the icons and toolbars correctly. See the [[Development/Tutorials/Games/KStandardGameAction|KStandardGameAction tutorial]] for more information on this.
<code cppqt n>
+
<syntaxhighlight lang="cpp-qt" line>
 
#include <KStandardGameAction>
 
#include <KStandardGameAction>
 
void setupActions()
 
void setupActions()
Line 46: Line 46:
 
   setupGUI();
 
   setupGUI();
 
}
 
}
</code>
+
</syntaxhighlight>
  
 
==Adding Scores==
 
==Adding Scores==
 
===Simplest way to add a score===
 
===Simplest way to add a score===
 
The simplest way to add a new score to the table is as follows:
 
The simplest way to add a new score to the table is as follows:
<code cppqt n>
+
<syntaxhighlight lang="cpp-qt" line>
 
#include <KScoreDialog>
 
#include <KScoreDialog>
  
Line 60: Line 60:
 
   ksdialog.exec();
 
   ksdialog.exec();
 
}
 
}
</code>
+
</syntaxhighlight>
 
This will add the score <tt>playersScore</tt> to the high score table and then launch the dialog. If it is the first score to be entered on the table, a blank line-edit will be provided for the player to enter their name, otherwise the line-edit will still be provided but the player's name will be automatically filled in.
 
This will add the score <tt>playersScore</tt> to the high score table and then launch the dialog. If it is the first score to be entered on the table, a blank line-edit will be provided for the player to enter their name, otherwise the line-edit will still be provided but the player's name will be automatically filled in.
  
 
However, you will notice that the dialog is shown every time, even if the player didn't manage to get onto the table. If you only want to show the table if the player achieved a high score, place an <tt>if()</tt> command around the <tt>KScoreDialog::addScore()</tt> call:
 
However, you will notice that the dialog is shown every time, even if the player didn't manage to get onto the table. If you only want to show the table if the player achieved a high score, place an <tt>if()</tt> command around the <tt>KScoreDialog::addScore()</tt> call:
<code cppqt>
+
<syntaxhighlight lang="cpp-qt">
 
if(ksdialog.addScore(playersScore))
 
if(ksdialog.addScore(playersScore))
 
   ksdialog.exec();
 
   ksdialog.exec();
</code>
+
</syntaxhighlight>
  
 
===Passing name by code===
 
===Passing name by code===
 
If you want to suggest a name programatically you can do so by using the <tt>KScoreDialog::FieldInfo</tt> type. It's easy to use, for example:
 
If you want to suggest a name programatically you can do so by using the <tt>KScoreDialog::FieldInfo</tt> type. It's easy to use, for example:
<code cppqt n>
+
<syntaxhighlight lang="cpp-qt" line>
 
#include <KScoreDialog>
 
#include <KScoreDialog>
  
Line 84: Line 84:
 
   ksdialog.addScore(scoreInfo);
 
   ksdialog.addScore(scoreInfo);
 
}
 
}
</code>
+
</syntaxhighlight>
  
 
And in this same way you can display other default fields by passing them to the <tt>KScoreDialog</tt> constructor and adding the information to your <tt>KScoreDialog::FieldInfo</tt>. For example, to display the level the player got to:
 
And in this same way you can display other default fields by passing them to the <tt>KScoreDialog</tt> constructor and adding the information to your <tt>KScoreDialog::FieldInfo</tt>. For example, to display the level the player got to:
<code cppqt n>
+
<syntaxhighlight lang="cpp-qt" line>
 
#include <KScoreDialog>
 
#include <KScoreDialog>
  
Line 102: Line 102:
 
   ksdialog.addScore(scoreInfo);
 
   ksdialog.addScore(scoreInfo);
 
}
 
}
</code>
+
</syntaxhighlight>
 
===Custom fields===
 
===Custom fields===
Since there are only a set number of standatd fields it is possible to add you own custom fields. For example you may want to put the number of moves the player made on the table. This is done through the <tt>KScoreDialog::addField()</tt> function.
+
Since there are only a set number of standard fields it is possible to add your own custom fields. For example you may want to put the number of moves the player made to the table. This is done through the <tt>KScoreDialog::addField()</tt> function.
<code cppqt n>
+
<syntaxhighlight lang="cpp-qt" line>
 
#include <KScoreDialog>
 
#include <KScoreDialog>
  
Line 117: Line 117:
 
    
 
    
 
   ksdialog.addField(KScoreDialog::Custom1,  
 
   ksdialog.addField(KScoreDialog::Custom1,  
                     "Num of Moves", "moves");
+
                     i18n("Num of Moves"), "moves");
 
   scoreInfo[KScoreDialog::Custom1].setNum(numMoves);
 
   scoreInfo[KScoreDialog::Custom1].setNum(numMoves);
 
    
 
    
 
   ksdialog.addScore(scoreInfo);
 
   ksdialog.addScore(scoreInfo);
 
}
 
}
</code>
+
</syntaxhighlight>
The first argument to <tt>KScoreDialog::addField()</tt> must be from <tt>KScoreDialog::Custom1</tt> to <tt>KScoreDialog::Custom5</tt>, the second is the text that will appear on the high score table at the head of the column and the last argument is a unique identifier for the field. After that, it is added to your <tt>KScoreDialog::FieldInfo</tt> in the same way as before.
+
The first argument to <tt>KScoreDialog::addField()</tt> must be from <tt>KScoreDialog::Custom1</tt> to <tt>KScoreDialog::Custom5</tt>, the second is the text that will appear on the high score table at the head of the column (and must therefore be i18n'd) and the last argument is a unique identifier for the field. After that, it is added to your <tt>KScoreDialog::FieldInfo</tt> in the same way as before.
  
 
==Grouping Scores==
 
==Grouping Scores==
 
Your game may have different levels of difficulty and so you don't want to put the scores for the 'Hard' level on the same table as the 'Easy' table. Grouping scores is easy to achieve using the <tt>KScoreDialog::setConfigGroup()</tt> function:
 
Your game may have different levels of difficulty and so you don't want to put the scores for the 'Hard' level on the same table as the 'Easy' table. Grouping scores is easy to achieve using the <tt>KScoreDialog::setConfigGroup()</tt> function:
<code cppqt n>
+
<syntaxhighlight lang="cpp-qt" line>
 
#include <KScoreDialog>
 
#include <KScoreDialog>
  
void newHighscore(int playersScore, const QString& difficulty)
+
void newHighscore(int playersScore)
 
{
 
{
 
   KScoreDialog ksdialog(KScoreDialog::Name, this);
 
   KScoreDialog ksdialog(KScoreDialog::Name, this);
   ksdialog.setConfigGroup(difficulty);
+
   ksdialog.setConfigGroup(I18N_NOOP("Easy"));
 
   ksdialog.addScore(playersScore);
 
   ksdialog.addScore(playersScore);
 
   ksdialog.exec();
 
   ksdialog.exec();
 
}
 
}
</code>
+
</syntaxhighlight>
 
Each group will show up as a tab on the high score table dialog. If you don't specify a group, the score will be added to the generic 'High Score' tab.
 
Each group will show up as a tab on the high score table dialog. If you don't specify a group, the score will be added to the generic 'High Score' tab.
 +
 +
A very important point to remember is to pass the untranslated group name through <tt>I18N_NOOP()</tt> otherwise the group name will not be translated properly and will cause bugs.
  
 
==Advanced Features==
 
==Advanced Features==
Line 144: Line 146:
 
In some special cases you won't want to display a score as such but rather just the time the player took to complete the level.
 
In some special cases you won't want to display a score as such but rather just the time the player took to complete the level.
  
While you're not going to display the 'Score' field in your table you must always submit a value to 'Score' field since it is used for sorting the scores. In fact, the 'Score' field is always present in a <tt>KScoreDialog</tt> object even if you didn't pass it to the constructor. However, ity is possible to hide the 'Score' field (and in fact any field) from display using the <tt>KScoreDialog::hideField()</tt> function. It takes a Field flag from the same set that the constructor takes.
+
While you're not going to display the 'Score' field in your table you must always submit a value to 'Score' field since it is used for sorting the scores. In fact, the 'Score' field is always present in a <tt>KScoreDialog</tt> object even if you didn't pass it to the constructor. However, it is possible to hide the 'Score' field (and in fact any field) from display using the <tt>KScoreDialog::hideField()</tt> function. It takes a Field flag from the same set that the constructor takes.
<code cppqt n>
+
<syntaxhighlight lang="cpp-qt" line>
 
#include <KScoreDialog>
 
#include <KScoreDialog>
  
Line 162: Line 164:
 
   ksdialog.exec();
 
   ksdialog.exec();
 
}
 
}
</code>
+
</syntaxhighlight>
  
 
[[Category:C++]]
 
[[Category:C++]]
 +
[[Category:KDEGames]]

Latest revision as of 20:34, 29 June 2011

High Scores
Tutorial Series   KDE Games
Previous   Introduction to KDE4 programming, KStandardGameAction Tutorial
What's Next  
Further Reading   KScoreDialog

Contents

[edit] Abstract

This tutorial will explain how to use the high score system built into kdegames in your application.

Dialog in KMines

[edit] KScoreDialog

The high score system is provided through the KScoreDialog class. It is intended to be a lightweight solution and offers the following abilities:

  1. Grouping of scores (for different difficulty levels etc.)
  2. Standard fields: Name, Level, Date, Time, Score
  3. Custom fields (e.g. number of moves)

[edit] Displaying Scores

To display the high score table, it's as simple as two lines of code.

  1. #include <KScoreDialog>
  2.  
  3. void showHighscores()
  4. {
  5.   KScoreDialog ksdialog(KScoreDialog::Name, this);
  6.   ksdialog.exec();
  7. }

This will display the highscore table showing, by default, the 'Name' and 'Score' columns.

In order to comply with the kdegames standards, you should use a KStandardGameAction to launch this function. This will set up the icons and toolbars correctly. See the KStandardGameAction tutorial for more information on this.

  1. #include <KStandardGameAction>
  2. void setupActions()
  3. {
  4.   KStandardGameAction::highscores(this, 
  5.                        SLOT(showHighscores()), 
  6.                        actionCollection());
  7.   setupGUI();
  8. }

[edit] Adding Scores

[edit] Simplest way to add a score

The simplest way to add a new score to the table is as follows:

  1. #include <KScoreDialog>
  2.  
  3. void newHighscore(int playersScore)
  4. {
  5.   KScoreDialog ksdialog(KScoreDialog::Name, this);
  6.   ksdialog.addScore(playersScore);
  7.   ksdialog.exec();
  8. }

This will add the score playersScore to the high score table and then launch the dialog. If it is the first score to be entered on the table, a blank line-edit will be provided for the player to enter their name, otherwise the line-edit will still be provided but the player's name will be automatically filled in.

However, you will notice that the dialog is shown every time, even if the player didn't manage to get onto the table. If you only want to show the table if the player achieved a high score, place an if() command around the KScoreDialog::addScore() call:

if(ksdialog.addScore(playersScore))
  ksdialog.exec();

[edit] Passing name by code

If you want to suggest a name programatically you can do so by using the KScoreDialog::FieldInfo type. It's easy to use, for example:

  1. #include <KScoreDialog>
  2.  
  3. void newHighscore(int score, const QString& name)
  4. {
  5.   KScoreDialog ksdialog(KScoreDialog::Name, this);
  6.  
  7.   KScoreDialog::FieldInfo scoreInfo;
  8.   scoreInfo[KScoreDialog::Name] = name;
  9.   scoreInfo[KScoreDialog::Score].setNum(score);
  10.  
  11.   ksdialog.addScore(scoreInfo);
  12. }

And in this same way you can display other default fields by passing them to the KScoreDialog constructor and adding the information to your KScoreDialog::FieldInfo. For example, to display the level the player got to:

  1. #include <KScoreDialog>
  2.  
  3. void newHighscore(int score, const QString& name, int level)
  4. {
  5.   KScoreDialog ksdialog(KScoreDialog::Name |
  6.                         KScoreDialog::Level, this);
  7.  
  8.   KScoreDialog::FieldInfo scoreInfo;
  9.   scoreInfo[KScoreDialog::Name] = name;
  10.   scoreInfo[KScoreDialog::Score].setNum(score);
  11.   scoreInfo[KScoreDialog::Level].setNum(level);
  12.  
  13.   ksdialog.addScore(scoreInfo);
  14. }

[edit] Custom fields

Since there are only a set number of standard fields it is possible to add your own custom fields. For example you may want to put the number of moves the player made to the table. This is done through the KScoreDialog::addField() function.

  1. #include <KScoreDialog>
  2.  
  3. void newHighscore(int score, const QString& name, int numMoves)
  4. {
  5.   KScoreDialog ksdialog(KScoreDialog::Name, this);
  6.  
  7.   KScoreDialog::FieldInfo scoreInfo;
  8.   scoreInfo[KScoreDialog::Name] = "Matt";
  9.   scoreInfo[KScoreDialog::Score].setNum(playersScore);
  10.  
  11.   ksdialog.addField(KScoreDialog::Custom1, 
  12.                     i18n("Num of Moves"), "moves");
  13.   scoreInfo[KScoreDialog::Custom1].setNum(numMoves);
  14.  
  15.   ksdialog.addScore(scoreInfo);
  16. }

The first argument to KScoreDialog::addField() must be from KScoreDialog::Custom1 to KScoreDialog::Custom5, the second is the text that will appear on the high score table at the head of the column (and must therefore be i18n'd) and the last argument is a unique identifier for the field. After that, it is added to your KScoreDialog::FieldInfo in the same way as before.

[edit] Grouping Scores

Your game may have different levels of difficulty and so you don't want to put the scores for the 'Hard' level on the same table as the 'Easy' table. Grouping scores is easy to achieve using the KScoreDialog::setConfigGroup() function:

  1. #include <KScoreDialog>
  2.  
  3. void newHighscore(int playersScore)
  4. {
  5.   KScoreDialog ksdialog(KScoreDialog::Name, this);
  6.   ksdialog.setConfigGroup(I18N_NOOP("Easy"));
  7.   ksdialog.addScore(playersScore);
  8.   ksdialog.exec();
  9. }

Each group will show up as a tab on the high score table dialog. If you don't specify a group, the score will be added to the generic 'High Score' tab.

A very important point to remember is to pass the untranslated group name through I18N_NOOP() otherwise the group name will not be translated properly and will cause bugs.

[edit] Advanced Features

[edit] Hiding Fields

In some special cases you won't want to display a score as such but rather just the time the player took to complete the level.

While you're not going to display the 'Score' field in your table you must always submit a value to 'Score' field since it is used for sorting the scores. In fact, the 'Score' field is always present in a KScoreDialog object even if you didn't pass it to the constructor. However, it is possible to hide the 'Score' field (and in fact any field) from display using the KScoreDialog::hideField() function. It takes a Field flag from the same set that the constructor takes.

  1. #include <KScoreDialog>
  2.  
  3. // timeString is a nicely formatted string
  4. // e.g. "1:24" used for display
  5. void newHighscore(const QString& timeString, int seconds)
  6. {
  7.   KScoreDialog ksdialog(KScoreDialog::Name | 
  8.                         KScoreDialog::Time, this);
  9.  
  10.   KScoreDialog::FieldInfo scoreInfo;
  11.   scoreInfo[KScoreDialog::Time] = timeString;
  12.   scoreInfo[KScoreDialog::Score].setNum(seconds);
  13.   ksdialog.hideField(KScoreDialog::Score);
  14.   ksdialog.addScore(scoreInfo, KScoreDialog::LessIsMore)
  15.   ksdialog.exec();
  16. }

This page was last modified on 29 June 2011, at 20:34. This page has been accessed 11,008 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal