Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

How to add Custom Events in Pygame?

  • Difficulty Level : Medium
  • Last Updated : 17 Jun, 2021

In this article, we will see how to add custom events in PyGame


PyGame library can be installed using the below command:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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.


<event_name> = pygame.USEREVENT + 1


# Here ADDITION and SUBTRACTION is the event name



Now, how do we publish our custom events once they are created? This can be done in two ways:

  • Using method.
  • Using pygame.time.set_timer() method.

Using method

We can directly post our events using 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.


# Step 1 – Convert event into event datatype of pygame 

ADD_event = pygame.event.Event(event)

# Step 2 – Post the 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.


# 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 : 


# Python program to add Custom Events
import pygame
# 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
# custom user event to change color
# 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:
                bg_active_color = WHITE
            elif bg_active_color == 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
                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()):
    # Drawing rectangle on the screen
    pygame.draw.rect(screen, RED, box)
    # Updating Screen
    # Setting Frames per Second

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.  

My Personal Notes arrow_drop_up
Recommended Articles
Page :