Skip to content

Commit 6436a59

Browse files
committed
Tutorial: Print events and justify more decisions.
1 parent 322e0db commit 6436a59

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

docs/tutorial/part-01.rst

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Initial script
99
==============================================================================
1010

1111
First start with a modern top-level script.
12+
You should have ``main.py`` script from :ref:`part-0`:
1213

1314
.. code-block:: python
1415
@@ -25,7 +26,7 @@ Loading a tileset and opening a window
2526
==============================================================================
2627

2728
From here it is time to setup a ``tcod`` program.
28-
Download `Alloy_curses_12x12.png <https://raw.githubusercontent.com/HexDecimal/python-tcod-tutorial-2023/6b69bf9b5531963a0e5f09f9d8fe72a4001d4881/data/Alloy_curses_12x12.png>`_ and place this file in your projects ``data/`` directory.
29+
Download `Alloy_curses_12x12.png <https://raw.githubusercontent.com/HexDecimal/python-tcod-tutorial-2023/6b69bf9b5531963a0e5f09f9d8fe72a4001d4881/data/Alloy_curses_12x12.png>`_ [#tileset]_ and place this file in your projects ``data/`` directory.
2930
This tileset is from the `Dwarf Fortress tileset repository <https://dwarffortresswiki.org/index.php/DF2014:Tileset_repository>`_.
3031
These kinds of tilesets are always loaded with :python:`columns=16, rows=16, charmap=tcod.tileset.CHARMAP_CP437`.
3132

@@ -62,7 +63,7 @@ Configuring an event loop
6263
The next step is to keep the window open until the user closes it.
6364

6465
Since nothing is displayed yet a :any:`Console` should be created with :python:`"Hello World"` printed to it.
65-
The size of the console can be used as a reference to create the context by adding the console to :any:`tcod.context.new`.
66+
The size of the console can be used as a reference to create the context by adding the console to :any:`tcod.context.new`. [#init_context]_
6667

6768
Begin the main game loop with a :python:`while True:` statement.
6869

@@ -71,11 +72,13 @@ Do this first in the game loop before handing events.
7172

7273
Events are checked by iterating over all pending events with :any:`tcod.event.wait`.
7374
Use the code :python:`for event in tcod.event.wait():` to begin handing events.
74-
Test if an event is for closing the window with :python:`if isinstance(event, tcod.event.Quit):`.
75-
If this is True then you should exit the function with :python:`raise SystemExit`.
75+
76+
In the event loop start with the line :python:`print(event)` so that all events can be viewed from the program output.
77+
Then test if an event is for closing the window with :python:`if isinstance(event, tcod.event.Quit):`.
78+
If this is True then you should exit the function with :python:`raise SystemExit()`. [#why_raise]_
7679

7780
.. code-block:: python
78-
:emphasize-lines: 2,3,11-18
81+
:emphasize-lines: 2,3,11-19
7982
8083
...
8184
import tcod.console
@@ -93,12 +96,14 @@ If this is True then you should exit the function with :python:`raise SystemExit
9396
while True: # Main loop
9497
context.present(console) # Render the console to the window and show it
9598
for event in tcod.event.wait(): # Event loop, blocks until pending events exist
99+
print(event)
96100
if isinstance(event, tcod.event.Quit):
97101
raise SystemExit()
98102
...
99103
100104
If you run this then you get a window saying :python:`"Hello World"`.
101105
The window can be resized and the console will be stretched to fit the new resolution.
106+
When you do anything such as press a key or interact with the window the event for that action will be printed to the program output.
102107

103108
An example game state
104109
==============================================================================
@@ -165,6 +170,7 @@ Modify the drawing routine so that the console is cleared, then passed to :pytho
165170
state.on_draw(console) # Draw the current state
166171
context.present(console) # Display the console on the window
167172
for event in tcod.event.wait():
173+
print(event)
168174
if isinstance(event, tcod.event.Quit):
169175
raise SystemExit()
170176
...
@@ -182,6 +188,7 @@ Begin matching with :python:`match event:`.
182188
The equivalent to :python:`if isinstance(event, tcod.event.Quit):` is :python:`case tcod.event.Quit():`.
183189
Keyboard keys can be checked with :python:`case tcod.event.KeyDown(sym=tcod.event.KeySym.LEFT):`.
184190
Make a case for each arrow key: ``LEFT`` ``RIGHT`` ``UP`` ``DOWN`` and move the player in the direction of that key.
191+
Since events are printed you can check the :any:`KeySym` of a key by pressing that key and looking at the printed output.
185192
See :any:`KeySym` for a list of all keys.
186193

187194
.. code-block:: python
@@ -209,7 +216,7 @@ See :any:`KeySym` for a list of all keys.
209216
Now replace the event handling code in ``main`` to defer to the states ``on_event`` method.
210217

211218
.. code-block:: python
212-
:emphasize-lines: 11
219+
:emphasize-lines: 12
213220
214221
...
215222
def main() -> None:
@@ -221,9 +228,21 @@ Now replace the event handling code in ``main`` to defer to the states ``on_even
221228
state.on_draw(console)
222229
context.present(console)
223230
for event in tcod.event.wait():
231+
print(event)
224232
state.on_event(event) # Pass events to the state
225233
...
226234
227235
Now when you run this script you have a player character you can move around with the arrow keys before closing the window.
228236

229237
You can review the part-1 source code `here <https://github.com/HexDecimal/python-tcod-tutorial-2023/tree/part-1>`_.
238+
239+
.. rubric:: Footnotes
240+
241+
.. [#tileset] The choice of tileset came down to what looked nice while also being square.
242+
Other options such as using a BDF font were considered, but in the end this tutorial won't go too much into Unicode.
243+
244+
.. [#init_context] This tutorial follows the setup for a fixed-size console.
245+
The alternatives shown in :ref:`getting-started` are outside the scope of this tutorial.
246+
247+
.. [#why_raise] You could use :python:`return` here to exit the ``main`` function and end the program, but :python:`raise SystemExit()` is used because it will close the program from anywhere.
248+
:python:`raise SystemExit()` is also more useful to teach than :any:`sys.exit`.

0 commit comments

Comments
 (0)