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
The first line is a `shebang <https://en.wikipedia.org/wiki/Shebang_%28Unix%29>`_ which allows direct execution of the script and will hint certain Python launchers which version to use.
25
-
If you always invoke Python directly then you do not need this line.
26
-
27
-
The triple-quoted string is a `docstring <https://en.wikipedia.org/wiki/Docstring>`_.
28
-
The one near the top documents the purpose for the module.
29
-
The one in ``main`` documents that function.
30
-
31
-
``from __future__ import annotations`` tells Python to use `Postponed Evaluation of Annotations <https://peps.python.org/pep-0563/>`_.
32
-
This is required for specific type-hints, such as a class using itself in its own annotations.
33
-
This will also speed up the initialization of code which uses type-hints.
34
-
35
-
``def main() -> None:`` has no significance other than convention.
36
-
Because this function returns nothing it is annotated with ``-> None``.
37
-
38
-
``if __name__ == "__main__":`` checks for the `Top-level code environment <https://docs.python.org/3/library/__main__.html>`_.
39
-
This prevents tools from accidentally launching the script when they just want to import it.
40
-
This is the only required boilerplate, everything else is optional.
18
+
You will replace body of the ``main`` function in the following section.
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.
47
-
This tileset is from the `Dwarf Fortress tileset repository <https://dwarffortresswiki.org/index.php/DF2014:Tileset_repository>`_ and you may choose to use any other tileset from there as long is you keep track of the filename yourself.
25
+
This tileset is from the `Dwarf Fortress tileset repository <https://dwarffortresswiki.org/index.php/DF2014:Tileset_repository>`_.
26
+
These kinds of tilesets are always loaded with ``columns=16, rows=16, charmap=tcod.tileset.CHARMAP_CP437``.
48
27
49
28
Load the tileset with :any:`tcod.tileset.load_tilesheet` and then pass it to :any:`tcod.context.new`.
50
29
These functions are part of modules which have not been imported yet, so new imports need to be added.
@@ -74,22 +53,22 @@ If you run this script now then a window will open and then immediately close.
74
53
If that happens without seeing a traceback in your terminal then the script is correct.
The next step is to change state based on user input.
125
104
126
105
Like ``tcod`` you'll need to install ``attrs`` with Pip, such as with ``pip install attrs``.
127
-
Alternatively you can use :any:`dataclasses`, but this tutorial uses ``attrs`` since it has a more modern implementation.
128
106
129
107
Start by adding an ``attrs`` class called ``ExampleState``.
130
108
This a normal class with the ``@attrs.define(eq=False)`` decorator added.
@@ -135,8 +113,7 @@ The parameters for ``on_draw`` are ``self`` because this is an instance method a
135
113
``on_draw`` returns nothing, so be sure to add ``-> None``.
136
114
137
115
:any:`Console.print` is the simplest way to draw the player because other options would require bounds-checking.
138
-
139
-
If ``tcod.console.Console`` is too verbose then you can add ``from tcod.console import Console`` so that you can use just ``Console`` instead.
116
+
Call this method using the players current coordinates and the ``"@"`` character.
140
117
141
118
.. code-block:: python
142
119
@@ -190,13 +167,6 @@ Modify the drawing routine so that the console is cleared, then passed to ``Exam
190
167
191
168
Now if you run the script you'll see ``@``.
192
169
193
-
This code is sensitive to typing.
194
-
If you wrote ``player_x=console.width / 2`` instead of ``player_x=console.width // 2`` (note the number of slashes) then ``player_x`` will be assigned as a float instead of an int.
195
-
If ``player_x`` is a float then :any:`Console.print` will raise a TypeError.
196
-
In this case the incorrect code is when ``ExampleState`` is created with an invalid type and not the print function call.
197
-
Running ``mypy`` on your code will show you this type error at the correct position.
198
-
Your IDE should also complain about a bad type if setup correctly.
199
-
200
170
The next step is to move the player on events.
201
171
A new method will be added to the ``ExampleState`` for this called ``on_event``.
202
172
``on_event`` takes a ``self`` and a :any:`tcod.event.Event` parameter and returns nothing.
0 commit comments