iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Pygame实现监听鼠标的示例分析
  • 191
分享到

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

2023-06-22 02:06:34 191人浏览 薄情痞子
摘要

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

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()

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

鼠标移动

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

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

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

mouse_x = mouse_y = 0move_x = move_y = 0print_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))

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

鼠标点击位置

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

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

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

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

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))

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()# 字体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实现监听鼠标的示例分析问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注编程网精选频道了解更多相关知识。

--结束END--

本文标题: Pygame实现监听鼠标的示例分析

本文链接: https://www.lsjlt.com/news/301978.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • c++中if elseif使用规则
    c++ 中 if-else if 语句的使用规则为:语法:if (条件1) { // 执行代码块 1} else if (条件 2) { // 执行代码块 2}// ...else ...
    99+
    2024-05-14
    c++
  • c++中的继承怎么写
    继承是一种允许类从现有类派生并访问其成员的强大机制。在 c++ 中,继承类型包括:单继承:一个子类从一个基类继承。多继承:一个子类从多个基类继承。层次继承:多个子类从同一个基类继承。多层...
    99+
    2024-05-14
    c++
  • c++中如何使用类和对象掌握目标
    在 c++ 中创建类和对象:使用 class 关键字定义类,包含数据成员和方法。使用对象名称和类名称创建对象。访问权限包括:公有、受保护和私有。数据成员是类的变量,每个对象拥有自己的副本...
    99+
    2024-05-14
    c++
  • c++中优先级是什么意思
    c++ 中的优先级规则:优先级高的操作符先执行,相同优先级的从左到右执行,括号可改变执行顺序。操作符优先级表包含从最高到最低的优先级列表,其中赋值运算符具有最低优先级。通过了解优先级,可...
    99+
    2024-05-14
    c++
  • c++中a+是什么意思
    c++ 中的 a+ 运算符表示自增运算符,用于将变量递增 1 并将结果存储在同一变量中。语法为 a++,用法包括循环和计数器。它可与后置递增运算符 ++a 交换使用,后者在表达式求值后递...
    99+
    2024-05-14
    c++
  • c++中a.b什么意思
    c++kquote>“a.b”表示对象“a”的成员“b”,用于访问对象成员,可用“对象名.成员名”的语法。它还可以用于访问嵌套成员,如“对象名.嵌套成员名.成员名”的语法。 c++...
    99+
    2024-05-14
    c++
  • C++ 并发编程库的优缺点
    c++++ 提供了多种并发编程库,满足不同场景下的需求。线程库 (std::thread) 易于使用但开销大;异步库 (std::async) 可异步执行任务,但 api 复杂;协程库 ...
    99+
    2024-05-14
    c++ 并发编程
  • 如何在 Golang 中备份数据库?
    在 golang 中备份数据库对于保护数据至关重要。可以使用标准库中的 database/sql 包,或第三方包如 github.com/go-sql-driver/mysql。具体步骤...
    99+
    2024-05-14
    golang 数据库备份 mysql git 标准库
  • 如何在 Golang 中优雅地处理错误?
    在 go 中,优雅处理错误包括:使用 error 类型;使用 errors 包函数和类型;自定义错误类型;遵循错误处理模式,包括关闭资源、检查错误、打印错误信息和处理或返回错误。 在 ...
    99+
    2024-05-14
    golang 错误处理
  • 如何构建 Golang RESTful API,并使用中间件进行身份验证?
    本文介绍了如何构建 golang restful api。首先,通过导入必要的库、定义数据模型和创建路由来构建 restful api。其次,使用 go-chi/chigot 和 go-...
    99+
    2024-05-14
    golang git
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作