Pygame实现监听鼠标示例详解
作者:我的天才女友 时间:2021-12-16 00:42:58
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()
来源:https://blog.csdn.net/qq_40801987/article/details/121948923
标签:Pygame,监听,鼠标
0
投稿
猜你喜欢
python正则表达式re.search()的基本使用教程
2022-03-31 15:46:28
go mod 安装依赖 unkown revision问题的解决方案
2024-05-09 14:59:34
Python机器学习NLP自然语言处理基本操作之Seq2seq的用法
2023-04-03 03:08:54
js实现鼠标切换图片(无定时器)
2023-09-07 02:44:58
python制作定时发送信息脚本的实现思路
2023-01-17 15:24:11
Python tkinter常用操作代码实例
2021-01-05 21:26:09
django的聚合函数和aggregate、annotate方法使用详解
2023-06-27 16:07:46
MySQL性能优化的最佳20+条经验
2024-01-27 15:25:06
mysql简单实现查询结果添加序列号的方法
2024-01-26 11:33:56
为什么是 Python -m
2022-11-25 16:38:21
跟老齐学Python之集合(set)
2023-02-11 00:51:31
Python中字符串对象语法分享
2022-04-19 14:48:34
Python常见异常的处理方式浅析
2022-05-14 17:32:37
python requests抓取one推送文字和图片代码实例
2023-10-26 23:11:16
Tensorflow:转置函数 transpose的使用详解
2021-01-17 20:36:37
使用Python实现BT种子和磁力链接的相互转换
2022-12-21 04:33:25
一文带你学会Python Flask框架设置响应头
2023-04-16 03:11:37
编程活动中几个不良现象
2008-09-01 12:23:00
Asp Response.Expires 属性介绍
2008-02-19 17:02:00
Python列表的索引与切片
2022-11-17 13:06:19