|
27 | 27 | while True: |
28 | 28 | console = context.new_console() |
29 | 29 | 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 |
32 | 33 | if isinstance(event, tcod.event.Quit): |
33 | | - print(event) |
34 | 34 | raise SystemExit() |
35 | 35 | 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 |
37 | 37 | if event.sym in KEY_COMMANDS: |
38 | 38 | print(f"Command: {KEY_COMMANDS[event.sym]}") |
39 | 39 | 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 |
41 | 41 | 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 |
45 | 43 |
|
46 | 44 | Python 3.10 introduced `match statements <https://docs.python.org/3/tutorial/controlflow.html#match-statements>`_ |
47 | 45 | which can be used to dispatch events more gracefully: |
|
61 | 59 | while True: |
62 | 60 | console = context.new_console() |
63 | 61 | 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. |
66 | 64 | match event: |
67 | 65 | case tcod.event.Quit(): |
68 | 66 | raise SystemExit() |
69 | 67 | case tcod.event.KeyDown(sym=sym) if sym in KEY_COMMANDS: |
70 | 68 | print(f"Command: {KEY_COMMANDS[sym]}") |
71 | 69 | case tcod.event.KeyDown(sym=sym, scancode=scancode, mod=mod, repeat=repeat): |
72 | 70 | 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=}") |
77 | 77 | case tcod.event.Event() as event: |
78 | | - print(event) # Show any unhandled events. |
| 78 | + print(event) # Print unhandled events |
79 | 79 |
|
80 | 80 | .. versionadded:: 8.4 |
81 | 81 | """ |
|
0 commit comments