Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Monitoring Mouse by Pygame

Shulou Source: shulou.com Published: 2022-06-02 06:34:35 09月23日 Update

Pygame implements the example analysis of monitoring mouse. In view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Initialization parameters import pygame, sysfrom pygame.locals import * def print_text (font, x, y, text, color= (0,0,0): "" print font function "" img_text = font.render (text, True, color) screen.blit (img_text, (x, y) pygame.init () screen = pygame.display.set_mode Pygame.display.set_caption ("listen for mouse activity") while True: for event in pygame.event.get (): if event.type = = QUIT: pygame.quit () sys.exit () screen.fill ((255,255,255)) pygame.display.update ()

Mouse movement

The event.type event is MOUSEMOTION, then the mouse is moved, and the event.pos can get the current position, the offset of the event.rel mouse.

Event.type = = MOUSEMOTION: event.pos event.rel

We output the position to define the position of the mouse and the offset of the mouse

Mouse_x = mouse_y = 0move_x = move_y = 0 print_text (font1, 0,0, "mouse event") print_text (font1, 0,20, "mouse position:" + str (mouse_x) + "," + str (mouse_y)) print_text (font1, 0,40, "mouse offset:" + str (move_x) + "," + str (move_y)

Mouse click location

MOUSEBUTTONDOWN and MOUSEBUTTONUP record mouse press and release actions

Mouse_down = mouse_up = 0mouse_down_x = mouse_down_y = 0mouse_up_x = mouse_up_y = 0

Output mouse position and corresponding buttons

Pygame.mouse.get_pressed () can listen for the three mouse buttons.

X, y = pygame.mouse.get_pos () print_text (font1, 0180, "mouse position:" + str (x) + "," + str (y)) b1, b2, b3 = pygame.mouse.get_pressed () print_text (font1, 0200, "button:" + str (b1) + "," + str (b2) + "," + str (b3))

Complete code import pygame, sysfrom pygame.locals import * def print_text (font, x, y, text, color= (0,0,0)): "" print font function "" img_text = font.render (text, True, color) screen.blit (img_text, (x, y)) pygame.init () # font font1 = pygame.font.SysFont ("founder bold black Song simplified" 18) # the moving position of the mouse mouse_x = mouse_y = 0move_x = move_y = 0mouse_down = mouse_up = 0mouse_down_x = mouse_down_y = 0mouse_up_x = mouse_up_y = 0screen = pygame.display.set_mode Pygame.display.set_caption ("listen for mouse activity") while True: for event in pygame.event.get (): if event.type = = QUIT: pygame.quit () sys.exit () elif event.type = = MOUSEMOTION: mouse_x, mouse_y = event.pos move_x Mouse_y = event.rel elif event.type = = MOUSEBUTTONDOWN: mouse_down = event.button mouse_down_x, mouse_down_y = event.pos elif event.type = = MOUSEBUTTONUP: mouse_up = event.button mouse_up_x, mouse_up_y = event.pos screen.fill ((255,255,255)) print_text (font1, 0,0 "mouse events") print_text (font1, 0,20, "mouse position:" + str (mouse_x) + "," + str (mouse_y)) print_text (font1, 0,40, "mouse offset:" + str (move_x) + "," + str (move_y)) print_text (font1, 0,60) "Mouse Down:" + str (mouse_down) + "in" + str (mouse_down_x) + "," + str (mouse_down_y) print_text (font1, 0,80, "Mouse release:" + str (mouse_up) + "in" + str (mouse_up_x) + "," + str (mouse_up_y)) x Y = pygame.mouse.get_pos () print_text (font1, 0180, "Mouse position:" + str (x) + "," + str (y)) b1, b2, b3 = pygame.mouse.get_pressed () print_text (font1, 0200, "Button:" + str (b1) + "," + str (b2) + " "+ str (b3)) pygame.display.update () this is the answer to the sample analysis question about the Pygame implementation of monitoring the mouse. I hope the above content can help you to a certain extent, if you still have a lot of doubts to be solved, you can follow the industry information channel to learn more related knowledge.

Tags: Mouse location monitoring offset analysis event font button question move example function more help activity solution output easy easy three Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB vpn OPPO Reno Docker