How to monitor mouse movements in Pygame

Method to monitor keyboard keys in Pygame_pygame gets keyboard input-CSDN blog mentioned that keyboard actions are monitored by getting events in the queue in the while True loop. Monitoring mouse movements is the same as monitoring keyboard movements.

Related connection 1 For related knowledge about queues and events, please refer to

How to monitor keyboard keys in Pygame_pygame gets keyboard input-CSDN Blog

1 Classification of mouse events

Mouse events in the queue are mainly divided into three categories: move, press and release. The values ​​corresponding to these three events are MOUSEMOTION, MOUSEBUTTONDOWN and MOUSEBUTTONUP respectively.

2. Handling of mouse movement events

By processing mouse movement events, the function of real-time display of mouse coordinates is realized, as shown in Figure 1.

Figure 1 Real-time display of mouse coordinates

In the while True loop, the event is first obtained from the queue through pygame.event.get(), and then the event type is judged through the if statement. The code is as follows.

if event.type == MOUSEMOTION:

At this time, it means that the mouse is moving on the screen. At this time, the screen is first filled, covering the previously displayed coordinates.

screen.fill((0, 0, 255))

After that, construct the coordinates to be displayed.

coordinate = '(%d,%d)'%event.pos

Among them, event.pos is a tuple that contains the current abscissa and ordinate of the mouse; finally, the constructed mouse coordinates are displayed on the screen.

imgText = font.render(coordinate, True, (255,255,255))
screen.blit(imgText,event.pos)
pygame.display.update()

Related links 2 How to use font, please refer to

How to monitor keyboard keys in Pygame_pygame gets keyboard input-CSDN Blog

3 Mouse press and release events

By monitoring mouse press and release events, the function of drawing lines on the screen can be realized. That is, draw a straight line with the mouse pressed as the starting point and released as the end point, as shown in Figure 2.

Figure 2 Screen drawing

When the mouse is pressed, the corresponding event is MOUSEBUTTONDOWN, and the mouse coordinates are saved as the starting point of the straight line.

line_start = event.pos

When the mouse is released, the corresponding event is MOUSEBUTTONUP. At this time, the mouse coordinates are saved as the end point of the straight line. According to the starting point and end point, the line is drawn on the screen through the pygame.draw.line() method.

line_end = event.pos
pygame.draw.line(screen, (255, 0, 0), line_start, line_end)
pygame.display.update()

Among them, the first parameter of the pygame.draw.line() method represents the specified screen, the second parameter represents the color of the line; the third and fourth parameters represent the starting point and end point of the line respectively.

4 complete code

The complete code mentioned above is shown below.

import sys
import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((500, 200))
screen.fill((0, 0, 255))
pygame.display.update()
font = pygame.font.Font(None, 20)
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        if event.type == MOUSEMOTION:
            screen.fill((0, 0, 255))
            coordinate = '(%d,%d)'%event.pos
            imgText = font.render(coordinate, True, (255,255,255))
            screen.blit(imgText,event.pos)
            pygame.display.update()
        if event.type == MOUSEBUTTONDOWN:
            line_start = event.pos
        if event.type == MOUSEBUTTONUP:
            line_end = event.pos
            pygame.draw.line(screen, (255, 0, 0), line_start, line_end)
            pygame.display.update()

Guess you like

Origin blog.csdn.net/hou09tian/article/details/133386530