PyGame Custom Event

后端 未结 3 1063
萌比男神i
萌比男神i 2020-12-30 10:55

I want to ask about the use of custom events in pygame ..
I experimented a little with pygame, so I know how usual pygame-generated events works ..
My Question

3条回答
  •  失恋的感觉
    2020-12-30 11:34

    You can compare events with "==". The compared events must be exactly the same with their attirubutes to be equal

    Event1=pygame.event.Event(pygame.USEREVENT, attr1='Event1')
    Event2=pygame.event.Event(pygame.USEREVENT, attr1='Event2')
    

    Throw events

    pygame.event.post(Event1)
    pygame.event.post(Event2)
    

    Main loop

    while True:
    
        for event in pygame.event.get():
    
            if event.type == pygame.QUIT:
                break
            if event== Event1:
                print("event1")
            elif event == Event2:
                print("event2")
    

提交回复
热议问题