Pygame编程(6)mouse模块

Pygame编程(6)mouse模块

函数

  • 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.get_pressed()
# pygame.mouse.get_focused()
# pygame.mouse.get_pos()
# pygame.mouse.get_rel()
# pygame.mouse.set_pos()

pygame.mouse.set_visible(False)
visible = pygame.mouse.get_visible()
print(visible)
pygame.mouse.set_visible(True)

# pygame.mouse.set_cursor()
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:
                # <Event(1025-MouseButtonDown {'pos': (642, 70), 'button': 1, 'touch': False, 'window': None})>
                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')
                # <Event(1026-MouseButtonUp {'pos': (771, 4), 'button': 3, 'touch': False, 'window': None})>
                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')
                # 上滚 <Event(1027-MouseWheel {'flipped': False, 'x': 0, 'y': 1, 'precise_x': 0.0, 'precise_y': 1.0, 'touch': False, 'window': None})>
                # 下滚 <Event(1027-MouseWheel {'flipped': False, 'x': 0, 'y': -1, 'precise_x': 0.0, 'precise_y': -1.0, 'touch': False, 'window': None})>
                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')
                # <Event(1024-MouseMotion {'pos': (771, 4), 'rel': (1, -1), 'buttons': (1, 0, 1), 'touch': False, 'window': None})>
                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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SongYuLong的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值