函数
- pygame.mouse.get_pressed
- 获取鼠标按钮的状态
- get_pressed(num_buttons=3) -> (button1, button2, button3)
- get_pressed(num_buttons=5) -> (button1, button2, button3, button4, button5)
- pygame.mouse.get_focused
- 检查显示器是否接收鼠标输入
- get_focused() -> bool
- pygame.mouse.get_rel
- 获取鼠标偏移量
- get_rel() -> (x, y)
- pygame.mouse.set_pos
- 设置鼠标光标位置
- set_pos([x, y]) -> None
- pygame.mouse.get_pos
- 获取鼠标光标位置
- get_pos() -> (x, y)
- pygame.mouse.set_visible
- 隐藏或显示鼠标光标
- set_visible(bool) -> bool
- pygame.mouse.get_visible
- 获取鼠标光标的当前可见性状态
- get_visible() -> bool
- pygame.mouse.set_cursor
- 将鼠标光标设置为新光标
- set_cursor(pygame.cursors.Cursor) -> None
- set_cursor(size, hotspot, xormasks, andmasks) -> None
- set_cursor(hotspot, surface) -> None
- set_cursor(constant) -> None
- pygame.mouse.get_cursor
- 获取当前鼠标光标
- get_cursor() -> pygame.cursors.Cursor
示例
import sys
import pygame
import pygame.cursors
from pygame import *
pygame.init()
screen = pygame.display.set_mode((800, 600), flags=RESIZABLE)
pygame.mouse.set_visible(False)
visible = pygame.mouse.get_visible()
print(visible)
pygame.mouse.set_visible(True)
cursor = pygame.mouse.get_cursor()
print(cursor)
pygame.mouse.set_cursor(pygame.cursors.Cursor(SYSTEM_CURSOR_ARROW))
def game_loop():
gameOver = False
gameExit = False
while not gameExit:
while gameOver == True:
for event in pygame.event.get():
pass
for event in pygame.event.get():
print(event)
if event.type == QUIT:
gameExit = True
if event.type == MOUSEBUTTONDOWN:
print('MOUSEBUTTONDOWN')
pos_x, pos_y = event.pos
button = event.button
touch = event.touch
print(pos_x, pos_y)
print(button)
print(touch)
if button == 1:
pygame.mouse.set_cursor(pygame.cursors.Cursor(SYSTEM_CURSOR_HAND))
if button == 2:
pygame.mouse.set_cursor(pygame.cursors.Cursor(SYSTEM_CURSOR_IBEAM))
if button == 3:
pygame.mouse.set_cursor(pygame.cursors.Cursor(SYSTEM_CURSOR_NO))
if event.type == MOUSEBUTTONUP:
print('MOUSEBUTTONUP')
pos_x, pos_y = event.pos
button = event.button
touch = event.touch
print(pos_x, pos_y)
print(button)
print(touch)
pygame.mouse.set_cursor(pygame.cursors.Cursor(SYSTEM_CURSOR_ARROW))
if event.type == MOUSEWHEEL:
print('MOUSEWHEEL')
flipped = event.flipped
x,y = event.x, event.y
precise_x = event.precise_x
precise_y = event.precise_y
touch = event.touch
print(flipped)
print(x, y)
print(precise_x, precise_y)
print(touch)
buttons = pygame.mouse.get_pressed(num_buttons=3)
print(buttons)
if buttons[0]:
pygame.mouse.set_cursor(pygame.cursors.Cursor(SYSTEM_CURSOR_CROSSHAIR))
if buttons[1]:
pygame.mouse.set_cursor(pygame.cursors.Cursor(SYSTEM_CURSOR_SIZEALL))
if buttons[2]:
pygame.mouse.set_cursor(pygame.cursors.Cursor(SYSTEM_CURSOR_WAIT))
if event.type == MOUSEMOTION:
print('MOUSEMOTION')
pos_x, pos_y = event.pos
rel_x, rel_y = event.rel
btn_left,btn_wheel,btn_right = event.buttons
touch = event.touch
print(pos_x, pos_y)
print(rel_x, rel_y)
print(btn_left, btn_wheel, btn_right)
print(touch)
if __name__ == '__main__':
game_loop()
pygame.quit()
sys.exit()