Example 1: pygame detect click
while ... # your main loop
# get all events
ev = pygame.event.get()
# proceed events
for event in ev:
# handle MOUSEBUTTONUP
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
# get a list of all sprites that are under the mouse cursor
clicked_sprites = [s for s in sprites if s.rect.collidepoint(pos)]
# do something with the clicked sprites...
Example 2: pygame get mouse position
x,y = pygame.mouse.get_pos()
#get the mouse cursor position
#get_pos() -> (x, y)
#Returns the X and Y position of the mouse cursor.
#The position is relative to the top-left corner of the display.
#The cursor position can be located outside of the display window,
#but is always constrained to the screen.
Example 3: event.type == pygame.MOUSEBUTTONDOWN click
def handle_event(self, screen, event, player):
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEMOTION:
pygame.draw.rect(screen, BACKGROUND, (0,0, self.screen_width, SQUARESIZE))
posx = event.pos[0]
if player == 1:
pygame.draw.circle(screen, P1STONE, (posx, int(SQUARESIZE/2)), RADIUS)
else:
pygame.draw.circle(screen, P2STONE, (posx, int(SQUARESIZE/2)), RADIUS)
pygame.display.update()
return None
if event.type == pygame.MOUSEBUTTONDOWN:
pygame.draw.rect(screen, BACKGROUND, (0,0, self.screen_width, SQUARESIZE))
posx = event.pos[0]
action = int(np.floor(posx/SQUARESIZE))
return action
Example 4: how to pring events in pygame
import pygame
pygame.init()
screen = pygame.display.set_mode((500,300))
thing = 0
while thing < 100000000:
thing += 1
event = pygame.event.get()
print (event)
pygame.quit()
Example 5: pygame events
QUIT none
ACTIVEEVENT gain, state
KEYDOWN key, mod, unicode, scancode
KEYUP key, mod
MOUSEMOTION pos, rel, buttons
MOUSEBUTTONUP pos, button
MOUSEBUTTONDOWN pos, button
JOYAXISMOTION joy (deprecated), instance_id, axis, value
JOYBALLMOTION joy (deprecated), instance_id, ball, rel
JOYHATMOTION joy (deprecated), instance_id, hat, value
JOYBUTTONUP joy (deprecated), instance_id, button
JOYBUTTONDOWN joy (deprecated), instance_id, button
VIDEORESIZE size, w, h
VIDEOEXPOSE none
USEREVENT code