Python: #3 Handling the events in pygame🔥

Hi all,
Today in this article we are going to continue our pygame series. In last article we had created the game loop but there was one problem, the screen that we made was not responding. To solve that problem we will handle it in today’s article.
Those who has not read mine last articles on pygame they can visit pygame series page –https://knowledge-junction.in/dashboard/python-pygame-series/
Now let’s start
import pygame
initilize_pygame_module = pygame.init()
game_display = pygame.display.set_mode((900,600))
title = pygame.display.set_caption("My first Python game")
# Specific game variables
exit_game = False
game_over = False
# Now we will handle this event
while not exit_game:
# pygame.event.get will print the events that we will perform on the screen
for event in pygame.event.get():
# Printing the event for the testing
print(event)
Test –

When we move the cursor it prints the position and some other information.
Now we can easily move the screen and the screen is also responding. But the problem is that when we will try to close the screen it will not.
import pygame
initilize_pygame_module = pygame.init()
game_display = pygame.display.set_mode((900,600))
title = pygame.display.set_caption("My first Python game")
# Specific game variables
exit_game = False
game_over = False
# Now we will handle this event
while not exit_game:
# pygame.event.get will print the events that we will perform on the screen
for event in pygame.event.get():
# Now here we don't need print statement it was just for testing.
# print(event)
#'''If the user presses close button then our game variable
# which was false before prsessing the close button will be True'''
# Now we will able to quit the game
if event.type==pygame.QUIT:
exit_game = True
In next article we will discuss how to detect the key if we press right arrow key then it will print the string we will also see how to control the keys how to move the characters using the keys
So be with our pygame series if you have not read last pygame articles the link is given you can visit pygame series page and read the articles.
Thank you 🙂
Have a nice day!!
You must log in to post a comment.