Commit 021359ad authored by David Boddie's avatar David Boddie 💬
Browse files

Update documentation to match updated example code

parent 8bf7a7f2
......@@ -36,12 +36,15 @@ The class is defined in the normal way, beginning with the ``__init__`` method:
.. literalinclude:: ../app/src/main.py
:language: python3
:start-at: class Application
:end-at: GLib.set_application_name
:end-at: 'com.example.treasure', 'scores'
This method performs two things that are necessary for the application to run correctly:
This method performs three things that are necessary for the application to run correctly:
1. It uses the ``super`` built-function to call the ``__init__`` method of the base class. This associates the application with the application ID given. The application ID must have a certain format. This is described in the `Gio.GApplication documentation`_.
2. It calls the `GLib.set_application_name <g_set_application_name_>`_ function to set a user-readable application name that will be localized if translations are available.
#. It uses the ``super`` built-function to call the ``__init__`` method of the base class. This associates the application with the application ID given. The application ID must have a certain format. This is described in the `Gio.GApplication documentation`_.
#. It calls the `GLib.set_application_name <g_set_application_name_>`_ function to set a user-readable application name that will be localized if translations are available.
#. It defines the location of the file containing user data. This will actually be used to hold the user's best score.
Only the first two of these are critical. The third is just a convenience.
Application Start-up
~~~~~~~~~~~~~~~~~~~~
......@@ -94,13 +97,15 @@ There are four more widgets that we need to access: the box that holds the main
:start-at: Get widgets
:end-at: lowest_turns_label
The application uses `GSettings`_ to record the user's best score so that the next time the game is played, the score is not forgotten. This value is read by creating a ``Gio.Settings`` object and reading the appropriate integer value. This is then used to update the corresponding label:
The application uses a data file to record the user's best score so that the next time the game is played, the score is not forgotten. This value is read creating a `GLib.KeyFile <GKeyFile_>`_ object to read the data file, and reading the appropriate integer value if the file exists. This is then used to update the corresponding label:
.. literalinclude:: ../app/src/main.py
:language: python3
:start-at: Read the lowest number
:end-at: self.rows
A default value of zero turns is recorded. This value is handled specially.
The final few lines of the method initialize attributes that keep track of the widgets that will be placed in the window's main area.
Application Activation
......@@ -164,14 +169,40 @@ The underscore function ``_()`` is installed when the program executable is run
Handling Clicks
~~~~~~~~~~~~~~~
The last method is the ``on_clicked`` method which handles the signals sent by buttons when they are clicked or touched by the player. If the player has already won or the cell they clicked is already revealed, we return immediately. Otherwise, we increase the number of turns and update the corresponding label.
The ``on_clicked`` method handles the signals sent by buttons when they are clicked or touched by the player. If the player has already won or the cell they clicked is already revealed, we return immediately. Otherwise, we increase the number of turns and update the corresponding label.
.. literalinclude:: ../app/src/main.py
:language: python3
:start-at: on_clicked
:start-at: def on_clicked
:end-at: update_turns_label
The rest of the method handles the check for the treasure location, changing the button's image to an open treasure chest if the player guessed the location. The ``ngettext`` function is used to select the appropriate string to display for the current language. If the treasure was not in the cell that was clicked then we choose one of two images to display in the cell.
The rest of the method handles the check for the treasure location, changing the button's image to an open treasure chest if the player guessed the location. The ``ngettext`` function is used to select the appropriate string to display for the current language:
.. literalinclude:: ../app/src/main.py
:language: python3
:start-at: if pos == self.treasure_pos:
:end-at: widget.set_image(Gtk.Image.new_from_pixbuf(pixbuf))
If the treasure was not in the cell that was clicked then we choose one of two images to display in the cell.
Saving the Score
~~~~~~~~~~~~~~~~
The ``save_score`` method is called when a new best score is obtained. It updates the lowest score and updates the corresponding label in the window. In addition, it sets the appropriate value in the ``GLib.KeyFile`` object so that the data file can be updated:
.. literalinclude:: ../app/src/main.py
:language: python3
:start-at: def save_score
:end-at: set_integer
The location of this file was defined at the start of the ``Application`` class. It is located within the common directory for user data, inside a subdirectory of its own. We check that this subdirectory exists, and create it if necessary, before writing the file:
.. literalinclude:: ../app/src/main.py
:language: python3
:start-after: Try to write the score to the keyfile
:end-at: pass
If writing fails for some reason, there isn't much we can do about it, so we try to fail gracefully by catching either of the two exceptions that might be raised.
Main Function
-------------
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment