Skip to content

Commit 82440a2

Browse files
committed
Refactor eventget.py sample to use pattern matching
1 parent 4add12e commit 82440a2

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

examples/eventget.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
import tcod.context
99
import tcod.event
1010
import tcod.sdl.joystick
11-
import tcod.sdl.sys
1211

13-
WIDTH, HEIGHT = 720, 480
12+
WIDTH, HEIGHT = 1280, 720
1413

1514

16-
def main() -> None:
15+
def main() -> None: # noqa: C901, PLR0912
1716
"""Example program for tcod.event."""
1817
event_log: list[str] = []
1918
motion_desc = ""
@@ -40,24 +39,23 @@ def main() -> None:
4039
for event in tcod.event.wait():
4140
context.convert_event(event) # Set tile coordinates for event.
4241
print(repr(event))
43-
if isinstance(event, tcod.event.Quit):
44-
raise SystemExit
45-
if isinstance(event, tcod.event.WindowResized) and event.type == "WindowResized":
46-
console = context.new_console()
47-
if isinstance(event, tcod.event.ControllerDevice):
48-
if event.type == "CONTROLLERDEVICEADDED":
49-
controllers.add(event.controller)
50-
elif event.type == "CONTROLLERDEVICEREMOVED":
51-
controllers.remove(event.controller)
52-
if isinstance(event, tcod.event.JoystickDevice):
53-
if event.type == "JOYDEVICEADDED":
54-
joysticks.add(event.joystick)
55-
elif event.type == "JOYDEVICEREMOVED":
56-
joysticks.remove(event.joystick)
57-
if isinstance(event, tcod.event.MouseMotion):
58-
motion_desc = str(event)
59-
else: # Log all events other than MouseMotion.
60-
event_log.append(str(event))
42+
match event:
43+
case tcod.event.Quit():
44+
raise SystemExit
45+
case tcod.event.WindowResized(type="WindowResized"):
46+
console = context.new_console()
47+
case tcod.event.ControllerDevice(type="CONTROLLERDEVICEADDED", controller=controller):
48+
controllers.add(controller)
49+
case tcod.event.ControllerDevice(type="CONTROLLERDEVICEREMOVED", controller=controller):
50+
controllers.remove(controller)
51+
case tcod.event.JoystickDevice(type="JOYDEVICEADDED", joystick=joystick):
52+
joysticks.add(joystick)
53+
case tcod.event.JoystickDevice(type="JOYDEVICEREMOVED", joystick=joystick):
54+
joysticks.remove(joystick)
55+
case tcod.event.MouseMotion():
56+
motion_desc = str(event)
57+
case _: # Log all events other than MouseMotion.
58+
event_log.append(repr(event))
6159

6260

6361
if __name__ == "__main__":

0 commit comments

Comments
 (0)