pygame如何捕捉鼠标的活动,方法是什么
Admin 2022-05-21 群英技术资讯 859 次浏览
这篇文章主要介绍“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 OpenCV实现基本图形绘制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要介绍了python返回函数和匿名函数的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
怎样用Python实现文本的滚动播放?对于文本滚动播放的应用场景有很多,我们经常能在网站的顶部看到,文本滚动播放功能也是比较实用的,下面我们就来看看用Python怎样写文本的滚动播放功能。
这篇文章给大家分享的是有关pytorch实现变量类型转换的内容,其实变量类型转换的方法有很多,因此下面小编和大家一起探究变量类型转换的方法有哪些?怎样实现?感兴趣的朋友就继续往下看吧。
读万卷书不如行万里路,只学书上的理论是远远不够的,只有在实战中才能获得能力的提升,本篇文章手把手带你用Python开发一套校园管理系统,包含各种人员,如教师、学生等。学校的系统通常还包括一些课程的信息,大家可以在过程中查缺补漏,提升水平
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008