You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
29
30
This tileset is from the `Dwarf Fortress tileset repository <https://dwarffortresswiki.org/index.php/DF2014:Tileset_repository>`_.
30
31
These kinds of tilesets are always loaded with :python:`columns=16, rows=16, charmap=tcod.tileset.CHARMAP_CP437`.
31
32
@@ -62,7 +63,7 @@ Configuring an event loop
62
63
The next step is to keep the window open until the user closes it.
63
64
64
65
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]_
66
67
67
68
Begin the main game loop with a :python:`while True:` statement.
68
69
@@ -71,11 +72,13 @@ Do this first in the game loop before handing events.
71
72
72
73
Events are checked by iterating over all pending events with :any:`tcod.event.wait`.
73
74
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]_
76
79
77
80
.. code-block:: python
78
-
:emphasize-lines: 2,3,11-18
81
+
:emphasize-lines: 2,3,11-19
79
82
80
83
...
81
84
import tcod.console
@@ -93,12 +96,14 @@ If this is True then you should exit the function with :python:`raise SystemExit
93
96
whileTrue: # Main loop
94
97
context.present(console) # Render the console to the window and show it
95
98
for event in tcod.event.wait(): # Event loop, blocks until pending events exist
99
+
print(event)
96
100
ifisinstance(event, tcod.event.Quit):
97
101
raiseSystemExit()
98
102
...
99
103
100
104
If you run this then you get a window saying :python:`"Hello World"`.
101
105
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.
@@ -165,6 +170,7 @@ Modify the drawing routine so that the console is cleared, then passed to :pytho
165
170
state.on_draw(console) # Draw the current state
166
171
context.present(console) # Display the console on the window
167
172
for event in tcod.event.wait():
173
+
print(event)
168
174
ifisinstance(event, tcod.event.Quit):
169
175
raiseSystemExit()
170
176
...
@@ -182,6 +188,7 @@ Begin matching with :python:`match event:`.
182
188
The equivalent to :python:`if isinstance(event, tcod.event.Quit):` is :python:`case tcod.event.Quit():`.
183
189
Keyboard keys can be checked with :python:`case tcod.event.KeyDown(sym=tcod.event.KeySym.LEFT):`.
184
190
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.
185
192
See :any:`KeySym` for a list of all keys.
186
193
187
194
.. code-block:: python
@@ -209,7 +216,7 @@ See :any:`KeySym` for a list of all keys.
209
216
Now replace the event handling code in ``main`` to defer to the states ``on_event`` method.
210
217
211
218
.. code-block:: python
212
-
:emphasize-lines: 11
219
+
:emphasize-lines: 12
213
220
214
221
...
215
222
defmain() -> None:
@@ -221,9 +228,21 @@ Now replace the event handling code in ``main`` to defer to the states ``on_even
221
228
state.on_draw(console)
222
229
context.present(console)
223
230
for event in tcod.event.wait():
231
+
print(event)
224
232
state.on_event(event) # Pass events to the state
225
233
...
226
234
227
235
Now when you run this script you have a player character you can move around with the arrow keys before closing the window.
228
236
229
237
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