pygame-community / pygame-ce

🐍🎮 pygame - Community Edition is a FOSS Python library for multimedia applications (like games). Built on top of the excellent SDL library.
https://pyga.me
853 stars 140 forks source link

event.set_allowed #2789

Closed Aston7292 closed 6 months ago

Aston7292 commented 6 months ago

Even after using pygame.event.set_allowed all events will appear in the queue.

import pygame as pg

pg.init()

win = pg.display.set_mode((800, 800))

pg.event.set_allowed(pg.QUIT)
while True:
    for event in pg.event.get():
        print(event)  # all events appear but only pg.QUIT should
        if event.type == pg.QUIT:
            exit()

    pg.display.update()
ankith26 commented 6 months ago

Hello!

This is expected behavior. By default all events are enabled. You need to explicitly block events.

I have updated your code to make it work as you expect it to:

import pygame as pg

pg.display.init()

pg.event.set_blocked(None)
pg.event.set_allowed(pg.QUIT)

win = pg.display.set_mode((800, 800))

while True:
    for event in pg.event.get():
        print(event)
        if event.type == pg.QUIT:
            exit()

    pg.display.update()

The main difference here is the call to pg.event.set_blocked(None) before doing pg.event.set_allowed(pg.QUIT). The former blocks all events, and then the latter unblocks quit specifically.