Skip to content

Commit 0188350

Browse files
committed
Update primary event handing example code
1 parent 84d37e2 commit 0188350

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

tcod/event.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,19 @@
2727
while True:
2828
console = context.new_console()
2929
context.present(console, integer_scaling=True)
30-
for event in tcod.event.wait():
31-
context.convert_event(event) # Adds tile coordinates to mouse events.
30+
for pixel_event in tcod.event.wait():
31+
event = context.convert_event(pixel_event) # Convert mouse pixel coordinates to tile coordinates
32+
print(event) # Print all events, for learning and debugging
3233
if isinstance(event, tcod.event.Quit):
33-
print(event)
3434
raise SystemExit()
3535
elif isinstance(event, tcod.event.KeyDown):
36-
print(event) # Prints the Scancode and KeySym enums for this event.
36+
print(f"{event.sym=}, {event.scancode=}") # Show Scancode and KeySym enum names
3737
if event.sym in KEY_COMMANDS:
3838
print(f"Command: {KEY_COMMANDS[event.sym]}")
3939
elif isinstance(event, tcod.event.MouseButtonDown):
40-
print(event) # Prints the mouse button constant names for this event.
40+
print(f"{event.button=}, {event.integer_position=}") # Show mouse button and tile
4141
elif isinstance(event, tcod.event.MouseMotion):
42-
print(event) # Prints the mouse button mask bits in a readable format.
43-
else:
44-
print(event) # Print any unhandled events.
42+
print(f"{event.integer_position=}, {event.integer_motion=}") # Current mouse tile and tile motion
4543
4644
Python 3.10 introduced `match statements <https://docs.python.org/3/tutorial/controlflow.html#match-statements>`_
4745
which can be used to dispatch events more gracefully:
@@ -61,21 +59,23 @@
6159
while True:
6260
console = context.new_console()
6361
context.present(console, integer_scaling=True)
64-
for event in tcod.event.wait():
65-
context.convert_event(event) # Adds tile coordinates to mouse events.
62+
for pixel_event in tcod.event.wait():
63+
event = context.convert_event(pixel_event) # Converts mouse pixel coordinates to tile coordinates.
6664
match event:
6765
case tcod.event.Quit():
6866
raise SystemExit()
6967
case tcod.event.KeyDown(sym=sym) if sym in KEY_COMMANDS:
7068
print(f"Command: {KEY_COMMANDS[sym]}")
7169
case tcod.event.KeyDown(sym=sym, scancode=scancode, mod=mod, repeat=repeat):
7270
print(f"KeyDown: {sym=}, {scancode=}, {mod=}, {repeat=}")
73-
case tcod.event.MouseButtonDown(button=button, pixel=pixel, tile=tile):
74-
print(f"MouseButtonDown: {button=}, {pixel=}, {tile=}")
75-
case tcod.event.MouseMotion(pixel=pixel, pixel_motion=pixel_motion, tile=tile, tile_motion=tile_motion):
76-
print(f"MouseMotion: {pixel=}, {pixel_motion=}, {tile=}, {tile_motion=}")
71+
case tcod.event.MouseButtonDown(button=button, integer_position=tile):
72+
print(f"MouseButtonDown: {button=}, {tile=}")
73+
case tcod.event.MouseMotion(integer_position=tile, integer_motion=tile_motion):
74+
assert isinstance(pixel_event, tcod.event.MouseMotion)
75+
pixel_motion = pixel_event.motion
76+
print(f"MouseMotion: {pixel_motion=}, {tile=}, {tile_motion=}")
7777
case tcod.event.Event() as event:
78-
print(event) # Show any unhandled events.
78+
print(event) # Print unhandled events
7979
8080
.. versionadded:: 8.4
8181
"""

0 commit comments

Comments
 (0)