pygame如何捕捉鼠标的活动,方法是什么
Admin 2022-05-21 群英技术资讯 1002 次浏览
这篇文章主要介绍“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()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
文本给大家分享的关于python实现反转字符串的技巧,这次我们要实现的效果是,将给定的一个字符串中的每个单词,做逐个翻转。那么具体怎么实现呢?下面我们一起来看看。
在列表,元组,实例,类,字典和函数中存在循环引用问题。有 __del__ 方法的实例会以健全的方式被处理。给新类型添加GC支持是很容易的。
Python单继承是很学习Python很重要的知识,因此这篇文章主要给大家分享Python单继承的内容,对新手学习Python有一定的借鉴价值,感兴趣的朋友可以参考一下,下面我们一起来学习一下吧。
这篇文章主要介绍了Python和JS反爬之解决反爬参数 signKey,Python 反爬中有一大类,叫做字体反爬,核心的理论就是通过字体文件或者 CSS 偏移,接下来文章的详细介绍,需要的小伙伴可以参考一下
在日常服务器运维工作中,备份数据库是必不可少的,刚工作那会看到公司都是用shell脚本循环备份数据库,到现在自己学习python语言后,利用多进程多线程相关技术来实现并行备份数据库,充分利用服务器资源,提高备份速度。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008