千家信息网

Pygame实现监听鼠标的示例分析

发表于:2024-09-21 作者:千家信息网编辑
千家信息网最后更新 2024年09月21日,Pygame实现监听鼠标的示例分析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。初始化参数import pygame, sysfro
千家信息网最后更新 2024年09月21日Pygame实现监听鼠标的示例分析

Pygame实现监听鼠标的示例分析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

初始化参数

import pygame, sysfrom 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 = 0move_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 = 0mouse_down_x = mouse_down_y = 0mouse_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, sysfrom 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 = 0move_x = move_y = 0mouse_down = mouse_up = 0mouse_down_x = mouse_down_y = 0mouse_up_x = mouse_up_y = 0screen = 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实现监听鼠标的示例分析问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。

鼠标 位置 监听 偏移 分析 事件 字体 按钮 问题 移动 示例 函数 更多 帮助 活动 解答 输出 易行 简单易行 三个 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 数据库打破国外垄断 服务器下架流程 98中华人民共和国网络安全法 人力管理数据库设计er 上海应用软件开发 服务器无法启动怎么办啊 电脑方舟进服务器一直在加载 饥荒联机版打开没有服务器 什么数据库不提供关键词检索 注解怎样返回多张表数据库 网络安全知多少 题目 在线影院服务器 php怎么连接数据库dns 服务器可以雷电多开吗 通达信行情服务器地址 希望之村里面如何进服务器 登录服务器失败请检查防火墙设置 主机屋的数据库初始化失败 数据库技术考试样卷 网络安全对保险行业的影响 对数据库技术有什么问题 网络安全边界最小化原则 数据库的数据怎么显示到安卓界面 服务器正在运行中点击切换到激活 数据库里面新建数据库 通达信行情服务器地址 洛龙区软件开发定制 怎么开发自己的数据库 主机屋的数据库初始化失败 河北邱县网络安全服务零距离
0