Skip to main content

Events Handling

Events in a game are the different things that happen during a game. Games are controlled by the user inputs either through mouse or keyboard. Whenever the user gives an input in some way to the game, the game has to react correspondingly according to what the user expects. Handling these events to produce some output is known as Event Handling.

Suppose the user opens a game. The user has option to quit the game any time they want.There are keyboard button controls for playing the game as well as mouse clicks and motions.
Every event is tracked by the pygame and it is our duty to just instruct what to do if that event occurs.

There are many types of events in pygame. Some of them are :


  1. QUIT
  2. ACTIVEEVENT
  3. KEYDOWN
  4. KEYUP
  5. MOUSEMOTION
  6. MOUSEBUTTONUP
  7. MOUSEBUTTONDOWN
  8. VIDEORESIZE
Let me write a code to show how the events are identified in  pygame.

import pygame
pygame.init()
display=pygame.display.set_mode((500,500))
game_loop=False
while not game_loop:
    for event in pygame.event.get():
       print(event)
       pygame.display.update()
pygame.quit()
quit()

We can also directly run the python program directly in the idle window by pressing F5 button.
This program when run, displays all the events that occurs in the game window on the Python shell.
When we click exit button,QUIT event is recognised, but the program does not quit as we have not written any code to handle the QUIT event.Likewise all the events such as key presses all are recognised. The pygame.event.get() gets all the events that can be used in the program.

The events are displayed on the Python shell
In this program, a variable named game_loop is declared and it is given the value as 'False'.
So the game_loop variable is of type boolean. We use this game_loop variable to run a while loop continuously to prevent the pygame window from exiting. This while loop is often called the game loop.

Handling QUIT event :

Now we will see how to handle when a quit event occurs and quit the pygame window.

import pygame
pygame.init()
display=pygame.display.set_mode((500,500))
game_loop=False
while not game_loop:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            game_loop=True
    pygame.display.update()
pygame.quit()
quit()

In this program, we have written code to handle the QUIT event. pygame.event.get() has all the events. In the loop, we check if the event type is a quit event,i.e pygame.QUIT. If the event is a quit event, then game_loop variable is set to True.So when quit event is detected,game_loop is set to True and hence the control comes out of the while loop(i.e the game loop).When the control comes out of the while loop, the next instruction to be executed is the pygame.quit() instruction and the quit() instruction. So the game window exits. Here,when we click the exit button on the top right corner of the pygame window, the game exits.

Thank you
                                            >> Next : Drawing shapes and filling colors >>

Comments

Popular posts from this blog

Python functions and modules

Functions are a block of code that performs the task specified. They either just perform some computation and display the result or returns the result to the main function. In python, functions are defined with the keyword 'def'. The syntax for defining a function in python is : def function_name :       #Block of statements The keyword def is followed by the name of the function. It is a good practise to name the functions meaningfully representing the task done by it. The function name is followed by a colon (:). In python, Comments are represented by the symbol #. So here #Block of statements is commented and hence it will not be executed by the compiler. # is used to comment one line of code. ''' .......''' is used to comment a block of code. For example, #this is a commented line of code. ''' this is a block of commented code Everything in this code will not be looked into by the compiler This is helpful in comment...

Moving Objects

Hi everyone ! In this post, let us see how to move objects on the game screen. In the previous post, we learnt how to draw objects and fill colors. Now we will see how to move those objects from one place to another. To move objects in a game, we need some event to occur. Let us make the object move on pressing the arrow buttons. import pygame pygame.init() white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255) display=pygame.display.set_mode((500,500)) game_loop=False rect_x = 100 rect_y = 100 while not game_loop:     for event in pygame.event.get():         if event.type == pygame.QUIT:             game_loop = True         if event.type == pygame.KEYDOWN :             if event.key == pygame.K_LEFT :                 rect_x -= 10             if event.key...

Drawing shapes And Filling colours

Till now, we have written a python program to open a blank black screen and the QUIT event is handled. In this post, let's see how to add colours to the game surface and draw shapes onto the screen. Filling colours : By default, pygame has no predefined colours. We have to define the colours by giving the RGB(red,green,blue) value of the colour.   import pygame pygame.init() white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255) display=pygame.display.set_mode((500,500)) game_loop=False while not game_loop:     for event in pygame.event.get():         if event.type==pygame.QUIT:             game_loop=True     display.fill(blue)     pygame.display.update() pygame.quit() quit() Here i have defined the basic colours by giving the rgb value of those colors. The values are given as a tuple here. Since white is a mixture o...