events in python

Introducing events in pygame module for python #2

Any interaction with the pygame window is recognized as pygame event.you change the position of your cursor that is event, if you have joystick connected and you press some buttons thats event, you use your keyboard that is also recognized as event in python. events that are recognized enter to event queue. pygame will recognize events only after  event device is initialized and Another important thing to remember is that  event module is  highly dependent upon pygame display module . if pygame display module is not initialized then event queue will not work.

here is the simple example


import pygame

pygame.init() #returns a tuple if printed

gameMode = pygame.display.set_mode([600,600])
pygame.display.set_caption("gameEvent")


gameExit = False
while not gameExit:
	for event in pygame.event.get():
		print(event)
	


pygame.quit()

if you are not familiar with basic structure of python program that utilizes pygame , i suggest you read :

https://logicaletter.com/2018/03/05/basic-structure-of-pygame-2/

if you have already covered its basic structure then we can take a closer look at


gameExit = False
while not gameExit:
	for event in pygame.event.get():
		print(event)

while gameExit value is not true . event will print values sent by pygame.event.get()  .

pygame.event.get() gets event from the queue.

if you run the program you get something like this :

events in python
event in python

if you try to click close button on pygame window, it wont close by itself so lets try to remove that error by adding small functionality

 


gameExit = False
while not gameExit:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			gameExit = True

pygame.quit()


while pygame.event.get() returns events from event queue. we have to check if event type is QUIT button or not, so we have to check the type using event.type. there are multiple event types such as

Event type 
QUIT none
KEYDOWNunicode,key,mode
KEYUPkey,mod
MOUSEMOTION pos, rel, buttons
MOUSEBUTTONUP pos, button
MOUSEBUTTONDOWN pos, button
JOYBUTTONUP joy, button
JOYBALLMOTION joy, ball, rel
JOYAXISMOTION
joy, axis, value
JOYBUTTONDOWN joy, button
VIDEORESIZE size, w, h
VIDEOEXPOSE none
JOYHATMOTIONjoy,hat value
USEREVENT code

There will be separate tutorial defining what each event will do but in this tutorial only some of them such as keyup,keydown will be discussed. so if you look at element keydown on table you will see parameters taken as

  • mod
  • key
  • unicode

These are the values that are taken by pygame to understand what is happening and which key is pressed. you dont have to worry about passing it by yourself. when you press any key , it is automatically passed. you can try printing events and you would see

event parameters being printed
event parameters being printed

Lets make our program  little bit more awsome. lets add a rectangle into game and  up,down,left,right will control the rectangle.

 


import pygame

pygame.init() #returns a tuple if printed

gameMode = pygame.display.set_mode([600,600])
pygame.display.set_caption("hello")
white=(255,255,255) #colors in form of RGB [red,green,blue] .these color combination are primary colors and can build up anything maximum is 255 for each color 
green=(0,255,0)
#variables for rectangle
width=20 
height=20
#position of rectangle on screen .
x=300
y=300


gameExit = False
while not gameExit:
	gameMode.fill(white)
	pygame.draw.rect(gameMode,green,[x,y,width,height])

	for event in pygame.event.get():
		
		if event.type == pygame.QUIT:
			gameExit = True
		if event.type == pygame.KEYDOWN:
			if event.key == pygame.K_LEFT:
				x-=10
			if event.key == pygame.K_RIGHT:
				x+=10
			if event.key == pygame.K_UP:
				y-=10
			if event.key == pygame.K_DOWN:
				y+=10


	pygame.display.update()

pygame.quit()


This is Ashish Thapa. It's just a name after all. Just had a thought that our names should signify our situation and should be dynamic. What if our name upgraded with respect to experiences acquired.

Leave a Comment

Your email address will not be published. Required fields are marked *