Question

This question arose out of the need to create a lot of USEREVENT type events. Since I could not find information on how to create more userevents than the limit allows I came to ask here for help.

Currently I know that the USEREVENT type event has a value of 24 and the maximum allowed id is 31. I also found that some id-s are reserved, at least in one of the comments in the official documentation (http://www.pygame.org/docs/ref/event.html#comment_pygame_event_Event).

Based on all that here is my two-parted question: can those SDL_EVENT_RESERVED event id-s be safely used as extra spaces for user-created events (for example, a timer: pygame.time.set_timer(USEREVENT + 7, 1000)) and is there a way to create an unlimited amount of separate user-created events like in the example piece of timer code?

I am sorry if the question is not understandable because of bad wording or due to other issues.

Était-ce utile?

La solution

User events should be between:

  • pygame.USEREVENT: 24
  • pygame.NUMEVENTS: 32

So you can have 9 different user events.

The usual way is to define a constant:

SOME_EVENT = pygame.USEREVENT + 0
ANOTHER_EVENT = pygame.USEREVENT + 1
...

If you create your event with event(...) you can assign attributes to the event, that way you can create many different sub-events and assign additional data to them, e.g.: key events.

Unfortunately when you use pygame.time.set_timer() you are stuck with only an ID.

Autres conseils

If I were you, I would just rely on one USEREVENT type, to create as many as custom events I want. So, the way to achieve that is like append a special attribute for the name/type. Ok, a little bit example to make it clearer below...

Tested on pygame 1.9.6 on windows 10, python 3.7.3

import pygame as pg

ON_MY_MOUSE_CLICK = 1  # your own codes for your own events...
ON_MY_SCROLL = 2
BLAH_BLAH = 3
# etc ...

pg.init()

video = pg.display.set_mode((100,100))

MouseEvent = pg.event.Event(pg.USEREVENT, MyOwnType=ON_MY_MOUSE_CLICK )
ScrollEvent = pg.event.Event(pg.USEREVENT, MyOwnType=ON_MY_SCROLL)
blahblahEvent = pg.event.Event(pg.USEREVENT, MyOwnType=BLAH_BLAH)

pg.event.post(MouseEvent)  # call your own type
pg.event.post(ScrollEvent)  # call your own type

for event in pg.event.get():
    if (event.type == pg.QUIT):
        pass  # your built-in event handle goes here

    elif (event.type == pg.USEREVENT):  # here we go
        if (event.MyOwnType == ON_MY_MOUSE_CLICK):
            print("My own mouse event!")  # handle for your own event

        elif (event.MyOwnType == ON_MY_SCROLL):
            print("My own scroll event!")  # handle for your own event

        elif (event.MyOwnType == BLAH_BLAH):
            print("My own blah blah event!")  # handle for your own event

EDIT :

If you find yourself need to use pg.time.set_timer, I know we can't assign the MyOwnType to the set_timer. However, you can freely use my set_interval.py from my Github. It's under MIT, so you don't have to worry about general GNU license or something like that...

Here's the example:

from set_interval import setInterval
import pygame as pg

def set_timer(eventObj, interval):
    func = lambda x: pg.event.post(x)
    return setInterval(func=func, sec=interval, args=[eventObj])

ON_MY_MOUSE_CLICK = 1  # your own codes for your own events...
ON_MY_SCROLL = 2
BLAH_BLAH = 3
# etc ...

pg.init()

video = pg.display.set_mode((100,100))

MouseEvent = pg.event.Event(pg.USEREVENT, MyOwnType=ON_MY_MOUSE_CLICK )
ScrollEvent = pg.event.Event(pg.USEREVENT, MyOwnType=ON_MY_SCROLL)
blahblahEvent = pg.event.Event(pg.USEREVENT, MyOwnType=BLAH_BLAH)

# replacement for pygame.time.set_timer(MouseEvent, 1000)
myIntervalHandle1 = set_timer(MouseEvent, 1)

# replacement for pygame.time.set_timer(ScrollEvent, 2500)
myIntervalHandle2 = set_timer(ScrollEvent, 2.5)

running = True
while running :
    for event in pg.event.get():
        if (event.type == pg.QUIT):
            pg.display.quit()
            myIntervalHandle1.stop()  # stop timer
            myIntervalHandle2.stop()  # stop timer
            running = False

        elif (event.type == pg.USEREVENT):  # here we go
            if (event.MyOwnType == ON_MY_MOUSE_CLICK):
                print("My own mouse event!")  # handle for your own event

            elif (event.MyOwnType == ON_MY_SCROLL):
                print("My own scroll event!")  # handle for your own event

            elif (event.MyOwnType == BLAH_BLAH):
                print("My own blah blah event!")  # handle for your own event

You can find the set_interval.py here:

https://github.com/Hzzkygcs/setInterval-python

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top