京公网安备 11010802034615号
经营许可证编号:京B2-20210330
Python中pygame的mouse鼠标事件用法实例
这篇文章主要介绍了Python中pygame的mouse鼠标事件用法,以完整实例形式详细分析了pygame响应鼠标事件的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下具体如下:
pygame.mouse提供了一些方法获取鼠标设备当前的状态
'''
pygame.mouse.get_pressed - get the state of the mouse buttons get the state of the mouse buttons
pygame.mouse.get_pos - get the mouse cursor position get the mouse cursor position
pygame.mouse.get_rel - get the amount of mouse movement get the amount of mouse movement
pygame.mouse.set_pos - set the mouse cursor position set the mouse cursor position
pygame.mouse.set_visible - hide or show the mouse cursor hide or show the mouse cursor
pygame.mouse.get_focused - check if the display is receiving mouse input check if the display is receiving mouse input
pygame.mouse.set_cursor - set the image for the system mouse cursor set the image for the system mouse cursor
pygame.mouse.get_cursor - get the image for the system mouse cursor get the image for the system mouse cursor
'''
在下面的demo中,主要用到了:
pygame.mouse.get_pressed()
pygame.mouse.get_pos()
展示的效果:
游戏效果:
当鼠标经过窗口的时候,窗口背景颜色会随着鼠标的移动而发生改变,当鼠标点击窗口
会在控制台打印出是鼠标的那个键被点击了:左,右,滚轮
#pygame mouse
import os, pygame
from pygame.locals import *
from sys import exit
from random import *
__author__ = {'name' : 'Hongten',
'mail' : 'hongtenzone@foxmail.com',
'Version' : '1.0'}
if not pygame.font:print('Warning, Can not found font!')
pygame.init()
screen = pygame.display.set_mode((255, 255), 0, 32)
screen.fill((255,255,255))
font = pygame.font.Font('data\\font\\TORK____.ttf', 20)
text = font.render('Cliked Me please!!!', True, (34, 252, 43))
mouse_x, mouse_y = 0, 0
while 1:
for event in pygame.event.get():
if event.type == QUIT:
exit()
elif event.type == MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()
for index in range(len(pressed_array)):
if pressed_array[index]:
if index == 0:
print('Pressed LEFT Button!')
elif index == 1:
print('The mouse wheel Pressed!')
elif index == 2:
print('Pressed RIGHT Button!')
elif event.type == MOUSEMOTION:
#return the X and Y position of the mouse cursor
pos = pygame.mouse.get_pos()
mouse_x = pos[0]
mouse_y = pos[1]
screen.fill((mouse_x, mouse_y, 0))
screen.blit(text, (40, 100))
pygame.display.update()
希望本文所述对大家Python程序设计有所帮助。
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
AB实验是互联网产品迭代、营销优化、功能升级的核心科学验证手段,通过流量随机分组、对照组与实验组对比,科学验证策略、功能、 ...
2026-08-10在MySQL数据库优化中,索引是提升查询效率、降低数据库IO开销、优化系统性能的核心手段。普通单列索引仅适配简单查询场景,面对 ...
2026-08-10 很多数据分析师每天盯着几十个指标,但当被问到“这套指标要支撑什么业务目标”“指标之间是什么逻辑关系”“业务变化时如何 ...
2026-08-10在数字化市场调研体系中,大数据与小数据是两类核心调研数据形态,分别对应海量行为统计与精准样本深度调研。行业普遍存在认知误 ...
2026-08-07数据透视表是Excel、WPS中最核心的数据分析工具,凭借快速汇总、分组统计、动态筛选的优势,被广泛应用于销量统计、业绩复盘、数 ...
2026-08-07 很多数据分析师每天盯着GMV、DAU、转化率,但当被问到“哪些指标在所有行业都适用”“哪些指标只对电商有意义”“二者如何搭 ...
2026-08-07在商品销量、市场需求、营收规模等业务数据中,季节性波动是最普遍、最核心的数据特征。零售快消、食品餐饮、家电服饰、电商行业 ...
2026-08-06在流量红利消退、市场竞争白热化的商业环境中,传统依托经验、跟风投放、广撒网式的营销模式,逐渐暴露出成本高、精准度低、转化 ...
2026-08-06 很多数据分析师每天盯着GMV、DAU、转化率,但当被问到“什么是指标”“指标和维度有什么区别”“如何定义指标值的计算规则和 ...
2026-08-06【核心关键词】知识、设备、工程师、数字化、建模、算法、工业大数据、数据分析、控制算法、机器学习、深度学习、业务目标、工 ...
2026-08-05增长率是数据分析、业务报表、可视化看板中最核心的指标之一,常用于衡量营收、销量、用户量、流量等业务数据的涨跌幅度与发展趋 ...
2026-08-05小陈是某电商平台的数据分析师。老板交给他一个任务:“我们平台的注册用户已经突破1000万了,想了解一下用户的平均月消费金额。 ...
2026-08-05在机器学习建模、数据预处理、抽样划分、模型初始化训练的全流程中,随机种子是控制实验随机性、保证结果可复现、提升模型稳定性 ...
2026-08-04Python作为面向对象的主流编程语言,核心编程体系由变量、方法、类三大基础要素构成。三者层层递进、相互协作,支撑起所有基础代 ...
2026-08-04 很多数据分析师沉迷于复杂的机器学习算法,却忽略了数据分析最基础也最核心的能力——描述性统计。事实上,80%的商业分析问 ...
2026-08-04为什么学习数据分析? 当下,我们已然步入数据要素价值全面释放的智能时代。数据不再只是零散的数字记录,更是驱动新质生产力运 ...
2026-08-03CDA等级认证证书有效期为三年,三年进行一次年审,主要考察CDA持证人的职业发展情况;学历、工作单位及职业变更情况;继续教育 ...
2026-08-03【核心关键词】金融、岗位、企业、算法、知识、专业、理论、课程、软件、数据分析、业务类型、应用场景、经营管理、大型企业、 ...
2026-08-03在日常数据分析、业务报表复盘、业绩预测工作中,平均增长率是衡量数据长期变化趋势的核心指标,广泛用于营收增长、用户增长、销 ...
2026-08-03很多人把统计学理解为“一堆公式和计算”,却忽略了它的本质——一门让数据“开口说话”的科学。真正的数据分析高手,不是会算平 ...
2026-08-03