Pygame实现监听鼠标示例详解

 更新时间:2021年12月15日 15:21:17   作者:我的天才女友  
这篇文章主要介绍了通过Pygame模块实现监听鼠标的功能,文章的示例代码讲解详细,对我们的学习或工作有一定的价值,感兴趣的小伙伴可以了解一下

pygame如何捕捉鼠标的活动

初始化参数

import pygame, sys
from pygame.locals import *


def print_text(font, x, y, text, color=(0, 0, 0)):
    """打印字体函数"""
    img_text = font.render(text, True, color)
    screen.blit(img_text, (x, y))


pygame.init()
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption("监听鼠标活动")


while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

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

    pygame.display.update()

鼠标移动

event.type 事件为MOUSEMOTION,则为鼠标移动,event.pos可以获取当前位置,event.rel鼠标的偏移。

        	event.type == MOUSEMOTION:
            event.pos
            event.rel

我们将位置输出出来,定义鼠标的位置和鼠标的偏移量

mouse_x = mouse_y = 0
move_x = move_y = 0
	print_text(font1, 0, 0, "鼠标事件")
    print_text(font1, 0, 20, "鼠标的位置:" + str(mouse_x) + "," + str(mouse_y))
    print_text(font1, 0, 40, "鼠标的偏移:" + str(move_x) + "," + str(move_y))

鼠标点击位置

MOUSEBUTTONDOWN和MOUSEBUTTONUP记录鼠标的按下和放开动作

mouse_down = mouse_up = 0
mouse_down_x = mouse_down_y = 0
mouse_up_x = mouse_up_y = 0

输出鼠标位置及其对用的按钮

pygame.mouse.get_pressed() 可以监听鼠标的三个按键。

    x, y = pygame.mouse.get_pos()
    print_text(font1, 0, 180, "鼠标位置:" + str(x) + "," + str(y))

    b1, b2, b3 = pygame.mouse.get_pressed()
    print_text(font1, 0, 200, "按钮:" + str(b1) + "," + str(b2) + "," + str(b3))

完整代码 

import pygame, sys
from pygame.locals import *


def print_text(font, x, y, text, color=(0, 0, 0)):
    """打印字体函数"""
    img_text = font.render(text, True, color)
    screen.blit(img_text, (x, y))


pygame.init()
# 字体
font1 = pygame.font.SysFont("方正粗黑宋简体", 18)
# 鼠标的移动位置
mouse_x = mouse_y = 0
move_x = move_y = 0
mouse_down = mouse_up = 0
mouse_down_x = mouse_down_y = 0
mouse_up_x = mouse_up_y = 0
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption("监听鼠标活动")


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, "鼠标事件")
    print_text(font1, 0, 20, "鼠标的位置:" + str(mouse_x) + "," + str(mouse_y))
    print_text(font1, 0, 40, "鼠标的偏移:" + str(move_x) + "," + str(move_y))
    print_text(font1, 0, 60, "鼠标按下:" + str(mouse_down)
               + "在" + str(mouse_down_x) + "," + str(mouse_down_y))
    print_text(font1, 0, 80, "鼠标松开:" + str(mouse_up)
               + "在" + str(mouse_up_x) + "," + str(mouse_up_y))
    x, y = pygame.mouse.get_pos()
    print_text(font1, 0, 180, "鼠标位置:" + str(x) + "," + str(y))

    b1, b2, b3 = pygame.mouse.get_pressed()
    print_text(font1, 0, 200, "按钮:" + str(b1) + "," + str(b2) + "," + str(b3))
    pygame.display.update() 

以上就是Pygame实现监听鼠标示例详解的详细内容,更多关于Pygame监听鼠标的资料请关注脚本之家其它相关文章!

相关文章

  • python多进程及通信实现异步任务的方法

    python多进程及通信实现异步任务的方法

    这篇文章主要介绍了python多进程及通信实现异步任务需求,本人也是很少接触多进程的场景,对于python多进程的使用也是比较陌生的。在接触了一些多进程的业务场景下,对python多进程的使用进行了学习,觉得很有必要进行一个梳理总结,感兴趣的朋友一起看看吧
    2022-05-05
  • python中exec函数的实现

    python中exec函数的实现

    exec()是Python内置的一个函数,用于在运行时执行动态生成的Python代码,下面就来介绍一下python中exec函数的实现,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • python图形开发GUI库pyqt5的详细使用方法及各控件的属性与方法

    python图形开发GUI库pyqt5的详细使用方法及各控件的属性与方法

    这篇文章主要介绍了python图形开发GUI库pyqt5的详细使用方法及各控件的属性与方法,需要的朋友可以参考下
    2020-02-02
  • Python实现批量添加视频文本水印

    Python实现批量添加视频文本水印

    这篇文章主要为大家详细介绍了如何基于PyQt5开发一个视频水印批量添加工具,旨在为多个视频文件添加文本水印,感兴趣的小伙伴可以参考一下
    2025-02-02
  • python中urllib模块用法实例详解

    python中urllib模块用法实例详解

    这篇文章主要介绍了python中urllib模块用法,以实例形式详细分析了python中urllib模块代替PHP的curl操作方法,具有不错的借鉴价值,需要的朋友可以参考下
    2014-11-11
  • python多进程读图提取特征存npy

    python多进程读图提取特征存npy

    这篇文章主要为大家详细介绍了python多进程读图提取特征存npy,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • 使用actor-critic方法来控制CartPole-V0 游戏详解

    使用actor-critic方法来控制CartPole-V0 游戏详解

    这篇文章主要为大家介绍了使用actor-critic方法来控制CartPole-V0 游戏详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • 学习Python中一些实用的库

    学习Python中一些实用的库

    这篇文章主要介绍了Python学习之盘点一些Python中实用的库,有需要的同学可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2021-09-09
  • python基于itchat模块实现微信防撤回

    python基于itchat模块实现微信防撤回

    这篇文章主要为大家详细介绍了python实现微信防撤回,基于itchat模块,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • Django启动时找不到mysqlclient问题解决方案

    Django启动时找不到mysqlclient问题解决方案

    这篇文章主要介绍了Django启动时找不到mysqlclient问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11

最新评论