In this article, we will see how to add custom events in PyGame.
Installation
PyGame library can be installed using the below command:
pip install pygame
Although PyGame comes with a set of events (Eg: KEYDOWN and KEYUP), it allows us to create our own additional custom events according to the requirements of our game. Custom events increase the control and flexibility we have over our game. A custom event is the same as creating a User-defined event.
Syntax:
<event_name> = pygame.USEREVENT + 1
Example:
# Here ADDITION and SUBTRACTION is the event name
ADDITION = pygame.USEREVENT + 1
SUBTRACTION = pygame.USEREVENT + 2
Now, how do we publish our custom events once they are created? This can be done in two ways:
- Using pygame.event.post() method.
- Using pygame.time.set_timer() method.
Using pygame.event.post() method
We can directly post our events using pygame.event.post() method. This method adds our event to the end of the events on the queue. In order to execute this, we need to convert our event to Pygame’s event type inorder to match the attributes of the post method and avoid errors.
Syntax:
# Step 1 – Convert event into event datatype of pygame
ADD_event = pygame.event.Event(event)
# Step 2 – Post the event
pygame.event.post(ADD_event) # event_name as parameter
Using pygame.time.set_timer() method
Broadcasting the event periodically by using PyGame timers. Here, we’ll be using another method to publish the event by using set_timer() function, which takes two parameters, a user event name and time interval in milliseconds.
Syntax:
# event_name, time in ms
pygame.time.set_timer(event, duration)
Note: In this, we don’t need to convert the user-defined event into PyGame event datatype.
Now to create a plot with custom events firstly the attributes for the screen should be set as per requirement. Then create an event and convert it to PyGame event datatype. Now add code for your operations that will generate a custom event.
In the given implementation both of the approaches have been handled.
Program :
Python3
# Python program to add Custom Events import pygame pygame.init() # Setting up the screen and timer screen = pygame.display.set_mode(( 500 , 500 )) timer = pygame.time.Clock() # set title pygame.display.set_caption( 'Custom Events' ) # defining colours WHITE = ( 255 , 255 , 255 ) RED = ( 255 , 0 , 0 ) GREEN = ( 0 , 255 , 0 ) BLUE = ( 0 , 0 , 255 ) # Keep a track of active variable bg_active_color = WHITE screen.fill(WHITE) # custom user event to change color CHANGE_COLOR = pygame.USEREVENT + 1 # custom user event to inflate defalte # box ON_BOX = pygame.USEREVENT + 2 # creating Rectangle box = pygame.Rect(( 225 , 225 , 50 , 50 )) grow = True # posting a event to switch color after # every 500ms pygame.time.set_timer(CHANGE_COLOR, 500 ) running = True while running: # checks which all events are posted # and based on that perform required # operations for event in pygame.event.get(): # switching colours after every # 500ms if event. type = = CHANGE_COLOR: if bg_active_color = = GREEN: screen.fill(GREEN) bg_active_color = WHITE elif bg_active_color = = WHITE: screen.fill(WHITE) bg_active_color = GREEN if event. type = = ON_BOX: # to inflate and deflate box if grow: box.inflate_ip( 3 , 3 ) grow = box.width < 75 else : box.inflate_ip( - 3 , - 3 ) grow = box.width < 50 if event. type = = pygame.QUIT: # for quitting the program running = False # Posting event when the cursor is on top # of the box if box.collidepoint(pygame.mouse.get_pos()): pygame.event.post(pygame.event.Event(ON_BOX)) # Drawing rectangle on the screen pygame.draw.rect(screen, RED, box) # Updating Screen pygame.display.update() # Setting Frames per Second timer.tick( 30 ) pygame.quit() |
Output :
In the above implementation, we have used the .post() method to inflate/deflate the box when the cursor is on the top of the box and .set_timer() method to switch background color after every 500ms.