In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 224
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.