Pygame allows us to get the mouse input with the pygame.mouse module. Below are three pygame.mouse module methods that we will often use to find the mouse input.
1) pygame.mouse.get_pressed will returns a tuple contains three 0 or 1 values for the left, middle, and right mouse buttons. If the button is pressed then the value will become 1 or else it will become 0.
2) pygame.mouse.get_pos will return the mouse coordinates as a tuple of the x and y values.
3) pygame.mouse.get_rel will return the relative mouse movement (or mickeys) as a tuple with the x and y relative movement.
Below script will print out the x and y coordinate of the mouse on the screen with the pygame.mouse.get_pos method.
#!/usr/bin/env python import pygame from pygame.locals import * from sys import exit pygame.init() screen = pygame.display.set_mode((640, 480), 0, 32) font = pygame.font.SysFont("arial", 17); font_height = font.get_linesize() while True: for event in pygame.event.get(): if event.type == QUIT: exit() screen.fill((255, 255, 255)) x, y = pygame.mouse.get_pos() text_surface = font.render("x coordinate : " + str(x) + " y coordinate : " + str(y), True, (0,0,0)) screen.blit(text_surface, (10, font_height)) pygame.display.update()
Mouse is more wonderful as compared to keyboard so make sure you have included the mouse factor in your game design. This article will conclude the keyboard and mouse chapter and now we are ready to move on into other chapter in Pygame.