0

The following works:

while True:
    for event in pygame.event.get():
        match (event.type):
            case pygame.QUIT:
                pygame.quit()

but if I write without "pygame." in the case expression

while True:
    for event in pygame.event.get():
        match (event.type):
            case QUIT:
                pygame.quit()

the case catches all the events and not only the QUIT one. Other potential cases are also marked unreachable by python.

Why is that? print(pygame.QUIT) and print(QUIT) both display "256".

Pierre Arlaud
  • 4,040
  • 3
  • 28
  • 42
  • What version of Python are you using, when I try something similar in Python 3.10.5, I encounter a `Syntax Error: name capture 'QUIT' makes remaining patterns unreachable`. Looking up this error takes me to [this answer](https://stackoverflow.com/a/67525259/2280890) which basically says you need to use dotted names. So I'd skip the * import. – import random Jul 04 '22 at 01:57
  • @importrandom Interesting, I'm on 3.10.5 too – Pierre Arlaud Jul 04 '22 at 08:11

1 Answers1

0

You cant use the QUIT keyword if you only had imported pygame, to use only QUIT you need to also import pygame locals:

import pygame # Default
from pygame.locals import * # Optional

Then you can use either pygame.QUIT or QUIT.

Also instead of matching these events you can use:

for event in pygame.event.get(): # Handle events
    if event.type == pygame.QUIT: # Check if the X button is clicked
        pygame.quit() # Quit pygame

Then you just only need to import pygame

Errorflin
  • 1
  • 1
  • Thanks for answering. Well that's my whole point, if you import * from pygame.locals, and then use only QUIT on the match, it doesn't work! Why is that? – Pierre Arlaud Jul 02 '22 at 19:57