京公网安备 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
【核心关键词】大数据、零售商、消费者、供应链、运营、企业、产品、客户、数据模型、大数据平台、数据开发、系统运维、业务逻 ...
2026-06-26在物流配送、供应链履约、终端供货等业务场景中,送货率是衡量企业履约能力、服务质量、供应链稳定性的核心业务指标,直接关联客 ...
2026-06-26 很多数据分析师精通描述性统计,能熟练计算均值、中位数、标准差,但当被问到“用500个样本如何推断10万用户的真实满意度” ...
2026-06-26在数字化管理与数据化运营体系中,指标是连接原始数据与业务决策的核心载体。零散的原始数据只是无意义的数值堆砌,无法直接反映 ...
2026-06-25在Excel数据汇总、财务统计、业务复盘等日常办公场景中,经常需要完成逐行相乘、整体汇总求和的计算需求,最典型的场景就是:单 ...
2026-06-25 很多数据分析师沉迷于复杂的机器学习算法,却忽略了数据分析最基础也最核心的能力——描述性统计。事实上,80%的商业分析问 ...
2026-06-25【核心关键词】主数据、资产、供应商、现金流、企业、精细化、集团、数字化、中国、数据质量、数据管理、经营管理、地产行业、 ...
2026-06-24在数据分析、假设检验、AB测试、学术研究等统计场景中,显著水平(α)与P值(P-value)是判断统计结果是否具有统计学意义的两个 ...
2026-06-24小李刚入职了一家互联网公司的运营部门。第一次参加业务复盘会,运营主管问了一个看似简单的问题:“这个月新用户留存率下降了5 ...
2026-06-24在数字化转型全面渗透的产业背景下,数据分析已成为互联网、金融、零售、制造等几乎所有行业的核心岗位能力。很多初学者对数据分 ...
2026-06-23在企业并购、股权定价、投融资评估、资产核算等资本市场核心场景中,市场法是应用最广泛、市场认可度最高的企业价值评估方法。传 ...
2026-06-23 许多数据分析师精通Excel函数和SQL查询,但当面对一张上万行的销售明细表,要快速回答“哪个地区销量最高”“哪款产品增长最 ...
2026-06-23【核心关键词】运营、证书、金融、客户、产品、软件、销售额、量化、科技、数据分析、金融行业、证券类软件、业务流程、金融机 ...
2026-06-22在企业方案选型、产品迭代评审、供应商筛选、运营效果复盘等决策场景中,单一指标的优劣判断往往无法支撑科学决策。一套转化效果 ...
2026-06-22 很多数据分析师掌握了Excel函数、会写SQL查询,但当被问到“数据从哪里来”“数据加工有哪些步骤”“如何使用分析工具连接数 ...
2026-06-22【核心关键词】软件、洞察力、大数据、产品、经验、硬件、流量、创新、决策、数据安全、网络安全、数据分析、决策制定、数据挖 ...
2026-06-18在方案选型、效果复盘、产品评估、供应商筛选等各类业务决策场景中,仅凭单一指标下结论往往会陷入 “以偏概全” 的误区。多维度 ...
2026-06-18 很多数据分析师精通Excel单元格操作,但当被问到“表结构数据的基本处理单位是什么”“字段和记录的本质区别”“为什么表结 ...
2026-06-18在数据分析、用户运营与业务增长的工作体系中,漏斗拆解是最基础也最高频的问题定位方法。很多业务场景下,我们只能看到最终的转 ...
2026-06-17在数据库开发、数据清洗与报表统计场景中,数值类型转换为日期是高频刚需操作。业务系统常以 Unix 时间戳、整型日期(如20240617 ...
2026-06-17