Introduction
We know that the gaming industry is one of the major developing industries in the current world using current developing technologies. And on the parallel side, Python is also one of the famous programming languages which are stretching its hands in all the fields due to its simplicity and a full-fledged pack of libraries. Gaming is really a fun way of learning things. Game programming also includes many concepts such as math, computer programming, logic development, and many more. In the current developing technology, AI is also showing some support for game programming. Python can be used for Game Programming by using the Pygame library. Pygame library is one of the python programming libraries which has very efficient functionalities such as building custom events, adding some logical context, etc. This article will try to discuss how to create and add custom events to our pygame application. Hope You will enjoy the article!
Adding Custom Events
A custom event is an event that has the user logic that will be executed when an action is performed in that application. These events are user-defined events and thus need to be implemented by the user itself. But after creating an event, to implement that event in our application, the language needs to support its implementation. Pygame uses Python as a programming language and thus provides many facilities to add. So far, we have discussed the custom events; now, we will learn how to create and add custom events.
We know that many built-in events are supported by Pygame, such as KEYDOWN, ONCLICK, etc... Along with that, it also supports custom events to ensure flexibility in our application.
The syntax for creating custom events:
Event_name = pygame.USEREVENT+1
Example: Event1 = pygame.USEREVENT+1
From these, we can able to create custom events by using the above syntax. In order to implement or integrate these custom events with our application, we need to use
- pygame.event.post(): method to post the custom event,
- pygame.time .set_timer(): method to make the event done in particular time intervals. And this method is optional.
- pygame.event.post(): We can able to post our created custom events using this event.post() method.This post method will take one Event type as a parameter. What this method does is it will basically add our event to the end of the queue of events. And then, it will perform two steps in order to post the events.
Step1: Converting event to the pygame event type.
Example: ADD_event = pygame.event.Event(event)
Step2: Post the converted event using post() method.
Example: pygame.event.post(ADD_event)
pygame.time.set_timer(): Making the created custom event execute periodically by using pygame timers. set_timer() method allows us to execute an event for a particular duration. This method as its definition suggests will take an event and duration in ms as its parameters. This method will take the event attribute without considering it as an event type or not.
The syntax of the set_timer() method is as follows:
pygame.time.set_timer(event, duration)
Example:
The actual flow of creating and adding custom events is like,
- Create a plot with custom events first.
- Create an event.
- Convert it to Pygame event datatype.
- Add code for your operations that will generate custom events.
- Finally, post the custom event.
Please refer to the below link for a code example.