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
@@ -23,7 +25,7 @@ Loading a tileset and opening a window
23
25
From here it is time to setup a ``tcod`` program.
24
26
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.
25
27
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``.
28
+
These kinds of tilesets are always loaded with :python:`columns=16, rows=16, charmap=tcod.tileset.CHARMAP_CP437`.
27
29
28
30
Load the tileset with :any:`tcod.tileset.load_tilesheet` and then pass it to :any:`tcod.context.new`.
29
31
These functions are part of modules which have not been imported yet, so new imports need to be added.
@@ -47,7 +49,7 @@ These functions are part of modules which have not been imported yet, so new imp
47
49
...
48
50
49
51
If an import fails that means you do not have ``tcod`` installed on the Python environment you just used to run the script.
50
-
If you use an IDE then make sure the Python environment it is using is correct and then run ``pip install tcod`` from the shell terminal within that IDE.
52
+
If you use an IDE then make sure the Python environment it is using is correct and then run :shell:`pip install tcod` from the shell terminal within that IDE.
51
53
52
54
If you run this script now then a window will open and then immediately close.
53
55
If that happens without seeing a traceback in your terminal then the script is correct.
@@ -57,18 +59,18 @@ Configuring an event loop
57
59
58
60
The next step is to keep the window open until the user closes it.
59
61
60
-
Since nothing is displayed yet a :any:`Console` should be created with ``"Hello World"`` printed to it.
62
+
Since nothing is displayed yet a :any:`Console` should be created with :python:`"Hello World"` printed to it.
61
63
The size of the console can be used as a reference to create the context by adding the console to :any:`tcod.context.new`.
62
64
63
-
Begin the main game loop with a ``while True:`` statement.
65
+
Begin the main game loop with a :python:`while True:` statement.
64
66
65
67
To actually display the console to the window the :any:`Context.present` method must be called with the console as a parameter.
66
68
Do this first in the game loop before handing events.
67
69
68
70
Events are checked by iterating over all pending events with :any:`tcod.event.wait`.
69
-
Use the code ``for event in tcod.event.wait():`` to begin handing events.
70
-
Test if an event is for closing the window with ``if isinstance(event, tcod.event.Quit):``.
71
-
If this is True then you should exit the function with ``raise SystemExit``.
71
+
Use the code :python:`for event in tcod.event.wait():` to begin handing events.
72
+
Test if an event is for closing the window with :python:`if isinstance(event, tcod.event.Quit):`.
73
+
If this is True then you should exit the function with :python:`raise SystemExit`.
72
74
73
75
.. code-block:: python
74
76
:emphasize-lines: 2,3,11-18
@@ -93,7 +95,7 @@ If this is True then you should exit the function with ``raise SystemExit``.
93
95
raiseSystemExit()
94
96
...
95
97
96
-
If you run this then you get a window saying ``"Hello World"``.
98
+
If you run this then you get a window saying :python:`"Hello World"`.
97
99
The window can be resized and the console will be stretched to fit the new resolution.
98
100
99
101
An example game state
@@ -102,18 +104,18 @@ An example game state
102
104
What exists now is not very interactive.
103
105
The next step is to change state based on user input.
104
106
105
-
Like ``tcod`` you'll need to install ``attrs`` with Pip, such as with ``pip install attrs``.
107
+
Like ``tcod`` you'll need to install ``attrs`` with Pip, such as with :shell:`pip install attrs`.
106
108
107
109
Start by adding an ``attrs`` class called ``ExampleState``.
108
-
This a normal class with the ``@attrs.define(eq=False)`` decorator added.
110
+
This a normal class with the :python:`@attrs.define(eq=False)` decorator added.
109
111
110
112
This class should hold coordinates for the player.
111
113
It should also have a ``on_draw`` method which takes :any:`tcod.console.Console` as a parameter and marks the player position on it.
112
-
The parameters for ``on_draw`` are ``self`` because this is an instance method and ``console: tcod.console.Console``.
113
-
``on_draw`` returns nothing, so be sure to add ``-> None``.
114
+
The parameters for ``on_draw`` are ``self`` because this is an instance method and :python:`console: tcod.console.Console`.
115
+
``on_draw`` returns nothing, so be sure to add :python:`-> None`.
114
116
115
117
:any:`Console.print` is the simplest way to draw the player because other options would require bounds-checking.
116
-
Call this method using the players current coordinates and the ``"@"`` character.
118
+
Call this method using the players current coordinates and the :python:`"@"` character.
117
119
118
120
.. code-block:: python
119
121
@@ -135,13 +137,13 @@ Call this method using the players current coordinates and the ``"@"`` character
135
137
console.print(self.player_x, self.player_y, "@")
136
138
...
137
139
138
-
Now remove the ``console.print(0, 0, "Hello World")`` line from ``main``.
140
+
Now remove the :python:`console.print(0, 0, "Hello World")` line from ``main``.
139
141
140
142
Before the context is made create a new ``ExampleState`` with player coordinates on the screen.
141
-
Each :any:`Console` has ``.width`` and ``.height`` attributes which you can divide by 2 to get a centered coordinate for the player.
142
-
Use Python's floor division operator ``//`` so that the resulting type is ``int``.
143
+
Each :any:`Console` has :python:`.width` and :python:`.height` attributes which you can divide by 2 to get a centered coordinate for the player.
144
+
Use Python's floor division operator :python:`//` so that the resulting type is :python:`int`.
143
145
144
-
Modify the drawing routine so that the console is cleared, then passed to ``ExampleState.on_draw``, then passed to :any:`Context.present`.
146
+
Modify the drawing routine so that the console is cleared, then passed to :python:`ExampleState.on_draw`, then passed to :any:`Context.present`.
145
147
146
148
.. code-block:: python
147
149
:emphasize-lines: 9,12-14
@@ -174,9 +176,9 @@ A new method will be added to the ``ExampleState`` for this called ``on_event``.
174
176
Events are best handled using Python's `Structural Pattern Matching <https://peps.python.org/pep-0622/>`_.
0 commit comments