Pygame 简明教程

Pygame - Mouse Events

Pygame 识别三个鼠标事件,即 MOUSEMOTION、MOUSEBUTTONUP 和 MOUSEBUTTONDOWN。相应的事件对象返回鼠标按下/释放时所在的坐标和按钮号。

Pygame recongnizes three mouse events, namely, MOUSEMOTION, MOUSEBUTTONUP, and MOUSEBUTTONDOWN. The corresponding event object returns the coordinates of position at which mouse is pressed/released and the button number.

例如,MOUSEBUTTONDOWN 事件对象将显示以下结果 −

For example, a MOUSEBUTTONDOWN event object will display following result −

<Event(1025-MouseButtonDown {'pos': (398, 328), 'button': 1, 'window': None})>

Example

要获取按钮按下时所在的坐标,可以使用与事件对象关联的 get_pos() 函数。

To obtain the coordinates of position of button down, we can use get_pos() function associated with event object.

import pygame, sys

pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Hello World")
while True:
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         pygame.quit()
         sys.exit()
      if event.type == pygame.MOUSEBUTTONDOWN:
         pos=pygame.mouse.get_pos()
         btn=pygame.mouse
         print ("x = {}, y = {}".format(pos[0], pos[1]))

Output

运行以上代码,在游戏窗口任意位置按下鼠标按钮。

Run above code and press the mouse button at random positions on the game window.

x = 192, y = 160
x = 419, y = 245
x = 204, y = 405
x = 449, y = 17
x = 12, y = 15

MOUSEMOTION 事件对象捕捉正在移动的鼠标位置的即时位置。

The MOUSEMOTION event object captures instantaneous position of moving mouse location.

if event.type == pygame.MOUSEMOTION:
   pos=event.pos
   print ("x = {}, y = {}".format(pos[0], pos[1]))

pygame.mouse 模块中的其他重要函数和属性如下 −

Other important functions and attributes in pygame.mouse module are as follows −

pygame.key.get_pressed

get the state of the mouse buttons

pygame.mouse.get_pos

get the mouse cursor position

pygame.mouse.get_rel

get the amount of mouse movement

pygame.mouse.set_pos

set the mouse cursor position

pygame.mouse.set_visible

hide or show the mouse cursor

pygame.mouse.get_visible

get the current visibility state of the mouse cursor

pygame.mouse.get_focused

check if the display is receiving mouse input

pygame.mouse.set_cursor

set the image for the mouse cursor

pygame.mouse.set_system_cursor

set the mouse cursor to a system variant

Pygame 定义了以下系统光标 −

Pygame defines following system cursors −

pygame.SYSTEM_CURSOR_ARROW

arrow

pygame.SYSTEM_CURSOR_IBEAM

i-beam

pygame.SYSTEM_CURSOR_WAIT

wait

pygame.SYSTEM_CURSOR_CROSSHAIR

crosshair

pygame.SYSTEM_CURSOR_SIZENWSE

double arrow pointing northwest and southeast

pygame.SYSTEM_CURSOR_SIZENESW

double arrow pointing northeast and southwest

pygame.SYSTEM_CURSOR_SIZEWE

double arrow pointing west and east

pygame.SYSTEM_CURSOR_SIZENS

double arrow pointing north and south

pygame.SYSTEM_CURSOR_SIZEALL

four pointed arrow

pygame.SYSTEM_CURSOR_NO

slashed circle or crossbones

pygame.SYSTEM_CURSOR_HAND

hand

以下语句将游戏窗口光标设置为十字光标。

Following statement will set the game window cursor to crosshair.

pygame.mouse.set_system_cursor(pygame.SYSTEM_CURSOR_CROSSHAIR)