pygame 个与模式匹配兼容的事件
pygame events compatible with pattern matching
我正在尝试对 pygame 事件使用模式匹配,但我得到 TypeError: called match pattern must be a type
请参阅下面的示例
import pygame
import pygame.event
a = pygame.event.Event(0)
def test_match(x):
match x:
case pygame.event.Event():
print('Case Event()', x)
case pygame.event.Event(type=pygame.USEREVENT):
print('Case Event(pygame.USEREVENT)', x)
case _:
print('not an event')
test_match(a)
满输出
pygame 2.1.0 (SDL 2.0.16, Python 3.10.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "match_test.py", line 15, in <module>
test_match(a)
File "match_test.py", line 8, in test_match
case pygame.event.Event():
TypeError: called match pattern must be a type
我们可以在模式匹配中使用pygame.event.EventType
。
case pygame.event.EventType(type=pygame.USEREVENT):
我正在尝试对 pygame 事件使用模式匹配,但我得到 TypeError: called match pattern must be a type
请参阅下面的示例
import pygame
import pygame.event
a = pygame.event.Event(0)
def test_match(x):
match x:
case pygame.event.Event():
print('Case Event()', x)
case pygame.event.Event(type=pygame.USEREVENT):
print('Case Event(pygame.USEREVENT)', x)
case _:
print('not an event')
test_match(a)
满输出
pygame 2.1.0 (SDL 2.0.16, Python 3.10.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "match_test.py", line 15, in <module>
test_match(a)
File "match_test.py", line 8, in test_match
case pygame.event.Event():
TypeError: called match pattern must be a type
我们可以在模式匹配中使用pygame.event.EventType
。
case pygame.event.EventType(type=pygame.USEREVENT):